Thread: New to C Programming - College Course (first assignment)

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    23

    New to C Programming - College Course (first assignment)

    Hi all,

    I'm new to programming and so naturally am new to the C programming language. I'm taking a college course (Intro to C Programming) and am working through my first assignment. I thought I had a good handle on what was being taught and feel for the most part that that is true. However, in this assignment I've clearly misunderstood something fundamental in regard to if statements.

    Here are the instructions from my professor:
    "Write a program that asks the user to enter an object's weight in pounds and to select the name of a planet from a menu. The program then outputs how much the object would weigh on that planet."

    Here's my code so far:

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        float objectWeight; //declare the variable for the object's weight
        int planet; //declare the variable for the planet number
    
    
        printf("Enter an object's weight in pounds.\n");    
        scanf_s("%f", &objectWeight); //read the value from the end user
        
    //Print the below menu to the screen
        printf("\n"); 
        printf("Please choose from the following menu one of the bodies found in our solar\n");
        printf("system by entering it's corresponding number (using 1 through 10).\n");
        printf("1. Mercury\n");
        printf("2. Venus\n");
        printf("3. Earth\n");
        printf("4. Earth's Moon\n");
        printf("5. Mars\n");
        printf("6. Jupiter\n");
        printf("7. Saturn\n");
        printf("8. Uranus\n");
        printf("9. Neptune\n");
        printf("10. Pluto\n\n");    
        
        scanf_s("%d", &planet); //read the planet number from the user
        printf("\n"); 
    
    
        //Start of the if statements that will assign the multiplier for the objectWeight based on the planet selected
        if (planet = 1) {
            printf("The weight of this object on the planet Mercury is %f.\n", objectWeight * 0.4155);
                }
        if (planet = 2) {
            printf("The weight of this object on the planet Venus is %f.\n", objectWeight * 0.8975);
        }
        if (planet = 3) {
            printf("The weight of this object on the planet Earth is %f.\n", objectWeight * 1.0);
        }
        if (planet = 4) {
            printf("The weight of this object on the the Earth's moon is %f.\n", objectWeight * 0.166);
        }
        if (planet = 5) {
            printf("The weight of this object on the planet Mars is %f.\n", objectWeight * 0.3507);
        }
        if (planet = 6) {
            printf("The weight of this object on the planet Jupiter is %f.\n", objectWeight * 2.5374);
        }
        if (planet = 7) {
            printf("The weight of this object on the planet Saturn is %f.\n", objectWeight * 1.0677);
        }
        if (planet = 8) {
            printf("The weight of this object on the planet Uranus is %f.\n", objectWeight * 0.8947);
        }
        if (planet = 9) {
            printf("The weight of this object on the planet Neptune is %f.\n", objectWeight * 1.1794);
        }
        if (planet = 10) {
            printf("The weight of this object on the planet Pluto is %f.\n", objectWeight * 0.0899);
            }
        
        return 0;
    }
    For example, if the user enters a weight of 100 pounds, and then selects the planet Saturn, the program should output a weight of 106.77 pounds (100 * 1.0677).

    What I was hoping to see is a single line returned that showed the printf statement that corresponded to the number of the planet and then the new object weight. Instead I'm getting all 10 lines returned that have performed the right calculation but I was expecting to see just 1 line so I'm not sure where I've gone wrong.

    Is the error in my if statement/s?

    Here's what the program looks like when it executes.
    New to C Programming - College Course (first assignment)-planetweight_c_program-png
    Last edited by PhantomJoe; 09-17-2017 at 03:28 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    Do you realize that there is a difference between the comparison operator== and the assignment operator=? You appear to be using the assignment operator in places where the comparison operator would be more appropriate, assignments always evaluate to true.

    Also you may want to consider using a switch statement.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    23
    Quote Originally Posted by jimblumberg View Post
    Do you realize that there is a difference between the comparison operator== and the assignment operator=? You appear to be using the assignment operator in places where the comparison operator would be more appropriate, assignments always evaluate to true.

    Also you may want to consider using a switch statement.

    Jim
    This was exactly what I was overlooking. When I replace all the assignment operators with comparison operators the program returns just the one line that corresponds to the entry from the end user. Thank you very much for you help.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I'd write it like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        float objectWeight; //declare the variable for the object's weight
        int planet; //declare the variable for the planet number
    
    
        printf("Enter an object's weight in pounds.\n");
        //scanf_s("%f", &objectWeight); //read the value from the end user
        scanf("%f", &objectWeight); //read the value from the end user
    
    //Print the below menu to the screen
        printf("\n"
        "Please choose from the following menu one of the bodies found in our solar\n"
        "system by entering it's corresponding number (using 1 through 10).\n"
        "1. Mercury\n"
        "2. Venus\n"
        "3. Earth\n"
        "4. Earth's Moon\n"
        "5. Mars\n"
        "6. Jupiter\n"
        "7. Saturn\n"
        "8. Uranus\n"
        "9. Neptune\n"
        "10. Pluto\n\n");
    
       // scanf_s("%d", &planet); //read the planet number from the user
        scanf("%d", &planet); //read the planet number from the user
        printf("\n");
    
    
    
    
    switch(planet)
    {
    
        case 1:
            printf("The weight of this object on the planet Mercury is %f.\n", objectWeight * 0.4155);
            break;
        case 2:
            printf("The weight of this object on the planet Venus is %f.\n", objectWeight * 0.8975);
            break;
        case 3:
            printf("The weight of this object on the planet Earth is %f.\n", objectWeight * 1.0);
            break;
       case 4:
            printf("The weight of this object on the the Earth's moon is %f.\n", objectWeight * 0.166);
            break;
       case 5:
            printf("The weight of this object on the planet Mars is %f.\n", objectWeight * 0.3507);
            break;
       case 6:
            printf("The weight of this object on the planet Jupiter is %f.\n", objectWeight * 2.5374);
            break;
       case 7:
            printf("The weight of this object on the planet Saturn is %f.\n", objectWeight * 1.0677);
            break;
       case 8:
            printf("The weight of this object on the planet Uranus is %f.\n", objectWeight * 0.8947);
            break;
        case 9:
            printf("The weight of this object on the planet Neptune is %f.\n", objectWeight * 1.1794);
            break;
        case 10:
            printf("The weight of this object on the planet Pluto is %f.\n", objectWeight * 0.0899);
            break;
       default:
            break;
       }
    
    
       return 0;
       }
    I used scanf instead of scanf_s to comply with Linux C gcc my system, and limited knowledge of what I am doing.
    out put is this:
    Code:
    userx@~/bin <> ./a.out
    Enter an object's weight in pounds.
    10
    
    Please choose from the following menu one of the bodies found in our solar
    system by entering it's corresponding number (using 1 through 10).
    1. Mercury
    2. Venus
    3. Earth
    4. Earth's Moon
    5. Mars
    6. Jupiter
    7. Saturn
    8. Uranus
    9. Neptune
    10. Pluto
    
    2
    
    The weight of this object on the planet Venus is 8.975000.
    userx@~/bin <>
    For your if statements along with that = vs == you might want to try ' if else if else block '

    if you by chance use that, you can just tell him if he asks how do you know how that works?

    just maybe say something like, it works off an int value. gezzzz
    Last edited by userxbw; 09-22-2017 at 11:47 AM.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Message to PhantomJoe: Don't use other people's code unless you understand how it works, professors can easily spot the cheaters by asking some questions about the program they supposedly wrote.

    That said, as long as we propose improvements, I would go for an array to hold all the different planet values, and index it with the value the user gives. I'm not going for efficiency, I genuinely think this implementation is easier to read and to be understood, as well as easily modifiable:
    Code:
    #include <stdio.h>
    
    int main()
    {
        float objectWeight; //declare the variable for the object's weight
        int planet; //declare the variable for the planet number
        int i;
    
        const char* planetNames[] = {
            "Mercury", "Venus", "Earth", "Earth's Moon", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"
        };
        const double planetRatios[] = {
            0.4155, 0.8975, 1.0, 0.166, 0.3507,
            2.5374, 1.0677, 0.8947, 1.1794, 0.0899
        };
    
    
        printf("Enter an object's weight in pounds.\n");
        scanf("%f", &objectWeight); //read the value from the end user
    
        //Print the below menu to the screen
        printf("\n"
               "Please choose from the following menu one of the bodies found in our solar\n"
               "system by entering it's corresponding number.\n");
    
        // Print all available planets
        for (i = 0; i < sizeof(planetRatios)/sizeof(planetRatios[0]); ++i) {
            printf("%d. %s\n", i+1, planetNames[i]);
        }
        putchar('\n');
    
        scanf("%d", &planet); //read the planet number from the user
        printf("\n");
    
        if (planet > 0 && planet <= sizeof(planetRatios)/sizeof(planetRatios[0])) {
            printf("The weight of this object on %s is %f.\n", planetNames[planet-1], objectWeight * planetRatios[planet-1]);
        }
    
        return 0;
    }
    Last edited by GReaper; 09-22-2017 at 12:36 PM.
    Devoted my life to programming...

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    Message to PhantomJoe: Don't use other people's code unless you understand how it works, professors can easily spot the cheaters by asking some questions about the program they supposedly wrote.

    That said, as long as we propose improvements, I would go for an array to hold all the different planet values, and index it with the value the user gives. I'm not going for efficiency, I genuinely think this implementation is easier to read and to be understood, as well as easily modifiable:
    Code:
    #include <stdio.h>
    
    int main()
    {
        float objectWeight; //declare the variable for the object's weight
        int planet; //declare the variable for the planet number
        int i;
    
        const char* planetNames[] = {
            "Mercury", "Venus", "Earth", "Earth's Moon", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"
        };
        const double planetRatios[] = {
            0.4155, 0.8975, 1.0, 0.166, 0.3507,
            2.5374, 1.0677, 0.8947, 1.1794, 0.0899
        };
    
    
        printf("Enter an object's weight in pounds.\n");
        scanf("%f", &objectWeight); //read the value from the end user
    
        //Print the below menu to the screen
        printf("\n"
               "Please choose from the following menu one of the bodies found in our solar\n"
               "system by entering it's corresponding number.\n");
    
        // Print all available planets
        for (i = 0; i < sizeof(planetRatios)/sizeof(planetRatios[0]); ++i) {
            printf("%d. %s\n", i+1 , planetNames[i]);
        }
        putchar('\n');
    
        scanf("%d", &planet); //read the planet number from the user
        printf("\n");
    
        if (planet > 0 && planet <= sizeof(planetRatios)/sizeof(planetRatios[0])) {
            printf("The weight of this object on %s is %f.\n", planetNames[planet-1], objectWeight * planetRatios[planet-1]);
        }
    
        return 0;
    }
    question before even running this, why dived the two if you're just printing out that first array size 10 - zero based. would not this also work?
    Code:
     for (i = 1; i < sizeof(planetNames); ++i) {  
     printf("%d. %s\n", i , planetNames[i]);
    
        }
    that would run it up 0 thru 9 = 10 then stop right? or do they both have to be used in tandem to get the names out of the planetNames array?
    because i+1 is printing the numbers, and then the array names.

    just wondering.

    and yeah OP read up on switch first if you use that one

    mod:
    don't fully understand it yet, but this is what I got after blowing it up.
    Code:
            printf ( "  sizeof(planetRatios)/sizeof(planetRatios[0]) %lu \n",  (sizeof(planetRatios)/sizeof(planetRatios[0]) )  );
            sizeof(planetRatios)/sizeof(planetRatios[0]) 10
    
            printf("sizeof(planetRatios) %lu \n",  (sizeof(planetRatios) ) );
            sizeof(planetRatios) 80 
    
            printf ( " sizeof(planetRatios[0]) %lu \n", sizeof(planetRatios[0])  );
            sizeof(planetRatios[0]) 8
    
            printf("sizeof(planetNames) %lu \n", sizeof(planetNames ) );
             sizeof(planetNames) 80
    80 / 8 = 10

    where would I read up on that? as adding one more results with the array[0] = 8 and that array = 88

    never mind:
    To determine the size of your array in bytes, you can use the sizeof
    operator: int a[17]; int n = sizeof(a); On my computer, ints are 4 bytes
    long, so n is 68. To determine the number of elements in the array, we
    can divide the total size of the array by the size of the array
    element.
    Last edited by userxbw; 09-22-2017 at 02:56 PM.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by userxbw View Post
    question before even running this, why dived the two if you're just printing out that first array size 10 - zero based.
    By coding it like this you only need to add, change or remove elements from planetNames and planetRatios, leaving everything else exactly the same.

    Quote Originally Posted by userxbw View Post
    would not this also work?
    That wouldn't work because sizeof returns the size in bytes, not the amount of elements. That is why I divide by the size of its first element.
    Devoted my life to programming...

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    By coding it like this you only need to add, change or remove elements from planetNames and planetRatios, leaving everything else exactly the same.


    That wouldn't work because sizeof returns the size in bytes, not the amount of elements. That is why I divide by the size of its first element.
    yes that part I know they both are working together, that sizeof I didn't have a full understanding of, I never read up on it, until just now, due to what you did. I just thought it returned the sizeof being the number of elements used, not bytes as I posted in a mod, and you stated.

    I understand the rest of it, that one ( sizeof ) caught me, now I know more! thanks for sharing that code.

    that saves a lot of typing.
    Last edited by userxbw; 09-22-2017 at 03:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] With College Assignment
    By Joshua Ray in forum C Programming
    Replies: 16
    Last Post: 11-20-2015, 02:52 PM
  2. HELP!! College Assignment
    By college in forum C Programming
    Replies: 8
    Last Post: 03-22-2011, 06:53 AM
  3. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  4. College programming course decision
    By Dino in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-19-2008, 01:32 PM
  5. C programming college course
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 03-22-2002, 01:55 PM

Tags for this Thread