Average contents in array object

I need help figuring out how to average the contents of an array in my program. The program needs to create any number of objects (named test) and then average the contents of that array and display the average. I believe that I need to pass the arrayy and the numTests into the class member function avgScores() on line 22 in my TestScores.cpp file.

Here's what I have so far.

TestScores.h
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
  #ifndef TESTSCORES_H
#define TESTSCORES_H


class TestScores
{
    private:
        int scrs;
        int avgScores;

    public:
        TestScores();
        TestScores(int);

        //Mutators
        void setScore();
        void avgScores(TestScores, int);

        //Accessors
        int getScore()  { return scrs; }
        int getavgScores() { return avgScores; }

        virtual ~TestScores();
    protected:

};

#endif // TESTSCORES_H




TestScores.cpp

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 "TestScores.h"
#include <iostream>

TestScores::TestScores()
{
    //ctor
    scrs = 0;
}


TestScores::TestScores(int s) {
    scrs = s;
}

//set score initializes the scrs member variable with user entered data
void TestScores::setScore() {
    std::cout << "Enter test score: ";
    std::cin >> scrs;
}

//this function takes the average of the scores entered and stores them in the avgScores member variable.
//I need to add up each array element and then divide by the number of tests A.K.A. the array size
void avgScores(TestScores arr, int tests) {
    //avgScores =
    scrs   / tests; //difficulty here


}

TestScores::~TestScores()
{
    //dtor
}



Main.cpp
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 "TestScores.h"

int main()
{
    //int score;  //variable to hold the score
    int numTests;   //how many tests you want to grade
    TestScores arrayy[numTests];    //arrayy to hold number of tests and their scores


    std::cout << "How many tests would you like to grade? ";
    std::cin >> numTests;

    //Get user info
    for (int index = 0; index < numTests; index++)
    {
        arrayy[index].setScore();

    }

    avgScores(arrayy, numTests);


    //display user info
    for (int index = 0; index < numTests; index++)
    {
        std::cout << "\nYour score is: " << arrayy[index].getScore() << std::endl;
    }



    return 0;
}
Hello TheEurekaMan,

I will let the comments of the code say most of what I changed, but will start with this.

In main line 8 will not work this way. In C++11 and before this needs a constant number, so at compile time and run time the amount of space that is needed for the array can be set aside when the program runs. The only way to use a variable is to create a dynamic array when the program runs. What you can do is use a variable like "MAXSIZE" as I did. This variable needs to be defined as a "const" or "constexpr" to work. You could also use a "#define" at the beginning of the program.

The function "void avgScores(TestScores arr, int tests)" in not a member function of the class. Just because you declare the prototype in the class and define the function in the "TestScores.cpp" file does not make it part of the class. "TestScores::" makes it a class function. There is no real advantage to making this function part of the class and it would have to be done differently as a class function. As you have it written I would put this function in its own file or in the main file, but it does work if left in the "TestScores.cpp" file because "TestScores.h is included in the other two files. Since the prototype in in the "public" section it is seen by everything.

For the rest of the changes see the comments in the code and compare it to your original code.

TestScores.h
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
#ifndef TESTSCORES_H
#define TESTSCORES_H


class TestScores
{
private:
	int scrs;
	int avgScores; // <--- Not needed and should be set in the ctor if ued.

public:
	TestScores();
	TestScores(int);
	~TestScores(); //<---Does not need to be virtual.

	//Mutators
	void setScore();
	//void avgScores(TestScores, int);  // Not a member function.

	//Accessors
	int getScore() { return scrs; }
	int getavgScores() { return avgScores; }  // <--- Not needed.

protected:  // <--- Not needed.

};

#endif // TESTSCORES_H 


TestScores.cpp
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
#include "stdafx.h"
#include <stdhead.hpp>

#include "proto.h"

#include "TestScores.hpp"

TestScores::TestScores()
{
	//ctor
	scrs = 0;
}


TestScores::TestScores(int s)  // <--- OK, but never used.
{
	scrs = s;
}

//set score initializes the scrs member variable with user entered data
void TestScores::setScore() {
	std::cout << "Enter test score: ";
	std::cin >> scrs;
}

//  This function will not work. 
//this function takes the average of the scores entered and stores them in the avgScores member variable.
//I need to add up each array element and then divide by the number of tests A.K.A. the array size
//void avgScores(TestScores arr, int tests) {
//	//avgScores =
//	scrs / tests; //difficulty here
//
//
//}

// Not a class member function. OK to leave here, but should be in its own file or in main.
double avgScores(TestScores arrayy[], int numTests)
{
	double totalScores{ 0.0 };

	for (size_t lc = 0; lc < numTests; lc++)
	{
		totalScores += arrayy[lc].getScore();  // <--- Changed to "getScore".
	}

	return totalScores / numTests;
}

TestScores::~TestScores()
{
	//dtor
}


Main.cpp
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
#include <iostream>
#include <iomanip>

#include "TestScores.h"

double avgScores(TestScores arrayy[], int numTests);

int main()
{
	constexpr size_t MAXSIZE{ 20 };  // <--- Added.
									 //int score;  //variable to hold the score
	double average{ 0.0 };  // <--- Added.
	int numTests;   //how many tests you want to grade
	TestScores arrayy[MAXSIZE];    // arrayy to hold number of tests and their scores
								   //  <--- A more accurate description is: an array of classes.


	std::cout << "How many tests would you like to grade? ";
	std::cin >> numTests;

	//Get user info
	for (int index = 0; index < numTests; index++)
	{
		arrayy[index].setScore();
	}

	average = avgScores(arrayy, numTests);  // <--- Changed your function.


											//display user info
	for (int index = 0; index < numTests; index++)
	{
		std::cout << "\nYour score is: " << arrayy[index].getScore() << std::endl;
	}

	//  Added these lines. First line needs the header file "iomanip"
	std::cout << std::fixed << std::showpoint << std::setprecision(2);
	std::cout << "\n The average score is: " << average << std::endl;

	std::cin.get(); // <--- To keep the console window open if needed.

	return 0;
}


Hope that helps,

Andy
Topic archived. No new replies allowed.