Thread: Data type issues

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    9

    Data type issues

    Hello,

    I wrote the code for my homework assignment (I did this on my own and am not trying to get someone else to do my homework) and I cannot get it to compute correctly. I am using code::blocks to compile the code for me. I couldnt get the output to display properly, so I am reaching out to see if someone can help me figure out why PI or any of the other variables are not being displayed properly.

    I appreciate any help and explanation, as I am a beginner and do not fully understand data types.

    Thanks,
    John
    Here is my code:

    Code:
    /****************************************************************/
    /*        CS263 Programming in C                                    */
    /*        Assignment #2 Page 182                                    */
    /*        Written by: John Strange                                */
    /*        Date: 9/5/2012                                            */
    /*                                                                */
    /*                                                                */
    /*   Compute diameter in centimeters of a steel rod,            */
    /*   an aluminum rod, and a copper rod which can withstand      */
    /*   a particular compression load.                             */
    /*                                                                */
    /****************************************************************/
    /*                                                                */
    /*     Defined constants:                                            */
    /*                                                                */
    /*        steelStress    - 25000                                     */
    /*                                                                */
    /*        alumStress    - 15000                                     */
    /*                                                                */
    /*        coppStress    - 20000                                     */
    /*                                                                */
    /*     Input Variables:                                            */
    /*                                                                */
    /*        cLoad  - Compression Load                                */
    /*                                                                */
    /*     Computed Variables:                                        */
    /*                                                                */
    /*        area  - area                                            */
    /*                                                                */
    /*        diam  - diameter of rod                                    */
    /*                                                                */
    /****************************************************************/
    
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        const float PI = 3.14159;
        const int steelStress = 25000;
        const int alumStress = 15000;
        const int coppStress = 20000;
        double steelRad, alumRad, coppRad;
        double cLoad, steelArea, alumArea, coppArea;
        double steelDiam, alumDiam, coppDiam;
    
        printf("Input the compression load:");
        scanf("%d", &cLoad);
    
        if(cLoad > 0)
        {
            steelArea = cLoad / steelStress;
            alumArea = cLoad / alumStress;
            coppArea = cLoad / coppStress;
    
            steelRad = sqrt(steelArea / PI);
            alumRad = sqrt(alumArea / PI);
            coppRad = sqrt(coppArea /PI);
    
            steelDiam = steelRad * steelRad;
            alumDiam = alumRad * alumRad;
            coppDiam = coppRad * coppRad;
    
            printf("\n\nValue of PI: %d", PI);
            printf("\nCompression Load Value: %d", cLoad);
            printf("\nAluminum Stress Value: %d", alumStress);
            printf("\nSteel Stress Value: %d", steelStress);
            printf("\nCopper Stress Value: %d", coppStress);
            printf("\nAluminum Radius Value: %d", alumRad);
            printf("\nSteel Radius Value: %d", steelRad);
            printf("\nCopper Radius Value: %d", coppRad);
            printf("\nAluminum Area Value: %d", alumArea);
            printf("\nSteel Area Value: %d", steelArea);
            printf("\nCopper Area Value: %d", coppArea);
            printf("\nAluminum Diameter Value: %d", alumDiam);
            printf("\nSteel Diameter Value: %d", steelDiam);
            printf("\nCopper Diameter Value: %d", coppDiam);
    
            printf("\n\nType of material: Steel");
            printf("\nCompression load: %d", cLoad);
            printf("\nAllowable stress in lbs/m^2: %d", steelStress);
            printf("\nDiameter in centimeters: %d", steelDiam);
    
            printf("\n\nType of material: Aluminum");
            printf("\nCompression load: %d", cLoad);
            printf("\nAllowable stress in lbs/m^2: %d", alumStress);
            printf("\nDiameter in centimeters: %d", alumDiam);
    
            printf("\n\nType of material: Copper");
            printf("\nCompression load: %d", cLoad);
            printf("\nAllowable stress in lbs/m^2: %d", coppStress);
            printf("\nDiameter in centimeters: %d", coppDiam);
    
    
        }
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How exactly does it not work?

    By the way, I note that the scanf format specifier for double is not %d
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First thing I recommend is to increase your compiler warning level and then fix the warnings the compiler is issuing. Here are the warnings I received when I compiled your code.
    main.c||In function ‘main’:|
    main.c|48|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]|
    main.c|64|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|65|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|69|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|70|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|71|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|72|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|73|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|74|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|75|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|76|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|77|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|80|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|82|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|85|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|87|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|90|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    main.c|92|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]|
    ||=== Build finished: 0 errors, 18 warnings ===|
    You may want to study the documentation for scanf() and printf() to find the correct format specifiers.

    Jim

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    I have changed the format specifier to %f so there are no issues with float and I changed the data type to floats. The issue is that when I run it the math is not calculated properly. If i enter 2000 for cLoad and steelArea = (cLoad / steelStress) then steelArea should equal 0.08 and not the number listed in the attachment. Here is my updated code.

    Code:
    /****************************************************************/
    /*        CS263 Programming in C                                    */
    /*        Assignment #2 Page 182                                    */
    /*        Written by: John Strange                                */
    /*        Date: 9/5/2012                                            */
    /*                                                                */
    /*                                                                */
    /*   Compute diameter in centimeters of a steel rod,            */
    /*   an aluminum rod, and a copper rod which can withstand      */
    /*   a particular compression load.                             */
    /*                                                                */
    /****************************************************************/
    /*                                                                */
    /*     Defined constants:                                            */
    /*                                                                */
    /*        steelStress    - 25000                                     */
    /*                                                                */
    /*        alumStress    - 15000                                     */
    /*                                                                */
    /*        coppStress    - 20000                                     */
    /*                                                                */
    /*     Input Variables:                                            */
    /*                                                                */
    /*        cLoad  - Compression Load                                */
    /*                                                                */
    /*     Computed Variables:                                        */
    /*                                                                */
    /*        area  - area                                            */
    /*                                                                */
    /*        diam  - diameter of rod                                    */
    /*                                                                */
    /****************************************************************/
    
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        const float PI = 3.14159;
        const float steelStress = 25000;
        const float alumStress = 15000;
        const float coppStress = 20000;
        float steelRad, alumRad, coppRad;
        float cLoad;
        float steelArea, alumArea, coppArea;
        float steelDiam, alumDiam, coppDiam;
    
        printf("Input the compression load: ");
        scanf("%f", &cLoad);
    
        if(cLoad > 0)
        {
            steelArea = (cLoad / steelStress);
            alumArea = (cLoad / alumStress);
            coppArea = (cLoad / coppStress);
    
            steelRad = (sqrt(steelArea / PI));
            alumRad = (sqrt(alumArea / PI));
            coppRad = (sqrt(coppArea /PI));
    
            steelDiam = (steelRad * steelRad);
            alumDiam = (alumRad * alumRad);
            coppDiam = (coppRad * coppRad);
    
            printf("\n\nValue of PI: %f", test);
            printf("\nCompression Load Value: %f", cLoad);
            printf("\nAluminum Stress Value: %f", alumStress);
            printf("\nSteel Stress Value: %f", steelStress);
            printf("\nCopper Stress Value: %f", coppStress);
            printf("\nAluminum Radius Value: %f", alumRad);
            printf("\nSteel Radius Value: %f", steelRad);
            printf("\nCopper Radius Value: %f", coppRad);
            printf("\nAluminum Area Value: %f", alumArea);
            printf("\nSteel Area Value: %f", steelArea);
            printf("\nCopper Area Value: %f", coppArea);
            printf("\nAluminum Diameter Value: %f", alumDiam);
            printf("\nSteel Diameter Value: %f", steelDiam);
            printf("\nCopper Diameter Value: %f", coppDiam);
    
            printf("\n\nType of material: Steel");
            printf("\nCompression load: %f", cLoad);
            printf("\nAllowable stress in lbs/m^2: %f", steelStress);
            printf("\nDiameter in centimeters: %f", steelDiam);
    
            printf("\n\nType of material: Aluminum");
            printf("\nCompression load: %f", cLoad);
            printf("\nAllowable stress in lbs/m^2: %f", alumStress);
            printf("\nDiameter in centimeters: %f", alumDiam);
    
            printf("\n\nType of material: Copper");
            printf("\nCompression load: %f", cLoad);
            printf("\nAllowable stress in lbs/m^2: %f", coppStress);
            printf("\nDiameter in centimeters: %f", coppDiam);
    
    
        }
    
    }
    Also, when I output the value of the constant PI, it says 0. Any further help to understand my problems would be greatly appreciated! Here is the output when I run the program and enter 2000 for cLoad:

    Input the compression load:2000


    Value of PI: 0
    Compression Load Value: 2000
    Aluminum Stress Value: 15000
    Steel Stress Value: 25000
    Copper Stress Value: 20000
    Aluminum Radius Value: -972164833
    Steel Radius Value: -1686605182
    Copper Radius Value: -296046439
    Aluminum Area Value: 1181977184
    Steel Area Value: 559379162
    Copper Area Value: 886482888
    Aluminum Diameter Value: 951150351
    Steel Diameter Value: -288303248
    Copper Diameter Value: 713362763

    Type of material: Steel
    Compression load: 2000
    Allowable stress in lbs/m^2: 25000
    Diameter in centimeters: -288303248

    Type of material: Aluminum
    Compression load: 2000
    Allowable stress in lbs/m^2: 15000
    Diameter in centimeters: 951150351

    Type of material: Copper
    Compression load: 2000
    Allowable stress in lbs/m^2: 20000
    Diameter in centimeters: 713362763
    Process returned 35 (0x23) execution time : 12.935 s
    Press any key to continue.

    Thanks,
    John

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    After changing the following line:
    Code:
            printf("\n\nValue of PI: %f", test);
    (I replaced test with PI). I got a compile error that test was not defined.

    The program produced these results:
    Input the compression load: 2000


    Value of PI: 3.141590
    Compression Load Value: 2000.000000
    Aluminum Stress Value: 15000.000000
    Steel Stress Value: 25000.000000
    Copper Stress Value: 20000.000000
    Aluminum Radius Value: 0.206013
    Steel Radius Value: 0.159577
    Copper Radius Value: 0.178412
    Aluminum Area Value: 0.133333
    Steel Area Value: 0.080000
    Copper Area Value: 0.100000
    Aluminum Diameter Value: 0.042441
    Steel Diameter Value: 0.025465
    Copper Diameter Value: 0.031831

    Type of material: Steel
    Compression load: 2000.000000
    Allowable stress in lbs/m^2: 25000.000000
    Diameter in centimeters: 0.025465

    Type of material: Aluminum
    Compression load: 2000.000000
    Allowable stress in lbs/m^2: 15000.000000
    Diameter in centimeters: 0.042441

    Type of material: Copper
    Compression load: 2000.000000
    Allowable stress in lbs/m^2: 20000.000000
    Diameter in centimeters: 0.031831
    Jim

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Thanks Jim! For some reason it was just hosed. I created a new project and pasted my code...then it worked just fine. Thank you for testing!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  2. Data Structure issues
    By junglist in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 04:17 PM
  3. Data Alignment Issues...
    By aaba1 in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 11:03 AM
  4. Type definition issues?
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 1
    Last Post: 07-05-2010, 07:17 PM
  5. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM