Using arrays in C++: The number of millions of gallons of sewage that are disposed of each day for a major city is measured continuously for about one month. The records, saved in a file, EX6_1.DAT, follow: 123.,134,122,128,116,96,83,144,143,156,128,138 121,129,117,96,87,148,149,151,129,138,127 126,115,94,83,142 Write a program to calculate the frequency distribution using an interval of 10 million gallons per day. But modify the program so that the input is read by calling a function named read_data (). The input specs is to use the array sewage_amt[100] to read the number of millions of gallons from file EX6_1.DAT. The output spec is to display the following data on screen: Day no. Millions of gallons 1 123 2 134 3 122 ... Sewage per day frequency of occurrence 81-90 3 91-100 3 101-111 0 ...
1
Expert's answer
2012-07-24T10:34:42-0400
#include <iostream> #include <fstream> using namespace std; #include <string> void read_data(int *, int &); void process_and_out(int *, int); int main () { int sewage_amt[100]; int lenght; read_data(sewage_amt, lenght); process_and_out(sewage_amt,lenght);
system ("pause"); return 0; }
void read_data(int array[100], int &lenght){ ifstream fi; fi.open("EX6_1.DAT"); if (!fi) { & cout<<"Error! File not found! You can closed the program!\n"; } string a; string num; fi>>a; lenght = 0; for (int i = 0 ; i < a.size() ; i++, lenght++){ & while ( i < a.size() && a[i] != ',' ) & { & num += a[i]; & i++; & } & array[lenght] = atoi(num.c_str()); & num.clear(); & } } void process_and_out(int array[100], int lenght){ int frequency[20] = {}; int min, max; min = max = array[0]; for (int i = 0 ; i < lenght ; i++){ & if (array[i] > max ) max = array[i]; & if (array[i] < min ) min = array[i]; } max /= 10 ; max = max * 10 + 10; min /= 10; min *= 10; int minSave = min;
for (int i = 0 ; i < lenght ; i++){ & int index = array[i]/10 - min / 10; & frequency[index]++; }
for (int i = 0 ; i < lenght ; i++){ & cout << i+1 << ' '<< array[i]<<endl; } cout<<"Sewage per day frequency of occurrence:\n"; for (int i = 0 ; i < (max-minSave) / 10 ; i++){ & cout << min+1 << '-'; & min += 10; & cout<<min << ' ' <<frequency[i]<< endl;
Dear Carrie, Add this line to your code #include "stdafx.h".
Carrie
11.07.14, 17:21
I keep getting this error message. Can you plz help me? 1>------ Build
started: Project: Sewage, Configuration: Debug Win32 ------ 1>
Sewage.cpp 1>c:\users\carrie\documents\visual studio
2010\projects\sewage\sewage\sewage.cpp(72): fatal error C1010:
unexpected end of file while looking for precompiled header. Did you
forget to add '#include "StdAfx.h"' to your source? ========== Build:
0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Leave a comment
Thank you! Your comments have been successfully added. However, they need to be checked by the moderator before being published.
Comments
Dear Carrie, Add this line to your code #include "stdafx.h".
I keep getting this error message. Can you plz help me? 1>------ Build started: Project: Sewage, Configuration: Debug Win32 ------ 1> Sewage.cpp 1>c:\users\carrie\documents\visual studio 2010\projects\sewage\sewage\sewage.cpp(72): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source? ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Leave a comment