I'm taking a computer science class, but I do not have a very good professor. He does not explain anything.
We have an assignment to :
Write a complete C++ program in stats.cc to read an arbitrary number of real numbers(one value per input record) from a specified input file and write to a specified output file each of the values as they are read in and print with appropriate labels the following summary output (after all values have been individually read and processed):
1. the number of strictly positive values read (those greater than zero)
2. the number of strictly negative values read (those less than zero)
3. the total number and arithmetic mean (average) of values read
4. the largest and smallest values read
5. percentages of the number of strictly negative values and strictly positive values read


I'm not looking for anyone to write this for me, but rather to explain exactly what he is asking me for and help me out.


I know I need:

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

using namespace std;

int main() {

ifstream infile;
ofstream outfile;

Sounds like you are going to use a while loop to read in lines from your input file, inside the loop, you want to print out to the console the number, and then before the next iteration, output the number to your output file, If i correctly understand the question that is.

so, for my positive and negative values...

int main()
{
int n;
cout <<"Enter the starting number>";
cin >> n;

if (n > 0)
cout << "n is positive";
else if (x < 0)
cout << "n is negative";
else
cout << "x is 0";

is any of this right at all?

>>1. the number of strictly positive values read (those greater than zero)
>>2. the number of strictly negative values read (those less than zero)

Your code is correct for the above.

>> 3. the total number and arithmetic mean (average) of values read
Use two variables. The fist will be the total sum of the values that you read in. The second will be the total number of values you read in. Once you have those two values, divide and get the average.

sum += sum + num; //where num is the value that is read in
counter ++; //counter starts at 0, then increments by one every time a number is added to sum.

>>4. the largest and smallest values read
This is pretty easy. Make two variables, largest and smallest. If num < smallest, then num is your new smallest. If num > largest, then num is your new largest.

>>5. percentages of the number of strictly negative values and strictly positive values read

You will need the total number of values read in (counter variable should suffice) and the total number of positive and the total number of negative values. You can use seperate counters for each.

so 4,

int l = largest;
int s = smallest;
if (n > l); //then n is new largest
if (n < s); //then n is new smallest

and 5,

a code to calculate the sum of all positive/(counter ++) //then percent of 
                                                         //num strictly positive
a code to calculate the sum of all negative/(counter ++)//then percent of 
                                                        //num strictly negative

and I still need to write an outfile?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.