Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C problem.

Options
  • 05-10-2007 6:51pm
    #1
    Closed Accounts Posts: 4,564 ✭✭✭


    Hello folks, I am having a small difficulty with a programming problem I got from my College programming lecture the other day.
    Bear in mind I am a first year NoOb so go easy:)

    Basically, I just wrote a program to calculate the volume and surface area of a Cuboid.
    The problem is that I can't seem to initialize the local variables without the program returning a value of 0.000....
    Am I missing something stupidly Obvious >_< ?
    Thanks in advance.
    /* program to calculate the Volume and surface area of a cube*/
    
    #include <stdio.h>
    #include <conio.h>
    
    /* This section calculates the volume of the cube*/
    
    int main()
    
    {
        
    float  width=0;
    float  lenght=0;
    float  height=0;
    float  volume=0;
    float  surface_area=0;
    
    
    
    volume = lenght*width*height;
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("please enter the height of the object:\n");
    scanf("%f" , &height);
    
    printf("the volume of the object is: %f\n" ,volume);
    
    printf("\n");
    printf("\n");
    
    
    
    /* this section calculates the surface area of the cube*/
    
    float lenght_2=0;
    float width_2=0;
    
    
    surface_area =lenght_2*width_2*6;
    
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("the surface area of the cube is: %f\n" ,surface_area);
    
    
     _getch();
    
    
    }
    
    


