Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void part_1(){
  4. int i=0, j, k, x, y, z, l, n;
  5. double x_arr[25], x_high, x_low, x_second_arr[25], norm_x_arr[25], max = 30.0, min = 0.0;
  6. printf("\nPART ONE:\n\n");
  7.  
  8. FILE *in;
  9. in = fopen("data.txt", "r");
  10.  
  11. while(i != 25){ /*Scan file and add to array*/
  12. fscanf(in, "%lf, ", &x_arr[i]);
  13. i++;
  14. }
  15. for(j = 0; j<25; j++) /*Print array from file*/
  16. printf("%.lf ", x_arr[j]);
  17.  
  18. x_high = x_arr[0]; /*Calculate the highest value and print*/
  19. for(k = 1; k<25; k++){
  20. if(x_arr[k] > x_high)
  21. x_high = x_arr[k];
  22. }
  23. printf("\nHighest: %.lf", x_high);
  24.  
  25. x_low = x_arr[0]; /*Calculate the lowest value and print*/
  26. for(x = 1; x<25; x++){
  27. if(x_arr[x] < x_low)
  28. x_low = x_arr[x];
  29. }
  30. printf("\nLowest: %.lf\n", x_low);
  31.  
  32. for(y = 0; y<25; y++) /*Multiply the original array by 3 and print both*/
  33. x_second_arr[y] = x_arr[y]*3;
  34.  
  35. printf("\nOriginal Array: ");
  36. for(z = 0; z<25; z++)
  37. printf("%.lf ", x_arr[z]);
  38.  
  39. printf("\nSecond Array (x3): ");
  40. for(l = 0; l<25; l++)
  41. printf("%.lf ", x_second_arr[l]);
  42.  
  43. printf("\n\nNormalized Array (Between 0 and 30): "); /*Normalize the original array between 0 and 30*/
  44. for(n = 0; n<25; n++){
  45. norm_x_arr[n] = min + ((x_arr[n] - x_low) * (max - min))/(x_high - x_low);
  46. printf("%.lf ", norm_x_arr[n]);
  47. }
  48. printf("%.lf ", norm_x_arr);
  49. printf("\n");
  50. fclose(in);
  51. }
  52. void part_2(){
  53. int monthly_water_arr[50], num_months = 0, j, k, count_arr[7], i, z, x;
  54. printf("\nPART TWO:\n");
  55. FILE *in;
  56. in = fopen("water.txt", "r");
  57.  
  58. while(fscanf(in, "%d, ", &monthly_water_arr[num_months]) != EOF) /*Scan file and add to array until EOF*/
  59. num_months++;
  60. printf("\nMonthly Water Provided: "); /*Print Array*/
  61. for(j = 0; j < num_months; j++)
  62. printf("%d ", monthly_water_arr[j]);
  63.  
  64. for(i = 0; i < 7; i++) /*Zero all elements in count_arr*/
  65. count_arr[i] = 0;
  66. for(k = 0; k < num_months; k++){ /*Find out how many samples fall into select ranges and add to the count array*/
  67. if(monthly_water_arr[k] >= 71 && monthly_water_arr[k] <= 80)
  68. count_arr[0]++;
  69. else if(monthly_water_arr[k] >= 81 && monthly_water_arr[k] <= 90)
  70. count_arr[1]++;
  71. else if(monthly_water_arr[k] >= 91 && monthly_water_arr[k] <= 100)
  72. count_arr[2]++;
  73. else if(monthly_water_arr[k] >= 101 && monthly_water_arr[k] <= 110)
  74. count_arr[3]++;
  75. else if(monthly_water_arr[k] >= 111 && monthly_water_arr[k] <= 120)
  76. count_arr[4]++;
  77. else if(monthly_water_arr[k] >= 121 && monthly_water_arr[k] <= 130)
  78. count_arr[5]++;
  79. else if(monthly_water_arr[k] >= 131 && monthly_water_arr[k] <= 140)
  80. count_arr[6]++;
  81. }
  82. printf("\n\n10 M Gals Water Per Day\n"); /*Print out all the ranges and the counts*/
  83. for(z = 0; z < 7; z++)
  84. printf("%2d - %2d \t %d\n", 71+(z*10), 80+(z*10), count_arr[z]);
  85.  
  86. printf("\n\n10 M Gals Water Per Day (Only Non-Zero Lines)\n"); /*Challenge: Same as above but only including non-zero lines*/
  87. for(z = 0; z < 7; z++){
  88. if(count_arr[z] != 0)
  89. printf("%2d - %2d \t %d\n", 71+(z*10), 80+(z*10), count_arr[z]);
  90. }
  91. }
  92. int main() /*Main Function*/
  93. {
  94. part_1();
  95. part_2();
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement