## Help with some beginner C code!

here is the code. it just doesn't work...i dont know why.

Code:

`#import <stdio.h>`

int main()

{

int length;

int height;

int area = length*2+height*2;

printf("\nEnter the length of a quadrelateral to calculate the area: ");

scanf("%d",&length);

printf("\nEnter the height of a quadrelateral to calculate the area: ");

scanf("%d",&height);

printf("\nThe area of the quadrelateral is %d\n.",area);

return 0;

}

i know the formula for a quadralateral's area is:

Area=(length*2)+(width*2)

//some random number because it is not initialized, let's say 8

int height;

//some random number because it is not initialized, let's say 5

int area = length*2 + height*2;

//area = 8 * 2 + 5 * 2 = 26

scanf("%d", &length);

//length = whatever you typed in, height = 5, area still equals 26

scanf("%d", &height);

//length = whatever you typed in before, height = whatever you typed in now, area still equals 26

printf("blah blah %d", area);

//prints out 26, because you didn't recalculate area

Sparing you the boring details of computer science and engineering, you'll need to recalculate the area once your lenght and height are at the values you want. There are ways to have it constantly keep track of the variables it uses, but they are above your head for the time being.

i am taking geometry this year, and some programs like this could help me a lot.

Edit: BTW, you are calculating the perimeter of the quadrilateral, not the area. Area is width*height. (to be exact, this would be for a parallelogram)

Tyaedalis Wrote:i still dont understand fully. what should i do to fix my code? i want the user to input the length and height.

Move the line that calculates the area to after the lines where the width and height is entered in by the user but before it's printed out.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).

Code:

`#import <stdio.h>`

int main()

{

int length;

int height;

printf("\nEnter the length of a quadrelateral to calculate the area: ");

scanf("%d",&length);

printf("\nEnter the height of a quadrelateral to calculate the area: ");

scanf("%d",&height);

int area = length*2+height*2;

printf("\nThe area of the quadrelateral is %d\n.",area);

return 0;

}

it still doesn't work.

Edit: FYI, with all but the newest standard of C, you're supposed to put all declarations of variables before anything else, so older compilers might complain about that code.

Code:

`printf("\nEnter the length of a quadrelateral to calculate the area: ");`

[b]scanf("%d",&length);[/b]

printf("\nEnter the height of a quadrelateral to calculate the area: ");

[b]scanf("%d",&height);[/b]

printf("\nThe area of the quadrelateral is %d\n.",area);

bold equals output.

anyway, you mean when i run it? ill give you the result,

Code:

`[Session started at 2005-09-12 21:43:05 -0700.]`

Enter the length of a quadrelateral to calculate the area: 2.5

Enter the height of a quadrelateral to calculate the area:

The area of the quadrelateral is 4

.

Executable â€œTesting codesâ€ has exited with status 0.

BTW, notice how the period is on the second line? (for your result) To fix that, put the \n after the .

You can't tell the area of a quadrilateral with that information; in fact, since "length" and "height" don't necessarily apply usefully to any given quadrilateral, you're not calculating anything meaningful. Toy examples are fine, but really... ;-)

Anyway, the area of a rectangle or parallelogram is length * height (but be careful how you measure them); the perimeter of a rectangle is 2*length + 2*height. This code:

Code:

`#include <stdio.h>`

int main(int argc, const char **argv)

{

float length;

float height;

float area;

float maybe_perimeter;

printf("\nTo calculate the area of a rectangle or parallelogram, first enter its length:\n> ");

scanf("%f", &length);

printf("\nNow enter its height:\n> ");

scanf("%f", &height);

area = length * height;

printf("\nThe area of the quadrilateral is %f; ", area);

maybe_perimeter = 2 * length + 2 * height;

printf("if it's a rectangle, its perimeter is %f.\n\n", maybe_perimeter);

return 0;

}

gives this output:

Code:

`william-reades-powerbook-g4-17:~ william$ pico quad.c`

william-reades-powerbook-g4-17:~ william$ gcc quad.c -o quad

william-reades-powerbook-g4-17:~ william$ ./quad

To calculate the area of a rectangle or parallelogram, first enter its length:

> 1.8

Now enter its height:

> 2.5

The area of the quadrilateral is 4.500000; if it's a rectangle, its perimeter is 8.600000.

william-reades-powerbook-g4-17:~ william$

herre's my code:

Code:

`#import <stdio.h>`

int main()

{

float length;

float height;

float area;

printf( "\nTo calculate the area of a rectangle or parallelogram, enter it's length: \n" );

scanf( "%f" , &length);

printf( "\nNow, enter it's width: \n" );

scanf( "%f" , &height);

area = length * height;

printf( "\nThe area of the quadrelateral is %f.\n\n" , area );

return 0;

}

now, coiuld i make it a GUI? or is that not compatable with c? (im using xCode, btw)