Help with some beginner C code!
I am trying to make a calculator type thing to test my skills.
here is the code. it just doesn't work...i dont know why.
i know the formula for a quadralateral's area is:
Area=(length*2)+(width*2)
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)
int length;
//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.
//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 still dont understand fully. what should i do to fix my code? i want the user to input the length and height.
i am taking geometry this year, and some programs like this could help me a lot.
i am taking geometry this year, and some programs like this could help me a lot.
You have to calculate the area after you read in the values. Since you haven't read the values yet with the way you currently did it, they aren't what you wanted them to be.
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)
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).
i got this:
it still doesn't work.
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.

What's the input and output?
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.
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.
Actually, bold equals input, but that's not what I was talking about. I mean what do you put in as the values then what does it print out as the calculation.
oh yeah, those are input, sorry
.
anyway, you mean when i run it? ill give you the result,

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.
First of all, since you are using ints, 2.5 probably breaks it. To allow it to take 2.5, you need to change all the ints to either float or double, then change %d to %f in all your printf and scanf functions.
BTW, notice how the period is on the second line? (for your result) To fix that, put the \n after the .
BTW, notice how the period is on the second line? (for your result) To fix that, put the \n after the .
oops, forgot aout the float and missed the period. fixed now. ill update more tomorrow. thanks for all the help! i planon getting more help on other days. just remember that!
I don't mean to be a complete pedant, but I seem not to be able to help myself :-/. So:
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:
gives this output:
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$
wow, thanks. i modified my code and now i understand everything!
that really helped. (btw, i didnt copy and paste yours
)
herre's my code:
now, coiuld i make it a GUI? or is that not compatable with c? (im using xCode, btw)


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)
I definitely wouldn't jump from just beginning to understand a few functions and variable types to trying to have a GUI. You can have a GUI with C, but you have to use either an API such as GLUT or SDL (where you would have to do a lot of the stuff yourself, BTW) or use the Carbon API, which can also get quite complicated. You could also use Objective C with Cocoa. Either way, I wouldn't try it for a while.