Hey guys I'm new here and I'm sorry for bugging you all but I've had some problems with this program we are assigned to work on.
This is the project:
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
This is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void read_data(int *, int &);
void process_and_out(int *, int);
int main () {
int sewage_amt[100];
int length;
read_data(sewage_amt, length);
process_and_out(sewage_amt,length);
system ("pause");
return 0;
}
void read_data(int array[100], int &length){
ifstream fi;
fi.open("EX6_1.DAT");
if (!fi) {
cout<<"File not found. Close the program.\n";
}
string a;
string num;
fi>>a;
length = 0;
for (int i = 0 ; i < a.size() ; i++, length++){
while ( i < a.size() && a[i] != ',' )
{
num += a[i];
i++;
}
array[length] = atoi(num.c_str());
num.clear();
}
}
void process_and_out(int array[100], int length){
int frequency[20] = {};
int min, max;
min = max = array[0];
for (int i = 0 ; i < length ; 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 < length ; i++){
int index = array[i]/10 - min / 10;
frequency[index]++;
}
for (int i = 0 ; i < length ; i++){
cout << i+1 << ' '<< array[i]<<endl;
}
cout<<"Frequency per day: \n";
for (int i = 0 ; i < (max-minSave) / 10 ; i++){
cout << min+1 << '-';
min += 10;
cout<<min << ' ' <<frequency[i]<< endl;
}
}
///////////////////////////////////////////////////////
When it compiles and runs this is what I get:
1 123
Frequency per day:
121-130 1
Press any key to continue.
In other news, do you know that fi >> a; isn't getting all of the data? It stops at the newline character '\n' when it hits 138, then again at 127. Be sure to take in all the input.