Moortiii

hand-in_1_1

Aug 25th, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* https://stackoverflow.com/questions/19923889/how-to-make-the-program-loop-until-user-input-specific-number
  4.  * Source used for finding out how to ask a user for input in C and scan for a specific value, I miss Python already */
  5.  
  6. int main() {
  7.     int count = -1;
  8.     /* Using doubles for precision and to make sure the user can enter a decimal number */
  9.     float value;
  10.     float sum = 0;
  11.     float average = 0;
  12.  
  13.     /* Ask for a value and keep asking until the user enters 0 */
  14.     do {
  15.         printf("Enter a number. Type '0' to exit.\n\n");
  16.  
  17.         /* There really should be some test here to make sure what the user enters is actually a number */
  18.  
  19.         /* We have to pass a reference to the variable value for this to work, read up on how
  20.          * something can be referenced by address without being a pointer in the first place */
  21.         scanf("%g", &value);
  22.         sum += value;
  23.         count++;
  24.  
  25.     } while(value != 0); /* Keep looping until the user enters '0'; */
  26.  
  27.     average = sum/count;
  28.  
  29.     /* Check if Bamboo actually wants to include the number 0 as a "number" in the count */
  30.     /* Finally once the user is done entering numbers we can do our calculations */
  31.     printf("You entered: %i numbers\n", count);
  32.     printf("Sum: %g\n", sum);
  33.     printf("Average: %g\n", average);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment