Moortiii icon

hand-in_1_1

Moortiii | PRO | 08/25/17 12:30:08 PM UTC | 0 ⭐ | 431 👁️ | Never ⏰ | []
C |

1.34 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
 
/* https://stackoverflow.com/questions/19923889/how-to-make-the-program-loop-until-user-input-specific-number
 * Source used for finding out how to ask a user for input in C and scan for a specific value, I miss Python already */
 
int main() {
    int count = -1;
    /* Using doubles for precision and to make sure the user can enter a decimal number */
    float value;
    float sum = 0;
    float average = 0;
 
    /* Ask for a value and keep asking until the user enters 0 */
    do {
        printf("Enter a number. Type '0' to exit.\n\n");
 
        /* There really should be some test here to make sure what the user enters is actually a number */
 
        /* We have to pass a reference to the variable value for this to work, read up on how
         * something can be referenced by address without being a pointer in the first place */
        scanf("%g", &value);
        sum += value;
        count++;
 
    } while(value != 0); /* Keep looping until the user enters '0'; */
 
    average = sum/count;
 
    /* Check if Bamboo actually wants to include the number 0 as a "number" in the count */
    /* Finally once the user is done entering numbers we can do our calculations */
    printf("You entered: %i numbers\n", count);
    printf("Sum: %g\n", sum);
    printf("Average: %g\n", average);
    return 0;
}

Comments