Comments

  • Registered Users Posts: 1,228 ✭✭✭carveone


    Um. I see you've never programmed before :)

    You realise that
    volume = lenght*width*height;

    is not like a function. You aren't saying that volume is l*w*h and have it remember that, you are saying "let volume equal the expression on the right hand side".

    In this case l = 0, w = 0, h = 0 therefore

    volume = l * w * h = 0 * 0 * 0 = 0.

    Which is why you get 0.

    Simply move the volume =... expression down, after you get some numbers for it!!!!


  • Registered Users Posts: 1,228 ✭✭✭carveone


    To explain further...

    There is no difference whatsoever between

    l=0; w=0; h=0;
    volume = l * w * h;

    and

    volume = 0;

    Ok?


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    carveone wrote:
    To explain further...

    There is no difference whatsoever between

    l=0; w=0; h=0;
    volume = l * w * h;

    and

    volume = 0;

    Ok?

    I understand what you are saying, its just I thought that to initialize a variable, the initial value must be set to 0 or else its undefined.
    If I remove the values I just get an error during execution.
    I have done some elementary programming before btw:)


  • Registered Users Posts: 1,636 ✭✭✭henbane


    He's not telling you not to initialise, he's telling you to assign the values you want computed before working out the volume and surface area.


  • Registered Users Posts: 1,228 ✭✭✭carveone


    henbane wrote:
    He's not telling you not to initialise, he's telling you to assign the values you want computed before working out the volume and surface area.

    What he said :p


  • Advertisement
  • Registered Users Posts: 593 ✭✭✭Cathy


    When you ask it to calculate the volume, it still thinks that length and width and height are zero.

    Put the bit where you read in these values from the user before you ask it to calculate the volume, and then it'll have the right numbers. It should give you the right answer then, and not just zero.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Cathy wrote:
    When you ask it to calculate the volume, it still thinks that length and width and height are zero.

    Put the bit where you read in these values from the user before you ask it to calculate the volume, and then it'll have the right numbers. It should give you the right answer then, and not just zero.

    Thanks, but the above Doesen't seem to work.


  • Registered Users Posts: 2,379 ✭✭✭toiletduck


    Post up the code that isn't working.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    the surface area won't work because you have

    length_2 and width_2 initialised to zero ala your first problem.

    i'm assuming that you are getting the volume and the surface area of the same cube right?

    if so there's no need to have the length_2 and width_2 variable as you can use the two (length and width) from the volume equation to calculate the surface area (l*w*6)

    if it's not the same cube you need to do your surface area calculation after you assign values (from user input).

    since the compiler interprets the program line by line you need to restructure your program. when you declare variables like float width you are basically labelling a piece of memory in which you want to store a floating point number in, the value inside can be anything you just don't know (it's more often or not 0). Now since you don't know what's inside these variables you cannot determine the result of length*width*height so you need to store the user input into the variables before you do your calculation.

    on a off topic i can tell what lecturer you have (as you go to DIT) by this line int main() :D


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    
    /* program to calculate the Volume and surface area of a cube*/
    
    #include <stdio.h>
    #include <conio.h>
    
    /* This section calculates the volume of the cube*/
    
    int main()
    
    {
    	
    float  width;
    float  lenght;
    float  height;
    float  volume;
    float  surface_area;
    
    
    
    volume = lenght*width*height;
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("please enter the height of the object:\n");
    scanf("%f" , &height);
    
    printf("the volume of the object is: %f\n" ,volume);
    
    printf("\n");
    printf("\n");
    
    
    
    /* this section calculates the surface area of the cube*/
    
    
    
    surface_area =lenght*width*6;
    
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("the surface area of the cube is: %f\n" ,surface_area);
    
    
     _getch();
    
    
    }
    
    
    


  • Advertisement
  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Cremo wrote:
    i'm assuming that you are getting the volume and the surface area of the same cube right?

    if so there's no need to have the length_2 and width_2 variable as you can use the two (length and width) from the volume equation to calculate the surface area (l*w*6)



    on a off topic i can tell what lecturer you have (as you go to DIT) by this line int main() :D

    Yeah, That was a bit silly so I removed it and that particular part seems ok now:o


  • Registered Users Posts: 593 ✭✭✭Cathy


    Naikon wrote:
    
    /* program to calculate the Volume and surface area of a cube*/
    
    #include <stdio.h>
    #include <conio.h>
    
    /* This section calculates the volume of the cube*/
    
    int main()
    
    {
    	
    float  width;
    float  lenght;
    float  height;
    float  volume;
    float  surface_area;
    
    
    
    volume = lenght*width*height;  [b]<-- put this line after the printf and scanf statements (but before the printf line that says what the volume is)[/b]
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("please enter the height of the object:\n");
    scanf("%f" , &height);
    
    printf("the volume of the object is: %f\n" ,volume);
    
    printf("\n");
    printf("\n");
    
    
    
    /* this section calculates the surface area of the cube*/
    
    
    
    surface_area =lenght*width*6; [b]<-- put this line after the printf and scanf statements (but before the printf line that says what the surface area is)[/b]
    
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("the surface area of the cube is: %f\n" ,surface_area);
    
    
     _getch();
    
    
    }
    
    
    

    Do you understand what we're saying here? You initialise the variables to zero just so that there's something there, but you have to read in the actual values before you tell the program to calculate the volume and surface area. The printf line asks the user to enter the value, and the scanf line reads in the value and saves it to the variable name. Before these two lines, for example, length is zero, but after them, it's the number you put in.

    Since you're asking it to calculate the volume and surface area before you read in the proper values, it's going to give you zero.


    Naikon wrote:
    Yeah, That was a bit silly so I removed it and that particular part seems ok now:o

    The reason that that's working is that you're now asking it to calculate the surface area after the length and width have been read in.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    Naikon wrote:
    Yeah, That was a bit silly so I removed it and that particular part seems ok now:o

    there is no need now for getting the user to input the length and width again for the second calculation, they've done it from the first part.

    also just noticed myself that if it's a cube you're trying to find the volume of it cannot have differing width and height values, otherwise it's a cuboid.


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Thanks for the help everyone.
    It works now:)
    I was placing the line after the user input lines, but also after the following printf statement, thus messing things up.
    Ok Cremo, its a cuboid/rectangle etc:p
    
    
    /* program to calculate the Volume and surface area of a cuboid*/
    
    
    #include <stdio.h>
    #include <conio.h>
    
    /* This section calculates the volume of the cuboid etc*/
    
    int main()
    
    {
    	
    float  width;
    float  lenght;
    float  height;
    float  volume;
    float  surface_area;
    
    
    printf("please enter the lenght of the object:\n");
    scanf("%f" , &lenght);
    
    printf("please enter the width of the object:\n");
    scanf("%f" , &width);
    
    printf("please enter the height of the object:\n");
    scanf("%f" , &height);
    
    volume = lenght*width*height;
    
    printf("the volume of the object is: %f\n" ,volume);
    
    printf("\n");
    printf("\n");
    
    
    /* this section calculates the surface area of the object etc*/
    
    
    
    surface_area =lenght*width*6;
    
    
    printf("the surface area of the object is: %f\n" ,surface_area);
    
    
     _getch();
    
    
    }
    
    
    
    

    *Edits*


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    Not really a C problem, but you spelt 'length' wrong...

    (although at least it's consistent, which is more important in programming!)

    Also, one of these is redundant:

    printf("\n");
    printf("\n");

    You could just use:

    printf("\n\n");


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    FruitLover wrote:
    You could just use:

    printf("\n\n");

    Thanks for the tip.


Advertisement