Beginner Need Help with Program!

Hi im a beginner at this and i need help to write a program that reads in the length of a square from the user, and computes the area, perimeter, and diagonal of the square. Thanks
What have you done so far? Us doing your homework for you wont help you, it will just prevent you from understanding what you are doing. Just work out your algorithms for each of those, and then put them into your program. For example, your algorithms might be this:

Perimeter = 4 * side
Area = side * side
Diagonal = square_root_of_2 * side


And work it out in your program. Good luck!
Last edited on
Can you show us your code please? Don't forget code tags - use the <> button on the right under the format menu.

There is also a tutorial, reference material, and articles at the top left of this page.

We are all pleased to help, just would like to see a little effort on your part
Thank You for the help, i figured out the perimeter and the area i was just unsure how to compute the diagonal
OK, for your reference, look up Pythagoras' theorem. I just wrote a simplified version there, as it prevents work that would need to be done by the computer. In its main form, Pythagoras' theorem is that if you have a right angled triangle, the square of the hypotenuse (the longest side, opposite the right angle) is equal to the square of the other two sides. Say you had a right angled triangle h, a, b, it could be shown like this:
h2 = a2 + b2

I just simplified it by square rooting both sides and simplifying based on the lengths of a side in a square being equal.
unsure how to compute the diagonal

NT3 mentioned the formula but didn't say how he got it so here is how:

Think of a square as two triangles and we have two sides a and b. Now to find a missing side of a triangle the formula is: a^2 + b^2 = c^2

So then it can be wrote as sqrt( a^2 + b^2 ) = sqrt( c^2 ) == sqrt( a^2 + b^2) = c

In squares though the sides are all the same so a == b which means a + b == 2a

we now have

sqrt( 2a^2 ) = c

which can now be wrote as:

sqrt( 2 ) * sqrt( a^2) = c

which can be wrote as:

sqrt( 2 ) * a = c


*edit took to long typing NT3 already mentioned :P
Last edited on
Youre probably going to want to use the <math.h> library as well as it contains pow(to find an exponent e.g 2^2 and sqrt (to find the square root of a number)

Example code
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <math.h>  // pow and sqrt
using namespace std;

int main()
{
cout << pow(7, 3); // Same as 7^3
cout << sqrt(100); // square root of 100


}
Last edited on
Topic archived. No new replies allowed.