[HELP] My code is not getting the correct data

Code is failing to do the correct average calculations. So when i compile i get some really large number in sci notation.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>

using namespace std;

int studentsSurveyed();
int * studentArray(int);
int studentResults();
int numMoviesViewed(int *);
float averageViewedMovies(int*);
void displayReports();
void Reports(int, double);

int main()
{
    int *movieArray;
    int numStudents;
    double averageMoviesViewed;



    double avgMoviesViewed = 0;
    numStudents = studentsSurveyed();
    movieArray = studentArray(numStudents);


    numMoviesViewed(movieArray);
    avgMoviesViewed = averageViewedMovies(movieArray);



    cout << "The average number of movies viewed by the group of students is " << avgMoviesViewed << endl;

    Reports(numStudents, avgMoviesViewed);



    return 0;
}

int studentsSurveyed()
{
    int numStudents;

    cout << "How many total students were surveyed?\n";
    cin >> numStudents;

    return numStudents;
}

int * studentArray(int numStudent)
{
    int * array;
    array = new int[numStudent];

    for (int count = 0; count < numStudent;count++)
    {
        array[count];
    }
    return array;
}

int numMoviesViewed(int *array)
{
    int movies = 0;
    int totMovies = 0;
    if(sizeof(*array) <= 0)
    return -1;

    for(int student = 0; student < (sizeof(*array) - 1); student++)
    {
    cout << "How many movies has student " << (student + 1) <<" seen this month?\n";
    cin >> movies;

    // array[student] = movies;

    totMovies += (int)movies;
    }
    return 1;
}

float averageViewedMovies(int *array)
{
    float average = 0;
    int totMovies = 0;

    for(int i=0; i < sizeof(*array)-1; i++)
    {
        totMovies += array[i];
        //cout << "debug" << array[i] << endl;
    }
    average = totMovies / sizeof(*array) -1;


    return average;

}

void Reports(int numStudents, double avgMoviesViewed)
{
    cout << "The total number of students surveyed was " << numStudents << " and the average movies veiwed was "
         << avgMoviesViewed << "." << endl;

         return;
}
Also the total number of students surveyed is complying with the number of movies each said student watched.

ex: enter 5 total student, program only asks for 3 students
sizeof(*array)
This is not the right way to get the array length.

If the size of the array is declared in compile time then use can use sizeof(array) / sizeof(int) to get the length of the array. But in your case "array" is a dynamic array, so you'll have to include numStudents in every function that include int *array, such as:
int numMoviesViewed(int *, int);
float averageViewedMovies(int*, int);
and
int numMoviesViewed(int *array, int numStudents)
float averageViewedMovies(int *array, int numStudents)

and then change "sizeof(*array)" to "numStudents"

and one more thing, why (minus 1), shouldn't you include the last student in
for(int student = 0; student < (sizeof(*array) - 1); student++)
Last edited on
the -1 was compensatinf for an off by one issue. i could find it, so i used -1 to correct it. ill need to fix the array problem and continue debugging
sizeof(*array) will return sizeof(int) because array is an integer array, that is *array is int and sizeof(*array) will return sizeof(int) which is 4. That explains why you need -1 to get to 3 =.= Your number of students could be 4 5 6 or 2 because it is dynamic. If you were only asked for 3 then make an array of 3 int instead of using "new". Oh and you'll have to delete movieArray to free memory that you allocated...

int main()
{
....
delete [] movieArray;
return 0;
}
Topic archived. No new replies allowed.