#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Help with simple program please (beginner)


    Hi there.
    For the output at the end, no matter what, if the output is a float, it will be an extremely high number (over 2 million.) If it is an integer, then the output is 0.



    Code:
    // Trucking Program ~  ~  ~  ~ Sept. 19, 2013
    // This program caluculates the costs and properties of shipping cans inside boxes
    
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        const float boxHeight = 60; //Declaring the dimensions of the box
        const float boxWidth = 100;
        const float boxLength = 75;
        float cansBoxWidth = 0; //number of cans that can fit in the width
        float cansBoxHeight = 0; //number of cans that can fit in in the height
        float cansBoxLength = 0; //number of cans that can fit in the length
        int numCans; //Number of cans needed to ship
        float canHeight; // Height of the cans
        float canRadius; // Radius of the cans
        float canDiameter; //diameter of the cans
        float distance; // Distance needed to transport the boxes
        int cansPerBox; // The # of cans that can fit inside ONE box
        int cansInAllBoxes; //The # of cans in EVERY box
        float loadCost; // The cost of loading/unloading boxes
        int numBoxes; //number of boxes required
        float totalCost; // Grand total
        float distanceCost; // cost of the distance travelled
        
        printf("Welcome to the can trucking program!\n");
        printf("Please enter how many cans you wish to ship: ");
        scanf("%f", &numCans);
        printf("\nPlease enter the height of the cans you want to transport in cm: ");
        scanf("%f", &canHeight);
        printf("\nPlease enter the radius of your cans in cm: ");
        scanf("%f", &canRadius);
        printf("\nPlease enter how far you wish to transport the cans in km: ");
        scanf("%f", &distance);
        //This section gathers all the inputs from the user. ^^^^^   
    
        canDiameter = canRadius * 2;
        
        while (cansBoxHeight < 60)
        {
            cansBoxHeight += canHeight;
            if (cansBoxHeight > 60)
            {
                cansBoxHeight -= canHeight;
                break;
            }
        }
        
        while (cansBoxWidth < 100)
        {
            cansBoxWidth += canDiameter;
            if (cansBoxWidth > 100)
            {
                cansBoxWidth -= canDiameter;
                break;
            }
        }
        
        while (cansBoxLength < 75)
        {
            cansBoxLength += canDiameter;
            if (cansBoxLength > 75)
            {
                cansBoxLength -= canDiameter;
                break;
            }
        }
        
        cansPerBox = (cansBoxHeight / canHeight) * (cansBoxWidth/canDiameter) * (cansBoxLength / canDiameter);   
        // ^^^We have the dimensions of cans per box, but now we need to know how many cans.
        
        cansInAllBoxes = cansPerBox; //We start out with one box, the section below adds more
                                     //boxes until they contain all the cans
                                     
        while (cansInAllBoxes < numCans) //This whole section determines how many boxes are
        {                                //needed to store all the cans.
            numBoxes += 1;
            cansInAllBoxes += cansPerBox;
            if (cansInAllBoxes >= numCans)
            {
                break;
            }
        }
        
        loadCost = 8 * numBoxes;        //Figuring out the other expenses
        distanceCost = 0.37 * distance * numBoxes;
        totalCost = loadCost + distanceCost;
        
        printf("The number of cans per box: %d\n", &cansPerBox);
        printf("The number of boxes: %d\n", &numBoxes);
        printf("Cost of loading  the boxes: %f\n", &loadCost);
        printf("Cost of transporting the boxes: %f\n", &distanceCost);
        printf("Grand total: %f\n", &totalCost);
    
        system("pause");
        return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    You may want to insure you compiler is generating the maximum number of warnings and fix those warnings before continuing.

    main.c||In function ‘main’:|
    main.c|31|warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘int *’ [-Wformat=]|
    main.c|92|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]|
    main.c|93|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]|
    main.c|94|warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]|
    main.c|95|warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]|
    main.c|96|warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]|
    main.c|13|warning: unused variable ‘boxLength’ [-Wunused-variable]|
    main.c|12|warning: unused variable ‘boxWidth’ [-Wunused-variable]|
    main.c|11|warning: unused variable ‘boxHeight’ [-Wunused-variable]|
    Jim
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    Here's a hint for dealing with those warnings: while scanf requires addresses because it needs to know where to put the values, printf only needs values. IOW, while you normally need to use the address operator with scanf, you do not normally need it for printf.

    Comments on this post

    • dingmutt agrees
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You could replace these miserable loops with division:

    cansBoxHeight = (int)(boxHeight/canHeight);

    instead of

    Code:
    while (cansBoxHeight < 60)
        {
            cansBoxHeight += canHeight;
            if (cansBoxHeight > 60)
            {
                cansBoxHeight -= canHeight;
                break;
            }
        }
    Since valid values for your variables like cansBoxHeight are essentially integer, cansBoxHeight and the other measures in "can units" should probably be declared int.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by b49P23TIvg
    You could replace these miserable loops with division:

    cansBoxHeight = (int)(boxHeight/canHeight);

    instead of

    Code:
    while (cansBoxHeight < 60)
        {
            cansBoxHeight += canHeight;
            if (cansBoxHeight > 60)
            {
                cansBoxHeight -= canHeight;
                break;
            }
        }
    Since valid values for your variables like cansBoxHeight are essentially integer, cansBoxHeight and the other measures in "can units" should probably be declared int.
    Thanks for reducing the code. The program, however, still outputs 0.00000 for the floats and over 2 million for the ints. What is the reason for this?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Why is %f acting as a placeholder for a double? Isn't that %g?
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    without reading the documentation, and therefor as I recall, oh no let's test. The test reveals that %g and %f both expect double (compilation warnings for variants on this program are not shown), and that %g thoughtfully considers the value and uses little space to show the value while %f dutifully stamps out what you tell it. Given the default field widths, of course. You could have written this program!
    Code:
    #include<math.h>
    #include<stdio.h>
    
    void show(char*title, double a) {
      puts(title);
      printf("using %%f: %f\n", a);
      printf("using %%g: %g\n", a);
    }
    
    int main() {
      double tau = 2*M_PI;
      show("displaying a billion times TAU.  Search internet for tauday.com .", tau*1e9);
      show("and now, one eighth more than seventeen.", (double)17.125);
      return 0;
    }
    
    
    
    #if 0
    
      result:
    
      $ gcc -Wall c.c -o c.exe
      $ ./c
      displaying a billion times TAU.  Search internet for tauday.com .
      using %f: 6283185307.179586
      using %g: 6.28319e+009
      and now, one eighth more than seventeen.
      using %f: 17.125000
      using %g: 17.125
      $ 
    
    #endif
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    Code:
        ...
        scanf("%d", &numCans); // *** numCans is an integer
        ...
        loadCost = 8.0 * numBoxes; // *** let compiler knows here is floating point
        ...
        printf("The number of cans per box: %d\n", cansPerBox); // *** printf needs value
        printf("The number of boxes: %d\n", numBoxes); // *** printf needs value
        printf("Cost of loading  the boxes: %.3f\n", loadCost); // *** printf needs value
        printf("Cost of transporting the boxes: %.3f\n", distanceCost); // *** printf needs value
        printf("Grand total: %.3f\n", totalCost); // *** printf needs value
                             // set precision of float, e.g. 3
        ...
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Extremely high number.

    Laughter.

    largest integer used in a serious mathematical proof: http://en.wikipedia.org/wiki/Graham's_number

    Infinity is also really, really big.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0
    Fixed!

    The problem was that I had an ampersand in front of the variable in the printf section at the bottom. Rookie mistake!

IMN logo majestic logo threadwatch logo seochat tools logo