fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. // declare const variable for array size, and array prototypes
  8. const int SIZE = 20;
  9. void fill(int arr[SIZE]);
  10. void print(int arr[SIZE]);
  11. void printRanges(int arr[SIZE]);
  12.  
  13. int main() {
  14.  
  15. // create an array with 20 components
  16. int arr[SIZE] = { 0 };
  17.  
  18. // call functions
  19. fill(arr);
  20. print(arr);
  21. printRanges(arr);
  22.  
  23.  
  24.  
  25. // pause and exit
  26. getchar();
  27. getchar();
  28. return 0;
  29. }
  30.  
  31. //fills array arr with 20 random numbers
  32. void fill(int arr[SIZE]) {
  33. for (int i = 0; i < SIZE; i++) {
  34. arr[i] = rand() % 100;
  35. }
  36. }
  37.  
  38. // prints the array
  39. void print(int arr[SIZE]) {
  40. for (int i = 0; i < SIZE; i++) {
  41. cout << arr[i] << " ";
  42. }
  43. }
  44.  
  45. // finds the range of each value in the array and stores it in the array ticker, then prints
  46. // a list from 00-90 documenting how many values are in each range
  47. void printRanges(int arr[SIZE]) {
  48. int ticker[10] = { 0 };
  49.  
  50. for (int i = 0; i < SIZE; i++) {
  51. int index = arr[i] / 10;
  52. if (index < 10) {
  53. ticker[index] += 1;
  54. }
  55. }
  56. cout << endl;
  57. for (int i = 0; i < 10; i++) {
  58. cout << setfill('0') << setw(2) << i << ": ";
  59. cout << setfill('*') << setw(ticker[i]) << "" << endl;
  60. }
  61. }
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
83 86 77 15 93 35 86 92 49 21 62 27 90 59 63 26 40 26 72 36 
00: 
01: *
02: ****
03: **
04: **
05: *
06: **
07: **
08: ***
09: ***