C++ Program that reads numbers from a file and calculates the average of them
https://gist.github.com/anonymous/9536644
Here is my code and basically these are the steps.
I feel like we have something good to work on but we keep getting errors. Literally any amount of help would be appreciated.
a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many numbers are in the file.
b. Save the output of the program in a file.
c. Modify the function getNumber so that it reads a number from the input file (opened in the function main), outputs the number to the output file (opened in the function main), and sends
the number read to the function main. Print only 10 numbers per line.
d. Have the program find the sum and average of the numbers.
e. Modify the function printResult so that it outputs the final results to the output file (opened in the function main). Other than outputting the appropriate counts, this new definition of the function printResult should also output the sum and average of the numbers.
Re: C++ Program that reads numbers from a file and calculates the average of them
Please post the code here rather than a link to an unknown web site. Myself and others here don't open links to unknown sites. When you post code, please format and use code tags. Go advanced, select the code and click '#'.
Re: C++ Program that reads numbers from a file and calculates the average of them
Code:
//Program: Classify Numbers
//This program counts the number of zeros, odd, and even numbers *?*/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Function prototypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);
bool getNumber(ifstream& numberFile, int& zeros, int& odds, int& evens);
void writeNumber(ofstream& resultsFile, int zeros, int odds, int evens,
int sum, int avg);
int calcSum( int zeros, int evens, int odds);
int main ()
{
ifstream inFile;
ofstream outFile;
int N;
int sum;
int avg;
//Variable declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd integers s
int evens; //variable to store the number of even integers
//int sum; // sum of the numbers
//int avg; //average of the number
initialize(zeros, odds, evens); //Step 1
//cout << "Please enter " << N << " integers."
// << endl; //Step 2
//cout << "The numbers you entered are: "
// << endl;
//numberfile.open ("f:number.dat");
// if(inFile)
//cout<< "Open \ 'f:numberfile.dat\' " << endl;
//else
//cout << "File not opened \ 'f:number.txt\' << endl;
inFile.open ("problem01Data.txt");
if(inFile)
{ //failure of opening number file
cout << "Error in opening number file .\n" << endl;
system ("pause");
return 0;
}
outFile.open("f:results.dat");
if(!outFile)
{ //failure of opeinging results file
cout << "Error in opening results file .\n" << endl;
system("pause");
return 0;
}
while ( getNumber (inFile, zeros, odds, evens))
{
for (counter = 1; counter <= N; counter++) //Step 3
{
getNumber(number); //Step 3a
cout << number << " "; //Step 3b
classifyNumber(number, zeros, odds, evens); //Step 3c
}// end for loop
// calcSum (zeroCount, evenCount, oddCount);
writeNumber(outFile, zeros, odds, evens, sum, avg);
}
cout << endl;
printResults(zeros, odds, evens); //Step 4
return 0;
}
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(int& num)
{
cin >> num;
}
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount)
{
switch (num % 2)
{
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
}
int calcSum( int zeroCount, int oddCount, int evenCount)
{
return zeroCount + oddCount + evenCount;
}
void printResults(int zeroCount, int oddCount, int evenCount)
{
cout << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;
cout << "The number of odd numbers is: " << oddCount
<< endl;
// calcuSum
}
Re: C++ Program that reads numbers from a file and calculates the average of them
Reposted it with the code, thanks for the tip man!
Re: C++ Program that reads numbers from a file and calculates the average of them
Firstly, have a look at http://forums.codeguru.com/showthrea...ork-assignment
From your code
Code:
while ( getNumber (inFile, zeros, odds, evens)) {
for (counter = 1; counter <= N; counter++) { //Step 3
where is N initialised? and where is the function definiton for this version of getNumber with 4 arguments?
Re: C++ Program that reads numbers from a file and calculates the average of them
We initialized N in the int main() function and I'm pretty sure the getNumber is in the our while statement.
Re: C++ Program that reads numbers from a file and calculates the average of them
you are defining here N to be of type int, but you aren't initialising (ie assigning a number) it.
Code:
while ( getNumber (inFile, zeros, odds, evens)) {
but in the code you provided, there is no definiton of this getNumber function - only for getNumber(int).
Re: C++ Program that reads numbers from a file and calculates the average of them
Code:
//Program: Classify Numbers
//This program counts the number of zeros, odd, and even numbers *?*/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Function prototypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& number, ifstream& inFile);
void classifyNumber(int number, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);
bool getNumber(ifstream& numberFile, int& zeros, int& odds, int& evens);
void writeNumber(ofstream& outFile, int zeros, int odds, int evens,
int sum, int avg);
int calcSum( int zeros, int evens, int odds);
int main ()
{
ifstream inFile;
ofstream outFile;
int sum;
int avg;
//Variable declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd integers s
int evens; //variable to store the number of even integers
//int sum; // sum of the numbers
//int avg; //average of the number
initialize(zeros, odds, evens); //Step 1
//cout << "Please enter " << N << " integers."
// << endl; //Step 2
//cout << "The numbers you entered are: "
// << endl;
//numberfile.open ("f:number.dat");
// if(inFile)
//cout<< "Open \ 'f:numberfile.dat\' " << endl;
//else
//cout << "File not opened \ 'f:number.txt\' << endl;
inFile.open ("problem01Data.txt");
if(inFile)
{ //failure of opening number file
cout << "Error in opening number file .\n" << endl;
system ("pause");
return 0;
}
outFile.open("f:results.dat");
if(!outFile)
{ //failure of opeinging results file
cout << "Error in opening results file .\n" << endl;
system("pause");
return 0;
}
while (inFile)
{
getNumber(number, inFile);
classifyNumber(number, zeros, odds, evens);
counter++;
sum = sum+number;
if (counter % 10 == 0)
outFile << endl;
}
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(int& num)
{
cin >> number;
}
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount)
{
switch (number % 2)
{
case 0:
evenCount++;
if (number == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
}
int calcSum( int zeroCount, int oddCount, int evenCount)
{
return zeroCount + oddCount + evenCount;
}
void printResults(int zeroCount, int oddCount, int evenCount)
{
cout << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;
cout << "The number of odd numbers is: " << oddCount
<< endl;
// calcuSum
}
Alright, so we got in contact with our professor and changed it up a little. Our only problem now is that one of the brackets is underlined red. Thank you again for your help.
Re: C++ Program that reads numbers from a file and calculates the average of them
Quote:
Our only problem now is that one of the brackets is underlined red.
And your closing bracket for main() is where? If you formatted your code with usual indents it is much easier to read and to see the matching opening and closing brackets.
You also have some other issues. Some functions have num as the argument but you are using number in the body. Also you have
Code:
getNumber(number, inFile);
but again you have not provided a function definition for a function that has these type of arguments - just a forward declaration.
Re: C++ Program that reads numbers from a file and calculates the average of them
Yeah that was a mistake on our bad and with the closing bracket for in main()
I changed all the nums to number.
Also in line 77 we defined getNumber, is that the wrong place to define it?
Re: C++ Program that reads numbers from a file and calculates the average of them
Line 77 is
Code:
getNumber(number, inFile);
This is not a definition. This is where you are calling the getNumber function - but there is no definition of the function in the code posted..
Re: C++ Program that reads numbers from a file and calculates the average of them
Alright so we worked on the code a little further and it does not have any trouble pulling the numbers from the file.
My only question now is if you could give us a hand with the outputs. When I try and debug the program it just tells me to press any key to continue.
Thanks
Code:
//Program: Classify Numbers
//This program counts the number of zeros, odd, and even numbers *?*/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Function prototypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& number, ifstream& inFile);
void classifyNumber(int number, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);
void writeNumber(ofstream& outFile, int zeroCount, int oddCount, int evenCount,
int sum, int avg);
int calcSum( int zeroCount, int evenCount, int oddCount);
int main ()
{
ifstream inFile;
ofstream outFile;
//Variable declaration
int sum = 0; // sum of the numbers
int avg = 0; //average of the numbers
int counter = 0; //loop control variable
int number; //variable to store the new number
int zeroCount = 0; //variable to store the number of zeros
int oddCount = 0; //variable to store the number of odd integers s
int evenCount = 0; //variable to store the number of even integers
initialize(zeroCount, oddCount, evenCount); //Step 1
inFile.open("problem01Data.txt");
while ( !(inFile.eof()) )
{
getNumber(number, inFile);
//cout << "Number: " << number << endl;
classifyNumber(number, zeroCount, oddCount, evenCount);
counter++;
sum = sum+number;
if (counter % 10 == 0)
outFile << endl;
}
return 0;
}
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(int& number, ifstream& inFile)
{
inFile >> number;
}
void classifyNumber(int number, int& zeroCount, int& oddCount,
int& evenCount)
{
switch (number % 2)
{
case 0:
evenCount++;
if (number == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
}
int calcSum( int zeroCount, int oddCount, int evenCount)
{
return zeroCount + oddCount + evenCount;
}
void printResults(int zeroCount, int oddCount, int evenCount)
{
cout << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;
cout << "The number of odd numbers is: " << oddCount
<< endl;
}
Re: C++ Program that reads numbers from a file and calculates the average of them
As zerocount, oddcount and evencount are set to 0 as part of the definition, you don't need initialize().
When you open the file, you are not testing to make sure the file is opened. You can either use the .is_open() method (http://www.cplusplus.com/reference/f...tream/is_open/) or test the stream using .good() method.
For output, you will need to open the required file for output (as in your code in post #7) and test that the file has been opened OK. Then you'll need to code the writeNumber() function and call it after the end of the while loop in main() and after you have calculated the average. I'm not sure why you are outputting a newline to outFile for every ten numbers read?
You seem to be coding this program 'on the hoof' as it were without having thought about how it is going to work first. Before starting to code a program, it is good practice to design the program first and then code the program from the design. The design expresses in simple English (or other natural language) how the program is going to accomplish its' objectives. eg A design for writing a program could be
Code:
determine inputs
determine outputs
determine any algorithms required (eg for finding average)
do {
design program
code program
test program
debug program
} while program_not_working