daily pastebin goal
5%
SHARE
TWEET

Untitled

a guest Dec 14th, 2018 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char * argv[]) {
  4.     float accumulator = 0.0;
  5.     int num_of_times = 0;
  6.     int ii = 1;
  7.     float average = 0.0;
  8.    
  9.     // Ask until we have a valid input
  10.     while (1) {
  11.         // Get the user input    
  12.         printf("How many?: ");
  13.         scanf("%d", &num_of_times);
  14.         printf("\n");
  15.  
  16.         // If number of times to run is less than 0,
  17.         // tell them, and try again        
  18.         if (num_of_times <= 0) {
  19.             printf("Please enter a number greater than 0.\n\n");
  20.         } else{
  21.             // We have a valid number now, break out of this loop
  22.             break;
  23.         }
  24.     }
  25.  
  26.     // Now that we have the number of times to ask
  27.     // the user for a number, do it    
  28.     for (ii = 0; ii < num_of_times; ii++) {
  29.         float user_input = 0.0;
  30.        
  31.         // Get the user input to add to
  32.         // the accumulator
  33.         printf("User Number: ");
  34.         scanf("%f", &user_input);
  35.        
  36.         // Add the input
  37.         accumulator += user_input;
  38.     }
  39.  
  40.     // Get the average
  41.     average = accumulator / (float)num_of_times;
  42.    
  43.     // Display it to the user
  44.     printf("\nAverage = %f\n", average);
  45.    
  46.     return 0;
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top