#includeusingnamespacestd;floatarea(floata,floatn){ "/> #includeusingnamespacestd;floatarea(floata,floatn){ ">

Home >Backend Development >C++ >What is the area of ​​the square formed by repeatedly connecting midpoints in a C program?

What is the area of ​​the square formed by repeatedly connecting midpoints in a C program?

WBOY
WBOYforward
2023-09-01 08:01:071404browse

Suppose we have a square with side length "a". We will make more squares by repeatedly connecting the midpoints of the squares. The number of repetitions is n times. We have to find the area of ​​the nth square.

What is the area of ​​the square formed by repeatedly connecting midpoints in a C program?

Since the side length of the outer square is "a", the area is

What is the area of ​​the square formed by repeatedly connecting midpoints in a C program?

Now use Pythagoras According to Sri Lanka's theorem, we can get that the area of ​​the second rectangle is -

Similarly, the area of ​​the third rd square is -

Using this we can know that the area of ​​the nth square is-

What is the area of ​​the square formed by repeatedly connecting midpoints in a C program?

Example

#include <iostream>
#include <cmath>
using namespace std;
float area(float a, float n) {
   if (a < 0 ) //if the value is negative it is invalid
      return -1;
   float area = (a*a) / pow(2, n-1);
   return area;
}
int main() {
   float a = 20.0, n = 10.0;
   cout << "Area : " << area(a, n);
}

Output

Area : 0.78125

The above is the detailed content of What is the area of ​​the square formed by repeatedly connecting midpoints in a C program?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete