Hello, I'm just starting out as a programmer in college as a freshman.
I am not posting a homework question, but my most recent assignment asks for me to make a function that will calculate the minimum, maximum and average of these numbers. After hours of nothing but zeros as output. Anyways I am wondering if my code could possibly be wrong for my function and if so what exactly am I doing wrong? Thanks in advance.
the file contains these numbers
7 4 5 6 3 4 7 2
5 3 8 1 1 2
Here is the code for the main program itself.
#include <iostream>
#include <fstream>
int *readFile(std::ifstream &inFile, int &num_students);
int computeAverage (std::ifstream &inFile, int &num_students);
int main ()
{
char filename [51]; //character definition for file input
int num_students;
int pointcount = 1;
int studentsum = 0;
std::ifstream inFile;
std::cout << "Please enter the name of the file: ";
std::cin >> filename;
inFile.open(filename);
if (!inFile)
{
std::cout << "File not read successfully.";
}
else
{
std::cout <<"File read successfully." <<std::endl;
}
int *array = NULL;
while (array = readFile(inFile, num_students))
{
std::cout <<"There are "<<num_students<<" people in class number "<< pointcount++ <<std::endl;
studentsum += num_students;
}
num_students = 0;
std::cout << "Total number of students: "<< studentsum << std::endl;
int averagesum = 0;
std::cout <<"The average number of movies watched is: " << computeAverage(inFile, num_students);
return 0;
}
And here is the problem code
#include <iostream>
#include <fstream>
int computeAverage (std::ifstream &inFile, int &num_students)
{
int Average = 0;
int averagesum = 0;
inFile >> num_students;
if(!inFile.eof())
{
int movieavg[num_students];
int *movieavgptr = movieavg;
for(int count = 0; count < num_students; count++)
{
averagesum += *movieavgptr;
movieavgptr++;
}
averagesum += Average;
averagesum/2;
return averagesum;
}
}
The output is here
Please enter the name of the file: csc2101.txt
File read successfully.
There are 7 people in class number 1.
There are 5 people in class number 2
Total number of students: 12
The average number of movies watched is: 0
I honestly do not see where I could be dividing by zero unless one of my variables are not working or that it is not reading the file though it does in my earlier function to get the total number of students.
#include <iostream>
#include <fstream>
int *readFile(std::ifstream &inFile, int &num_students);
double computeAverage (std::ifstream &inFile, int &num_students);
int main ()
{
char filename [51]; //character definition for file input
int num_students;
int pointcount = 1;
int studentsum = 0;
std::ifstream inFile;
std::cout << "Please enter the name of the file: ";
std::cin >> filename;
inFile.open(filename);
if (!inFile)
{
std::cout << "File not read successfully.";
}
else
{
std::cout <<"File read successfully." <<std::endl;
}
int *array = NULL;
while (array = readFile(inFile, num_students))
{
std::cout <<"There are "<<num_students<<" people in class number "<< pointcount++ <<std::endl;
studentsum += num_students;
}
num_students = 0;
std::cout << "Total number of students: "<< studentsum << std::endl;
std::cout <<"The average number of movies watched is: " << computeAverage(inFile, num_students);
return 0;
}
Sorry about not replying in a bit, had a few things to take care of. I just noticed that i see what you mean Average is doing nothing for this function. After looking more closely at this, could it be possible that since the line