I'm supposed to write a program that uses input file number.dat and output them to file stats.out. It is supposed to show the total number of positive values read, the total negative read, the total number read and the average of all values read, the largest and smallest values read, and what percent of the values were non-zero.
if (infile.fail()) {
cerr << "Can't open numbers.dat for input\n";
return 1;
}
outfile.open("stats.out");
if (outfile.fail()) {
cerr << "Can't open stats.out for input\n";
return 2;
}
first_number = true;
double number, max_number, min_number, average_number;
int total_positive, total_negative, zero;
while (infile >> number) {
total_positive = 0;
total_negative = 0;
zero = 0;
for (int i = 1; i <= Total_Numbers; i++) {
if (number > 0) {
total_positive = total_positive + 1;
}
if (number < 0) {
total_negative = total_negative + 1;
}
if (number == 0) {
zero = zero + 1;
}
if (first_number) {
max_number = number;
min_number = number;
first_number = false;
}
else if (number > max_number) {
max_number = number;}
else if (number < min_number) {
min_number = number;}
}}
outfile << "The largest number read was " << max_number << ".\n";
outfile << "The smallest number read was " << min_number << ".\n";
outfile << "The total number of positive values is " << total_positive << ".\n";
outfile << "The total number of negative values is " << total_negative << ".\n";
outfile << "The percent of non-zero values is " <<((total_negative + total_positive)/(total_negative + total_positive + zero)) << ".\n";
infile.close();
outfile.close();
return 0;
}
It outputs:
The largest number read was 3.42592e+007.
The smallest number read was -43000.3.
The total number of positive values is 14.
The total number of negative values is 0.
The percent of non-zero values is 1.
The totals and the percent are obviously off. So I was wondering if someone could help me out by finding where I went wrong. As well as help me incorporate finding the average. Thank You
int total_positive=0, total_negative=0, zero=0; //initialize herewhile (infile >> number) {
total_positive = 0; //remove this line
total_negative = 0; //remove this line
zero = 0; //remove this linefor (int i = 1; i <= Total_Numbers; i++) { //why you loop 14 times in here ?if (number > 0) {
total_positive = total_positive + 1;
}
if (number < 0) {
total_negative = total_negative + 1;
}
if (number == 0) {
zero = zero + 1;
}
...
Alright I deleted the for, and then I did what you said. Now Its outputing
The largest number read was 3.42592e+007.
The smallest number read was -43000.3.
The total number of positive values is 7.
The total number of negative values is 5.
The percent of non-zero values is 0.
Which is right except for the percent. It should be: 12/14
int total_positive=0, total_negative=0, zero=0; //initialize here
int running_total=0, cnt=0; //add this linewhile (infile >> number) {
running_total+=number; //add this line
cnt++; //add this line
total_positive = 0; //remove this line
total_negative = 0; //remove this line
zero = 0; //remove this line
for (int i = 1; i <= Total_Numbers; i++) { //why you loop 14 times in here ?
if (number > 0) {
total_positive = total_positive + 1;
}
if (number < 0) {
total_negative = total_negative + 1;
}
if (number == 0) {
zero = zero + 1;
}
..
outfile << "The arithmetic mean is " << (cnt==0 ? 0 : (running_total/cnt)) << ".\n"; //add this line