# Homework Help: C program to calculate area and a few other things

1. Sep 30, 2015

### Arnoldjavs3

Mod note: Edited the following to remove spoiler tags and to put code in code tags
1. The problem statement, all variables and given/known data
Hi, currently I have this assignment:
Write a program by going through the 6 problem solving steps that reads in the length and width of a rectangular yard in feet, the length and width of a rectangular house located in the yard in feet, and calculates and displays the time required to cut the grass in the yard at the rate of 2 square feet per second (we assume that the part of the yard that is not covered by the house is covered by grass and needs to be cut). The computed time has to be given in Hours, Minutes, and Seconds. Your program should also print the areas (length * width) of (i) the yard, (ii) the house and (iii) the grass area in both square feet and square meter. Your program is also required to print the perimeters (2 * (length + width)) of (i) the yard and (ii) the house in both feet and meter. Note that 1 (foot) feet is equal to 0.3048 meter.

Currently whenever I attempt to compile my program it gives me the following errors:

C:\Users\Britannia\Documents\assign2.c||In function 'main':|
C:\Users\Britannia\Documents\assign2.c|20|error: 'perimter_yardFT' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assign2.c|20|note: each undeclared identifier is reported only once for each function it appears in|
C:\Users\Britannia\Documents\assign2.c|21|error: 'perimeter_yardM' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assign2.c|22|error: 'perimter_houseFT' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assign2.c|22|error: 'houseleng' undeclared (first use in this function)|
C:\Users\Britannia\Documents\assign2.c|22|error: expected ')' before 'perimeter_yardFT'|
C:\Users\Britannia\Documents\assign2.c|23|error: 'perimter_houseM' undeclared (first use in this function)|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

It's telling me my variables are not declared, however I've declared them in the beginning block.

2. Relevant equations

3. The attempt at a solution
Code (C):

#include <stdio.h>
#include <math.h>
// #include <stdlib.h>
#define CONVERSIONCONSTANT 10.7639

int main() {
float time, yardarea_FT, yardarea_M, housearea_FT, housearea_M, grassarea_FT, grassarea_M, yardlength, yardwidth, houselength, housewidth, perimeter_yardFT, perimter_yardM, perimeter_houseFT, perimeter_houseM;

printf("Enter the yard length and width in feet in such order \n");
scanf("%f, %f \n", yardlength, yardwidth);
printf("Enter the house length and width in feet in such order \n");
scanf("%f, %f \n", houselength, housewidth);

yardarea_FT = yardlength * yardwidth;
yardarea_M = yardarea_FT * CONVERSIONCONSTANT;
housearea_FT = houselength * housewidth;
housearea_M = housearea_FT * CONVERSIONCONSTANT;
grassarea_FT = yardarea_FT - housearea_FT;
grassarea_M = grassarea_FT * CONVERSIONCONSTANT;
perimter_yardFT = (yardlength + yardwidth) * 2;
perimeter_yardM = (perimter_yardFT * CONVERSIONCONSTANT);
perimter_houseFT = (houseleng perimeter_yardFT, perimter_yardM, perimeter_houseFT, perimeter_houseM;th + housewidth) * 2;
perimter_houseM = (perimter_houseFT * CONVERSIONCONSTANT);

time = grassarea_FT / 2;

printf("The time required to cut the grass in the yard is: \t %.2f", time);
printf("The area of the yard in square feet is: \t%.2f", yardarea_FT);
printf("The area of the yard in square meters is: \t%.2f", yardarea_M);
printf("The area of the house in square feet is: \t%.2f", housearea_FT);
printf("The area of the house in square meters is: \t%.2f", housearea_M);
printf("The area of the grass in square feet is: \t%.2f", grassarea_FT);
printf("The area of the grass in square meters is: \t%.2f", grassarea_M);

printf("The perimeter of the yard in feet is: \t%.2f", perimeter_yardFT);
printf("The perimeter of the yard in square meters is: \t%.2f", perimeter_yardM);
printf("The perimeter of the house in feet is: \t%.2f", perimeter_houseFT);
printf("The perimeter of the house in square meters is: \t%.2f", perimeter_houseM);

return 0;

}

