Question

Programming Question

Exercise #2:

  1. Create a data file named water.dat with the following data: 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111. Each number represents the number of millions of gallons of water provided to a major city over the period of one month. The data runs for quite a number of months. You will write a loop to read all the data into an array of length 50 named monthly_water_arr. Your code should count the number of data elements input, putting that value into the variable num_months and after the entire set of data has been entered, write another loop controlled by num_months to print out the values. Provide an appropriate label so you can recognize the output.

  2. Extend your code by writing a loop which will go through the data in monthly_water_arr and use an if statement in the loop to print out any values between 71 and 80, inclusive.

  3. Modify your code by removing the printing from this latest loop, replacing it by code to count into a variable named count_0 how many values, between 71 and 80, inclusive, would have been printed. When the loop terminates,it will print out the variable count_0. (You will shortly see why "0" is used for this name.) Try this with the data.

  4. Further modify the loop by summing into another variable count_1, the number of values between 81 and 90 and after the loop terminates, print it out along with count_0. (There are none for this data so you should see 0 being printed for the second count.)

  5. Next, instead of individual summing elements count_0 and count_1, create an array named count_arr which is 7 elements in length. Before summing any of the count elements in the array, use a for loop to ensure that you zero all these elements. Further modify your loop running over the array monthly_water_arr to count the elements with values between 71 and 80 into the first count_arr element (count_arr[0]), and to count the elements between 81 and 90 into the second count_arr element (count_arr[1]), and so on. Remember, a counting loop is just a sum loop which adds one (or uses ++) each time a count is to be added in. Note from inspecting the data you can see that there are 7 "bins", grouped in 10's of millions of gallons of water per bin, into which the water usage amount can fall: 71-80 , 81-90, ... , 131-140 so 7 bins will suffice for count_arr. When the loop doing the counting is finished, use a for loop to print out the 7 values in count_arr, each on a separate line. These values are the frequency in months that showed this amount of water usage per month over the observation period, in groups of 10 million gals per day. Label each number with the millions of gallons range it covers. For the given data this would look like

  6. 10 M gals water per day
    71-80
    81-90
    91-100
    101-110
    111-120
    121-130
    131-140

  7. To produce the output with the labelling column on the left is not terribly trivial, as you will have to change the label for each iteration of the printing for loop. You can do this with a series of nested if statements or you can use variables to produce the labelling numbers, changing the values in those variables with each for loop iteration.

  8. As an additional challenge in the original form of this lab, see if you can produce the same result but this time only print out non-zero lines, as in

  9. 10 M gals water per day
    71-80
    91-100
    101-110
    111-120
    121-130
    131-140


0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <limits.h>


void tenfold(int *a1, int *a2,int size); /*HOMEWORK*/


int main(void) {

   /*Part 1*/

   double x_arr[10],xhigh = LONG_MIN, temp, xlow = LONG_MAX,x_second_arr[10],max,min,norm_x_arr[10] ; /*Smallest 32-bit number.*/

   FILE * i = fopen("TestData.txt","r");

   unsigned int count = 0;

   if (i == NULL) {

      fprintf(stderr,"Could not find file\n");

      return(1);

   }

   

   while ( !feof(i) ) { //This is another way of testing end of file.

      fscanf(i,"%lf",&x_arr[count]);

      count++;

   }

   

   puts("number | 3*number"); //Formatting columns.

   for (count = 0; count < 10; ++count) {

      temp = *(x_arr+count); //Another way to get the element from the array.

      if (xhigh < temp) {

         xhigh = temp;

      }

      if (xlow > temp) {

         xlow = temp;

      }

      x_second_arr[count] = *(x_arr + count) * 3;

      printf("%6.5lf %6.5lf\n",temp, x_second_arr[count]);

   }

   printf("The highest number in the array is %.3lf. The lowest is %.4lf\n",xhigh,xlow);

   

   puts("Now enter the new max and min you want your dataset to be normalized to: ");

   scanf("%lf %lf",&max,&min);

   

   puts("Your normal numbers are: ");

   for (count = 0; count < 10; count++) {

      *(norm_x_arr + count) = min + ((x_arr[count] - xlow) * (max-min))/(xhigh-xlow);

      printf("%.4lf\n",norm_x_arr[count]);

   }

   puts("\n");

   fclose(i);

   

   /*Part 2*/

   int monthly_water_arr[50],num_months = 0,c,temp2, small = 61;

   static int count_arr[7];

   

   FILE *j = fopen("water.dat","r");

   if (j == NULL) {

      fprintf(stderr,"Could not find file\n");

      return(1);

   }

   while (!feof(j)) {

      fscanf(j,"%d",&monthly_water_arr[num_months]);

      num_months++;

   }

   //puts("\"__\" indicates values between 71 and 80, inclusive.");

   for (c = 0; c < num_months; c++) {

      temp2 = monthly_water_arr[c];

      printf("%d\n",temp2);

      

      if (temp2 >= 71 && temp2 <= 80) {

         count_arr[0]++;

         //printf("__\n");

      }

      else if (temp2 <= 90) {

         count_arr[1]++;

      }

      else if (temp2 <= 100) {

         count_arr[2]++;

      }

      else if (temp2 <= 110) {

         count_arr[3]++;

      }

      else if (temp2 <= 120) {

         count_arr[4]++;

      }

      else if (temp2 <= 130) {

         count_arr[5]++;

      }

      else if (temp2 <= 140) {

         count_arr[6]++;

      }

      /*

      else {

      puts("");

      }*/

   }

   puts("\n10 M gals water per day: ");

   for (c = 0; c < 7; c++) {

      small += 10;

      if (count_arr[c] == 0) {

         continue;

      }

      printf("%d-%d: ",small,small+9);

      printf("%d\n", count_arr[c]);

   }

   fclose(j);

   puts("\n");

   

   /*HOMEWORK*/

   int array1[9] = {3,4,5,6,7,-8,-9,1,2}, array2[9];

   int size = 9;

   

   tenfold(array1,array2,size);

   

   return(0);

}


void tenfold(int *a1, int *a2,int size) {/*HOMEWORK*/

   int i,temp;

   for (i = 0; i < size; i++) {

      if (a1[i] > 0) {

         temp = *(a1+i) * 10;

      }

      else {

         temp = *(a1+i);

      }

      a2[i] = temp;

      printf("%d ",a2[i]);

   }

   printf("\n");

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Programming Question
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10...

    Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10 M gals water per day 71-80 91-100 101-110 111-120 121-130 131-140 Create a data file named water.dat with the following data: 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111 123 134 122 128 111 110 98 99 78 98 100 120 122 110 111. Each number represents the number of millions of gallons of water provided to...

  • Programming Question

    Exercise #1:Write a C program that contains the following steps. Read carefully each step as they are not only programming steps but also learning topics that explain how numeric arrays in C work.Write a while loop to input a series of numbers (either from a file or from the keyboard, but using a file will make for easier debugging) into a one dimensional array of type double named x_arr, declared to be 25 elements in length. Count the elements as you...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • In java please :-) 12 values have been input into array yearlyValues. Output all 12 elements,...

    In java please :-) 12 values have been input into array yearlyValues. Output all 12 elements, with 4 per line. If the elements are 10 20 30 40 50 60 70 80 90 100 110 120, the output is: 10 20 30 40 50 60 70 80 90 100 110 120 Hints: • Use a for loop with increment i += 4, rather than ++i. • Inside the for loop, just print all four elements using four print statements. An...

  • python programming → X CIO CIUSCUTETTE TUVOITTEESITTEETTICLE Question 30 10 pts (SHORT ANSWER) Assume the following:...

    python programming → X CIO CIUSCUTETTE TUVOITTEESITTEETTICLE Question 30 10 pts (SHORT ANSWER) Assume the following: • temps is a list of SIZE elements that has been assigned values • limit is a variable that has been assigned a value Write code to do the following: • Create a variable named count to keep track of the number of elements in temps list that are lesser than limit Using a loop, count the number of elements in the temps list...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • C++ please Possible algorithms – (1) use and modify the algorithm from program 2 if possible;...

    C++ please Possible algorithms – (1) use and modify the algorithm from program 2 if possible; (2) use unique value array; (3) use flag array (flags used to avoid counting a number more than 1 time); (4) compute frequency distribution (use unique and count arrays); (5) use count array to store the frequency of a number. No global variable are permitted in this assignment. Do not change the code provided only write the missing code. Write the missing C++ statements...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • Playing With Arrays The following problems are to give you practice with programming with arrays. Write...

    Playing With Arrays The following problems are to give you practice with programming with arrays. Write a program to: 1.Ask the user for numbers and place them in an array sequencially. 2.Next, print out the values in the array in the order they are stored. 3.Search the array for the postion containing the smallest value. 4.Print out the position where the smallest value was found. 5.Swap the smallest value with the value in the first postition of the array. 6.Print...

  • A. Write an Array Program Write a main program that counts the number of occurrences of...

    A. Write an Array Program Write a main program that counts the number of occurrences of the number 6.0 in a large array of doubles and then prints out the number of elements in the array and the number of values that are 6.0. Also, compute and print the average value to 7 decimal places of all the elements in the array. 1. Use a for loop. The array fArray [ is defined in a file called Lab8Adatasetx.h that will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT