Question

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

  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. 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.
  7. 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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The screenshots of code ,output and water.dat file are attached below:

Below is the C++ code for the same. All the parts from a to g are implemented one after another in the C++ program. Data is read from water.dat file.

#include<bits/stdc++.h>

using namespace std;

int main(){

//part a

cout<<" _________"<<endl;

cout<<"| PART a |"<<endl;

cout<<"|_________|"<<endl;

ifstream fin("water.dat");//for reading water.dat

int monthly_water_arr[50];

int num_months=0;

while(!fin.eof())

fin>>monthly_water_arr[num_months++];//reading data into monthly_water_arr

for(int i=0;i<num_months;i++)

cout<<"MONTH "<<i+1<<" : "<<monthly_water_arr[i]<<endl;//outputting monthly_water_arr

cout<<endl<<"============================================================"<<endl<<endl;

//part b

cout<<" _________"<<endl;

cout<<"| PART b |"<<endl;

cout<<"|_________|"<<endl;

for(int i=0;i<num_months;i++)

if(monthly_water_arr[i]>=71 && monthly_water_arr[i]<=80)//checking if value is btw 71-80

cout<<"MONTH "<<i+1<<" : "<<monthly_water_arr[i]<<" (71-80)"<<endl;//if so print the value

cout<<endl<<"============================================================"<<endl<<endl;

//part c

cout<<" _________"<<endl;

cout<<"| PART c |"<<endl;

cout<<"|_________|"<<endl;

int count_0=0;

for(int i=0;i<num_months;i++)

if(monthly_water_arr[i]>=71 && monthly_water_arr[i]<=80)//checking if value is btw 71-80

count_0++;//if so incrementing count_0

cout<<count_0<<" values between 71 and 80 (inclusive)."<<endl;

cout<<endl<<"============================================================"<<endl<<endl;

//part d

cout<<" _________"<<endl;

cout<<"| PART d |"<<endl;

cout<<"|_________|"<<endl;

int count_1=0;

for(int i=0;i<num_months;i++)

if(monthly_water_arr[i]>=81 && monthly_water_arr[i]<=90)//checking if value is btw 81-90

count_1++;//if so incrementing count_1

cout<<count_0<<" values between 71 and 80 (inclusive)."<<endl;

cout<<count_1<<" values between 81 and 90 (inclusive)."<<endl;

cout<<endl<<"============================================================"<<endl<<endl;

//part e & f

cout<<" ___________"<<endl;

cout<<"| PART e-f |"<<endl;

cout<<"|___________|"<<endl;

int count_arr[7]={0};

for(int i=0;i<num_months;i++)

//using if-else statements to put count in count_arr

if(monthly_water_arr[i]>=71 && monthly_water_arr[i]<=80)

count_arr[0]++;

else if(monthly_water_arr[i]>=81 && monthly_water_arr[i]<=90)

count_arr[1]++;

else if(monthly_water_arr[i]>=91 && monthly_water_arr[i]<=100)

count_arr[2]++;

else if(monthly_water_arr[i]>=101 && monthly_water_arr[i]<=110)

count_arr[3]++;

else if(monthly_water_arr[i]>=111 && monthly_water_arr[i]<=120)

count_arr[4]++;

else if(monthly_water_arr[i]>=121 && monthly_water_arr[i]<=130)

count_arr[5]++;

else if(monthly_water_arr[i]>=131 && monthly_water_arr[i]<=140)

count_arr[6]++;

for(int i=0;i<7;i++)

cout<<(7+i)*10+1<<"-"<<(8+i)*10<<" : "<<count_arr[i]<<endl;//using i to generate labels

cout<<endl<<"============================================================"<<endl<<endl;

//part g

cout<<" _________"<<endl;

cout<<"| PART g |"<<endl;

cout<<"|_________|"<<endl;

for(int i=0;i<7;i++)

if(count_arr[i]!=0)

cout<<(7+i)*10+1<<"-"<<(8+i)*10<<" : "<<count_arr[i]<<endl;//outputting only non-zero lines

cout<<endl<<"============================================================"<<endl<<endl;

}

Add a comment
Know the answer?
Add Answer to:
Exercise #2: 10 M gals water per day 71-80 81-90 91-100 101-110 111-120 121-130 131-140 10...
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
  • Programming Question

    Exercise #2: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...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • a.   Find the FV of $1,000 invested to earn 10% annually 5 years from now. Answer...

    a.   Find the FV of $1,000 invested to earn 10% annually 5 years from now. Answer this question by using a math formula and also by using the Excel function wizard. Inputs: PV = 1000 I/YR = 10% N = 5 Formula: FV = PV(1+I)^N = Wizard (FV): $1,610.51 Note: When you use the wizard and fill in the menu items, the result is the formula you see on the formula line if you click on cell E12. Put the...

  • so i have my c++ code and ive been working on this for hours but i...

    so i have my c++ code and ive been working on this for hours but i cant get it to run im not allowed to use arrays. im not sure how to fix it thank you for the help our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses...

  • okay so here is my c++ code and the errors im really stuck on fixing what...

    okay so here is my c++ code and the errors im really stuck on fixing what i did wrong it seems to be the same repeated error our job is to write a menu driven program that can convert to display Morse Code ere is the menu the program should display Menu Alphabet Initials N-Numbers - Punctuations S = User Sentence Q- Quit Enter command the user chooses A your program should use a loop and your morse code printing...

  • PL/SQL Auction Program 1. Create a user xyz, who is the owner of the auction. Create...

    PL/SQL Auction Program 1. Create a user xyz, who is the owner of the auction. Create the schema, and package. 2. Create users x1 and x2 who are the participants in the auction. They will need acces to the package. 3. Bid on the same item and record your observations. Verify all scenarios. Upload the files with the missing code and a detailed sample run. AUCTION OWNER.TXT SQL> conn / as sysdba Connected. SQL> drop user xyz cascade; User dropped....

  • 69. THE ENDOSPORE itself is might to stain_?_ color in an ACID FAST stain. (a) HOT...

    69. THE ENDOSPORE itself is might to stain_?_ color in an ACID FAST stain. (a) HOT pink (c) purple (d) green (e) baby-blue 70. All STAINS begin with a properly prepared _?_ . (a) dye (b) slide (c) smear (d) dog (e) cat 71. Which of the following is an ENDOTOXIN found in some microbes? This is results in fever, blood vessel dilation and possibly SHOCK when it is released into the human blood stream? (a) the plasma membrane (b)...

  • Python Pandas, Series and DataFrame Question (NO Loops, No If Statements, No List Comprehensions) The file...

    Python Pandas, Series and DataFrame Question (NO Loops, No If Statements, No List Comprehensions) The file bank.csv contains data about bank customers. The last column ('Personal Loan') indicates whether or not the customer was approved for a personal loan or not. Write a function named loan_by_zip that accepts 3 parameters: a file name, a minimum number of records, and a percentage approval rate. The function should return a DataFrame of those zip codes for which we meet the minimum number...

  • CASE 1-5 Financial Statement Ratio Computation Refer to Campbell Soup Company's financial Campbell Soup statements in...

    CASE 1-5 Financial Statement Ratio Computation Refer to Campbell Soup Company's financial Campbell Soup statements in Appendix A. Required: Compute the following ratios for Year 11. Liquidity ratios: Asset utilization ratios:* a. Current ratio n. Cash turnover b. Acid-test ratio 0. Accounts receivable turnover c. Days to sell inventory p. Inventory turnover d. Collection period 4. Working capital turnover Capital structure and solvency ratios: 1. Fixed assets turnover e. Total debt to total equity s. Total assets turnover f. Long-term...

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