I've read something that said C can only have variables declared in the starting block, however is that not what I've done?

Another question I have is how can I organize my program to look cleaner? I'm a rookie right now with a week of experience. And would this program even work if I were to get past my current issues?

Thanks.

Last edited by a moderator: Sep 30, 2015
2. Sep 30, 2015

### Staff: Mentor

You declared a variable named perimeter_yardFT, but are using the variable above, which wasn't declared. Most of your errors seem to be inconsistencies between the declared variable names and the ones you actually use, that have slightly different spellings.

Something that you are doing right is to use meaninful names for your variables. Just make sure that you are consistent in how you spell each variable.

Last edited: Sep 30, 2015
3. Sep 30, 2015

### Staff: Mentor

When you get input from the user, as in the line below, the best thing to do is to input one variable at a time. In the following line of code, because of the way scanf works, the user will have to enter a number, followed by a comma, and then another number.
Make it easy on the user by prompting for a single value, and then inputting that value, followed by echoing (printing) that value again so that the user can see that his/her input was successfully taken by the program.
The following lines contain several errors, including undeclared variables. I have no idea what you're trying to do in the third line below.
The control string in each of your printf() calls should end with a \n character so that output shows up on separate lines.
One more thing -- please stop using spoiler tags...

Last edited: Mar 21, 2016
4. Sep 30, 2015

### Arnoldjavs3

Okay thank you for the help! I used spoiler tags as I thought it was neater, but I'll use code tags instead.
I'm a little embarrassed I didn't even realize the variables were just spelled incorrectly...

The line:
Code (Text):
perimter_houseFT =(houseleng perimeter_yardFT, perimter_yardM, perimeter_houseFT, perimeter_houseM;th + housewidth)*2;
was meant to be:
perimeter_houseFT = (houselength + housewidth) * 2;

How it progressed into the first line I have no clue.
Anyhow, I've managed to compile the program successfully without any errors, however when I run the program it will close after the user inputs the first value.

Here is my new code:
Code (Text):

#include <stdio.h>
#include <math.h>
#define CONVERSIONCONSTANT 10.7639

int main() {
float time, yardarea_FT, yardarea_M, housearea_FT, housearea_M, grassarea_FT, grassarea_M, yardlength, yardwidth, houselength, housewidth, perimeter_yardFT, perimeter_yardM, perimeter_houseFT, perimeter_houseM;

printf("Enter the yard length  in feet\n");
scanf("%f \n", yardlength);
printf("You entered: %f\n", yardlength);
printf("Enter the yard width in feet\n");
scanf("%f \n", yardwidth);
printf("You entered: %f\n", yardwidth);
printf("Enter the house length in feet\n");
scanf("%f \n", houselength);
printf("You entered: %f\n", houselength);
printf("Enter the house width in feet\n");
scanf("%f \n", housewidth);

yardarea_FT = yardlength * yardwidth;
yardarea_M = yardarea_FT * CONVERSIONCONSTANT;
housearea_FT = houselength * housewidth;
housearea_M = housearea_FT * CONVERSIONCONSTANT;
grassarea_FT = yardarea_FT - housearea_FT;
grassarea_M = grassarea_FT * CONVERSIONCONSTANT;
perimeter_yardFT = (yardlength + yardwidth) * 2;
perimeter_yardM = (perimeter_yardFT * CONVERSIONCONSTANT);
perimeter_houseFT = (houselength + housewidth) * 2;
perimeter_houseM = (perimeter_houseFT * CONVERSIONCONSTANT);

time = grassarea_FT / 2;

printf("The time required to cut the grass in the yard is: \t %.2f\n", time);
printf("The area of the yard in square feet is: \t%.2f\n", yardarea_FT);
printf("The area of the yard in square meters is: \t%.2f\n", yardarea_M);
printf("The area of the house in square feet is: \t%.2f\n", housearea_FT);
printf("The area of the house in square meters is: \t%.2f\n", housearea_M);
printf("The area of the grass in square feet is: \t%.2f\n", grassarea_FT);
printf("The area of the grass in square meters is: \t%.2f\n", grassarea_M);

printf("The perimeter of the yard in feet is: \t%.2f\n", perimeter_yardFT);
printf("The perimeter of the yard in square meters is: \t%.2f\n", perimeter_yardM);
printf("The perimeter of the house in feet is: \t%.2f\n", perimeter_houseFT);
printf("The perimeter of the house in square meters is: \t%.2f\n", perimeter_houseM);

return 0;

}

The CMD prompt shows "Process returned -1073741819 <0xC0000005>"

5. Sep 30, 2015

### Staff: Mentor

You should fix all of your scanf() calls.
(I've removed the intervening printf() calls.

The first one should look like this:
Code (C):
scanf("%f", &yardlength);
The control string consists of just the format specifier: %f Don't put in a newline character
The variable name appears with a preceding & character. scanf() requires the address of a memory location into which it stores the input value.

Change the other three calls to scanf() in a similar way.

If you run your program in its own command prompt window, the program will run and the output will remain displayed. If you run your program in a debugger (I'm thinking Visual Studio, which might not be what you're using), the debugger opens a separate command window, but closes it when your program is done, so the window opens and closes so fast it's hard to tell what's happening.

Another thing you can do is to add another call to scanf() or other input function near the bottom of your code. The only purpose of this input is to keep the program from exiting. You don't need to do anything with the input value.

6. Sep 30, 2015

### Arnoldjavs3

Alright! Thanks alot. I got it to work the way I want it. I use codeblocks as my C compiler and I don't have that issue where it closes too fast. I have to press a key for it to close the prompt.

Just out of curiosity, do you think C is a good program to learn first? I was told otherwise since it's the closest high level language to Machine Language that it would end up confusing me. (And apparently memory management in C is painful to deal with?) So far I'm doing fine in the course.

7. Sep 30, 2015

### Staff: Mentor

C has been called a "high-level assembly language" (not machine language, which is all numbers). However, it's a lot higher level than assembly language.

IMO, C is a good language to start with. Although it doesn't have some of the features of more modern languages, such as object-oriented programming (OOP), inheritance, polymorphism, and the like, learning it gets you off to a good start at procedural programming and the use of control structures such as for and while loops, and branching structures such is if ... else and switch. Many of the newer languages derive from C to a lesser of greater extent. For example, C++, C#, Java, JavaScript, Python, and probably a few others. Even Matlab builds on C, particularly in the similarity of how it does output.

Once you get a good handle on C, learning other languages will be a little easier. Other languages, such as C++ and Java, that feature OO aspects, take a while to learn, but you won't have to start completely from scratch.

8. Oct 1, 2015

### Arnoldjavs3

I am really enjoying it right now, as I feel like I am understanding alot more about the computer as well. Hopefully I can transition into either C++ or java once I've had my fill of C, as it seems like those two are very widely used. The only issue right now I'm imposed with, is that it feels like all my professors / TA's are relatively worthless. They're useful for reference, but they don't seem to teach anything properly. Is computer science simply picking everything up on your own? I just had a lab where the TA's left a problem for us to solve, but they didn't teach anything. I had no idea what to do yet it was being graded for participation.

9. Oct 1, 2015

### Staff: Mentor

The role of the TA is usually less as a teacher and more as an assistant, fielding specific questions and the like. I took a summer class in Fortran at a nearby, well-known university (it was long ago), not as part of any degree requirements, but for my own personal knowledge. In the first class session, the prof admitted that he didn't know much about Fortran, and he didn't know much about the new operating system that had been installed (VAX VMS). He spent a good part of the first class period chit-chatting with some students that he apparently already knew. After the class was over (an hour and a half), the only thing on the board was something unrelated to Fortran scrawled diagonally across one of the blackboards. I decided to drop that class, and wound up taking it the following summer.