Arbitrary number

Sep 29, 2010 at 3:58am
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.

I have this so far.

/*
* xxxxxxx
* Assignment 3
* 9/28/2010
*/

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main() {
ifstream infile;
ofstream outfile;

const int Total_Numbers = 14;
bool first_number;

infile.open("numbers.dat");

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
Sep 29, 2010 at 4:07am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

int total_positive=0, total_negative=0, zero=0; //initialize here

while (infile >> number) {
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;
}
...
Sep 29, 2010 at 4:25am
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
Last edited on Sep 29, 2010 at 4:26am
Sep 29, 2010 at 4:30am
Which is right except for the percent. It should be: 12/14


You are expecting 12/14 or 0.8571428....

If 12/14, simple

 
outfile << "The percent of non-zero values is " << (total_negative + total_positive)  << "/" << (total_negative + total_positive + zero) << ".\n";
Sep 29, 2010 at 4:36am
Thank you that worked. One last thing. I can't think of anyway to find the arithmetic mean of all values read. Any ideas?
Sep 29, 2010 at 5:37am
That will depend on your definition of arithmetic mean. I assume you refer to the formula stated in http://en.wikipedia.org/wiki/Arithmetic_mean ?

Then basically you need a running total and the number of numbers.

x1 = 10
x2 = 30
x3 = 40

Arithmetic mean = (10+30+40)/3 = 26.666...

I don't know your input file got how many numbers cuz if they are a lot, the running total can easily go beyond a C++ int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int total_positive=0, total_negative=0, zero=0; //initialize here
int running_total=0, cnt=0; //add this line

while (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
Sep 29, 2010 at 6:27am
Thank you very much it worked perfectly.
Topic archived. No new replies allowed.