Write a C++ program that will calculate and display the areas of some standard g
ID: 667227 • Letter: W
Question
Write a C++ program that will calculate and display the areas of some standard geometric figures. Specifically, the program needs to display the areas of the following geometric figures for length of side 1, 2, 3, 4, 5, and 6. You need to have a separate area function for each geometric figure.
equilateral triangle
square
regular pentagon
regular hexagon
regular heptagon
regular octagon
For each of the following code fragments, give an analysis of the running time (Big-Oh notation)
sum = 0;
for (int i = 0; i < n; i++)
sum++;
sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum++;
sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n * n; j++)
sum++;
sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
sum++;
sum 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < i * i; j++)
for (int k = 0; k < j; k++)
sum++;
sum = 0;
for (int i = 1; i < n; i++)
for (int j = 1; j < i * i; j++)
if (j % i == 0)
for (k = 0; k < j; k++)
sum++;
Explanation / Answer
//main.cpp
//this software uses the formula A=L2*n/tan(2*pi/2n) to find the area of any regular polygon.
//if you have doubts about it please leave a comment to clarify it.
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int length,figure;
float equilateral(int a)
{float auxiliar, area_equilateral;
auxiliar=((float)a)*((float)a)*sqrt(3);
area_equilateral=auxiliar/4;
printf ("the equilateral area is %f", area_equilateral);
return area_equilateral;
}
int square(int b)
{int area_square;
area_square=b*b;
printf ("the square area is %d", area_square);
return area_square;
}
float pentagon(int c)
{float tangent, n=5, auxiliar, area_pentagon;
tangent = 2*tan(3.14159/n);
auxiliar=((float)c)*((float)c)*n;
area_pentagon=auxiliar/tangent;
printf("the pentagon area is %f", area_pentagon);
return area_pentagon;
}
float hexagon (int d)
{//the area of a hexagon is the same of six equilateral triangle
float auxiliar, area_equilateral, area_hexagon;
auxiliar=((float)d)*((float)d)*sqrt(3);
area_equilateral=auxiliar/4;
area_hexagon=6*area_equilateral;
printf ("the hexagon area is %f", area_hexagon);
return area_hexagon;
}
float heptagon (int e)
{float tangent, n=7, auxiliar, area_heptagon;
tangent = 2*tan(3.14159/n);
auxiliar=((float)e)*((float)e)*n;
area_heptagon=auxiliar/tangent;
printf("the heptagon area is %f", area_heptagon);
return area_heptagon;
}
float octagon (int f)
{float tangent, n=8, auxiliar, area_octagon;
tangent = 2*tan(3.14159/n);
auxiliar=((float)f)*((float)f)*n;
area_octagon=auxiliar/tangent;
printf("the octagon area is %f", area_octagon);
return area_octagon;
}
int main(int argc, char** argv) {
printf("insert the length of side 1, 2, 3, 4, 5, or 6 ");
scanf ( "%i", &length );
printf ("choose the geometric figure ");
printf ("1) equilateral triangle 2)square ");
printf ("3) regular pentagon 4) regular hexagon ");
printf ("5) regular heptagon 6) regular octagon ");
scanf ( "%i", &figure );
// printf("%i", figure);
// printf("%i", length);
switch (figure)
{
case 1:
equilateral (length);
break;
case 2:
square(length);
break;
case 3:
pentagon(length);
break;
case 4:
hexagon(length);
break;
case 5:
heptagon(length);
break;
case 6:
octagon(length);
break;
default:
printf("please insert a valid option ");
break;
}
return 0;
}
-------------------------------------
For each of the following code fragments, give an analysis of the running time (Big-Oh notation)
sum = 0;
for (int i = 0; i < n; i++)
sum++;
The loop executes N times, so the sequence of statements also executes N times. If we
assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N)
overall.
sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum++;
The outer loop executes N times. Every time the outer loop executes, the inner loop
executes N times. As a result, the statements in the inner loop execute a total of N * N
times. Thus, the complexity is O(N2).
sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n * n; j++)
sum++;
The outer loop executes N times. Every time the outer loop executes, the inner loop
executes N*N times. As a result, the statements in the inner loop execute a total of N * (N*N)
times. Thus, the complexity is O(N3).
sum = 0;
for (int i = 0; i < n; i++) // runs n times
for (int j = 0; j < i; j++) //runs n^2
sum++;
O(N^3)
sum 0;
for (int i = 0; i < n; i++) //runs n times
for (int j = 0; j < i * i; j++) //runs (N^2)^2=N^4
for (int k = 0; k < j; k++)// RUNS (N4)^2=N^8
sum++;
O(N^13)
sum = 0;
for (int i = 1; i < n; i++) \ RUNS N TIMES
for (int j = 1; j < i * i; j++) \ RUNS (N^2)^2 = N^4
if (j % i == 0)
for (k = 0; k < j; k++)
sum++;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.