Averaging Numbers from a file through use of an array and pointer

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

You're using integer division. Use double when dividing.

Also,

[code]
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Hello, formatting!" << std::endl;
}
[/code]
Thank you for the quick reply, I took you're advice and that did change the output instead of 0 it is now nan
Last edited on
The most likely reason it is NaN is that you are dividing by 0 somehow.
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.
Can you paste your current program [code]between code tags[/code] so we can see what you've done to change it?
Here is the updated code to the averaging function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
double computeAverage (std::ifstream &inFile, int &num_students)
{
    double Average = 0;
    double 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;
    }
}


and here is the main file in case it is needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
}
averagesum/2;

This does nothing. Maybe you meant /= perhaps? Also, you're not affecting the value of Average in any way; you're returning averagesum.
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
 
averagesum/num_students;


is outside of the for loop
1
2
3
4
5
for(int count = 0; count < num_students; count++)
        {
            averagesum += *movieavgptr;
            movieavgptr++;
        }

could averagesum could still = 0?
Last edited on
averagesum/num_students;
This line doesn't actually do anything at all, I mean. It has no meaning.

Consider:
1
2
x / y; //does nothing
x /= y; //x = x / y 
Topic archived. No new replies allowed.