Advertisement
ssoni

scores array

Jan 14th, 2022
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3.  
  4. void chart(int count, int scores[]);
  5.  
  6. int main(void)
  7. {
  8.     int n=0;
  9.     n = get_int("How many numbers? ");
  10.  
  11.     int scores[n];
  12.  
  13.     //get scores
  14.     for (int i=0; i<n; i++)
  15.     {
  16.         scores[i] = get_int("Enter score #%i: ",i+1);
  17.     }
  18.  
  19.     int total=0;
  20.  
  21.     //total the scores
  22.     for (int i=0; i<n; i++)
  23.     {
  24.         total = total + scores[i];
  25.     }
  26.  
  27.     //calc average
  28.     float avg=0;
  29.     avg = (float)total / n;
  30.     printf("Avg = %f\n",avg);
  31.  
  32.     chart(n, scores);
  33.  
  34. }
  35.  
  36. void chart(int count, int scores[])
  37. {
  38.    //Loop thru each test score
  39.    for (int i=0; i<count; i++)
  40.    {
  41.        printf("%i: ", scores[i]);
  42.        for (int j=0; j<scores[i]; j++)
  43.        {
  44.            printf("*");
  45.        }
  46.        printf("\n");
  47.    }
  48. }
  49.  
  50.  
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement