C program to calculate area and a few other things

In summary, the program fails to compile and gives the following errors:- Error: 'perimter_yardFT' undeclared (first use in this function)- Error: 'perimter_houseFT' undeclared (first use in this function)- Error: 'houseleng' undeclared (first use in this function)- Error: expected ')' before 'perimeter_yardFT'
  • #1
Arnoldjavs3
191
3
Mod note: Edited the following to remove spoiler tags and to put code in code tags

Homework Statement


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.

Homework Equations

The Attempt at a Solution


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:
Physics news on Phys.org
  • #2
Arnoldjavs3 said:
Mod note: Edited the following to remove spoiler tags and to put code in code tags

Homework Statement


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)|
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.

Arnoldjavs3 said:
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.

Homework Equations

The Attempt at a Solution


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:
  • #3
In addition to my comments about undeclared variables, here are some more comments.
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.
C:
scanf("%f, %f \n", yardlength, yardwidth);

The following lines contain several errors, including undeclared variables. I have no idea what you're trying to do in the third line below.
C:
    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);

The control string in each of your printf() calls should end with a \n character so that output shows up on separate lines.
C:
printf("The time required to cut the grass in the yard is: \t %.2f", time);

One more thing -- please stop using spoiler tags...
 
Last edited:
  • #4
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:
    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:
#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
You should fix all of your scanf() calls.
C:
    scanf("%f \n", yardlength);
    scanf("%f \n", yardwidth);
    scanf("%f \n", houselength);
    scanf("%f \n", housewidth);
(I've removed the intervening printf() calls.

The first one should look like this:
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
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 Langauge 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
Arnoldjavs3 said:
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 Langauge 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.
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
Mark44 said:
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.

I am really enjoying it right now, as I feel like I am understanding a lot 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
Arnoldjavs3 said:
I am really enjoying it right now, as I feel like I am understanding a lot 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.
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.
 

1. What is a C program to calculate area?

A C program to calculate area is a computer program written in the C programming language that is designed to calculate the area of a given shape, such as a square, rectangle, circle, or triangle. This program uses mathematical formulas and input from the user to determine the area of the shape.

2. What are the most common shapes that can be calculated using a C program?

The most common shapes that can be calculated using a C program are squares, rectangles, circles, and triangles. These shapes have well-defined mathematical formulas for determining their area, making them relatively easy to calculate using a computer program.

3. How does a C program calculate the area of a shape?

A C program calculates the area of a shape by using the appropriate mathematical formula for the given shape. For example, to calculate the area of a circle, the program would use the formula A = πr^2, where A is the area and r is the radius. The program would prompt the user for the radius and then perform the necessary calculations to determine the area.

4. Can a C program calculate the area of irregular shapes?

Yes, a C program can calculate the area of irregular shapes, but it may require more complex mathematical formulas and programming logic. The program would need to break down the irregular shape into smaller, simpler shapes (such as triangles or rectangles) and then use those shapes' area formulas to determine the total area of the irregular shape.

5. What are some other things that a C program can calculate besides area?

In addition to calculating area, a C program can also calculate other properties of shapes, such as perimeter, volume, and surface area. It can also perform more complex calculations, such as trigonometric functions and logarithms. Additionally, a C program can be used for a variety of other calculations, such as financial calculations, statistical analysis, and data processing.

Back
Top