thenewboston

C Programming Tutorial - 39 - int and float Arrays

Aug 22nd, 2014
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. int main()
  8. {
  9.     int i;
  10.  
  11.     //initialize an integer/float array by putting elements in curly brackets
  12.     //no null zero at end, that is only for character arrays
  13.     int meatBalls[5];
  14.     int totalBalls = 0;
  15.  
  16.     //get user input
  17.     for(i=0; i<5; i++){
  18.         printf("How many meatballs did you eat on day %d \n", i+1);
  19.         scanf(" %d", &meatBalls[i]);
  20.     }
  21.  
  22.     //calculate total amount you ate
  23.     for(i=0; i<5; i++){
  24.         totalBalls += meatBalls[i];
  25.     }
  26.  
  27.     int avg = totalBalls / 5;
  28.     printf("You ate %d total meatballs, thats an average of %d per day!", totalBalls, avg);
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment