Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void part_1(){
- int i=0, j, k, x, y, z, l, n;
- double x_arr[25], x_high, x_low, x_second_arr[25], norm_x_arr[25], max = 30.0, min = 0.0;
- printf("\nPART ONE:\n\n");
- FILE *in;
- in = fopen("data.txt", "r");
- while(i != 25){ /*Scan file and add to array*/
- fscanf(in, "%lf, ", &x_arr[i]);
- i++;
- }
- for(j = 0; j<25; j++) /*Print array from file*/
- printf("%.lf ", x_arr[j]);
- x_high = x_arr[0]; /*Calculate the highest value and print*/
- for(k = 1; k<25; k++){
- if(x_arr[k] > x_high)
- x_high = x_arr[k];
- }
- printf("\nHighest: %.lf", x_high);
- x_low = x_arr[0]; /*Calculate the lowest value and print*/
- for(x = 1; x<25; x++){
- if(x_arr[x] < x_low)
- x_low = x_arr[x];
- }
- printf("\nLowest: %.lf\n", x_low);
- for(y = 0; y<25; y++) /*Multiply the original array by 3 and print both*/
- x_second_arr[y] = x_arr[y]*3;
- printf("\nOriginal Array: ");
- for(z = 0; z<25; z++)
- printf("%.lf ", x_arr[z]);
- printf("\nSecond Array (x3): ");
- for(l = 0; l<25; l++)
- printf("%.lf ", x_second_arr[l]);
- printf("\n\nNormalized Array (Between 0 and 30): "); /*Normalize the original array between 0 and 30*/
- for(n = 0; n<25; n++){
- norm_x_arr[n] = min + ((x_arr[n] - x_low) * (max - min))/(x_high - x_low);
- printf("%.lf ", norm_x_arr[n]);
- }
- printf("%.lf ", norm_x_arr);
- printf("\n");
- fclose(in);
- }
- void part_2(){
- int monthly_water_arr[50], num_months = 0, j, k, count_arr[7], i, z, x;
- printf("\nPART TWO:\n");
- FILE *in;
- in = fopen("water.txt", "r");
- while(fscanf(in, "%d, ", &monthly_water_arr[num_months]) != EOF) /*Scan file and add to array until EOF*/
- num_months++;
- printf("\nMonthly Water Provided: "); /*Print Array*/
- for(j = 0; j < num_months; j++)
- printf("%d ", monthly_water_arr[j]);
- for(i = 0; i < 7; i++) /*Zero all elements in count_arr*/
- count_arr[i] = 0;
- for(k = 0; k < num_months; k++){ /*Find out how many samples fall into select ranges and add to the count array*/
- if(monthly_water_arr[k] >= 71 && monthly_water_arr[k] <= 80)
- count_arr[0]++;
- else if(monthly_water_arr[k] >= 81 && monthly_water_arr[k] <= 90)
- count_arr[1]++;
- else if(monthly_water_arr[k] >= 91 && monthly_water_arr[k] <= 100)
- count_arr[2]++;
- else if(monthly_water_arr[k] >= 101 && monthly_water_arr[k] <= 110)
- count_arr[3]++;
- else if(monthly_water_arr[k] >= 111 && monthly_water_arr[k] <= 120)
- count_arr[4]++;
- else if(monthly_water_arr[k] >= 121 && monthly_water_arr[k] <= 130)
- count_arr[5]++;
- else if(monthly_water_arr[k] >= 131 && monthly_water_arr[k] <= 140)
- count_arr[6]++;
- }
- printf("\n\n10 M Gals Water Per Day\n"); /*Print out all the ranges and the counts*/
- for(z = 0; z < 7; z++)
- printf("%2d - %2d \t %d\n", 71+(z*10), 80+(z*10), count_arr[z]);
- printf("\n\n10 M Gals Water Per Day (Only Non-Zero Lines)\n"); /*Challenge: Same as above but only including non-zero lines*/
- for(z = 0; z < 7; z++){
- if(count_arr[z] != 0)
- printf("%2d - %2d \t %d\n", 71+(z*10), 80+(z*10), count_arr[z]);
- }
- }
- int main() /*Main Function*/
- {
- part_1();
- part_2();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement