Question for my code :)

Hello all,

I would like to ask a question regarding a coding exercise. I have an exercise where I have to create a program with 10 students in which each student attends 3 courses and obtains 3 grades. AFter it has to calculate the average for each student and, if the average is >18.5 the program should return that the student excelled otherwise if the average is under 9.5 that the student has failed.

I wrote my object oriented code which works almost perfectly except that it seems that it does not insert the object array I created to the method that calculates the average for each student. Given this, my code prints that all of the students failed because it probably can not see the my object aray. the code is given below. Any help would be highly appreciated :) :

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
  using namespace std;

// Class student
class student {

public:   
    int ma8ima_1;
    int ma8ima_2;
    int ma8ima_3;
    student();
    student( float a, float b, float c);
    ~student();
    float average (float a, float b, float c);
} stnt[10];

// Dummy constructor
student::student() {
    ma8ima_1 = 0;
    ma8ima_2 = 0;
    ma8ima_3 = 0;
}
//constructor
student::student(float a, float b, float c) {
    ma8ima_1 = a;
    ma8ima_2 = b;
    ma8ima_3 = c;
}
// destructor
student::~student() {

}
// average method
float student:: average (float a, float b, float c) {

    return (a + b + c) / 3;
}

int main() {

   //variable and object array definitions
    int i, a, b, c, count1=0, count2=0;
    student stnt[10];

    //Inputs the grades for the first course
    cout << "dwse ba8mous sto prwto ma8ima" << endl;
    for (i = 0; i < 10; i++) {
        cin >> a;
        stnt[i].ma8ima_1 = a;     
    }
    //Inputs the grades for the second course
    cout << "dwse ba8mous sto deutero ma8ima" << endl;
    for (i = 0; i < 10; i++) {
        cin >> b;
        stnt[i].ma8ima_2 = b;
    }
    //Inputs the grades for the third course
    cout << "dwse ba8mous sto trito ma8ima" << endl;
    for (i = 0; i < 10; i++) {
        cin >> c;
        stnt[i].ma8ima_3 = c;
    }
    //prints the array of students with student's 3 courses
    for (i = 0; i < 10; i++) {
        cout << stnt[i].ma8ima_1 << endl;
        cout << stnt[i].ma8ima_2 << endl;
        cout << stnt[i].ma8ima_3 << endl;
    }
    //Finds the average for each student
    for (i = 0; i < 10; i++) {
        if (stnt[i].mesos_oros(a, b, c) < 9.5) {
            cout << "apetuxe" << endl;
            count1++;
        }
        else if (stnt[i].mesos_oros(a, b, c) > 18.5) {
            cout << "petuxe" << endl;
            count2++;
        }
    }
    //prints how excelled and who failed
    cout << "apetuxan: " << count1 << " " << "foitites" << "se pososto " << " " << (count1) / (count1 + count2) << '%' << endl;
    cout << "petuxan: " << count2 << " " << "foitites" << "se pososto " << " " << (count2) / (count1 + count2) << '%' << endl;
    return 0;
}
Last edited on
Your a, b, c variables are just whatever the user happened to type in last during the 'input' part of your code. As in, you seem to only be using them locally as temporary variables for input. Are you sure that's what you should be passing into your 'mesos_oros' function?
Last edited on
Hello geovoulg,

First you should compile your program and try to fix the errors before posting.

Prefer to use "double" over "float". Better, but not 100% accurate all the time.

Everything in the class is "public". This does work, but tends to defeat the purpose of the class by protecting the variables and sometimes functions.

In the statements if (stnt[i].mesos_oros(a, b, c) < 9.5) you are trying to call the function, but it is not defined. You either need to change the name here or change the class function name.

I have not tried to run this code yet, but the comments should be helpful.
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
105
106
107
108
109
110
111
112
#include <iostream>

using namespace std;

// Class student
class student
{
    public:
        int ma8ima_1;  // <--- All 3 should be a "double". Also should be "private".
        int ma8ima_2;
        int ma8ima_3;

        student();  // <--- Needed. No longer provided by the compiler because of the next line.

        student(double a, double b, double c);

        //~student();  // <--- Not necessary unless you define your own. Which is not necessary here. Will be provided by the compiler.
        double mesos_oros(double a, double b, double c);  // <--- Changed name.
};  // <--- stnt[10]; This array never used.

// Dummy constructor. Default ctor not dummy.
student::student()
{
    ma8ima_1 = 0;
    ma8ima_2 = 0;
    ma8ima_3 = 0;
}

//constructor. Overloaded ctor.
student::student(double a, double b, double c)
{
    ma8ima_1 = a;  // <--- These lines storing a "double" in an "int". Will loose the decimal part.
    ma8ima_2 = b;
    ma8ima_3 = c;
}

// destructor. Will be provided by the compiler.
//student::~student()
//{
//
//}

// average method
double student::mesos_oros(double a, double b, double c)  // <--- Changed name.
{

    return (a + b + c) / 3;
}

int main()
{

    //variable and object array definitions
    int i, a, b, c, count1 = 0, count2 = 0;
    student stnt[10];

    //Inputs the grades for the first course
    cout << "dwse ba8mous sto prwto ma8ima" << endl;  // <--- give grades in the first lesson.
    
    for (i = 0; i < 10; i++)
    {
        cin >> a;
        stnt[i].ma8ima_1 = a;
    }

    //Inputs the grades for the second course
    
    cout << "dwse ba8mous sto deutero ma8ima" << endl;  // <--- give grades in the second lesson.
    for (i = 0; i < 10; i++)
    {
        cin >> b;
        stnt[i].ma8ima_2 = b;
    }

    //Inputs the grades for the third course        
    cout << "dwse ba8mous sto trito ma8ima" << endl;  // <--- give grades in the third lesson.
    
    for (i = 0; i < 10; i++)
    {
        cin >> c;
        stnt[i].ma8ima_3 = c;
    }

    //prints the array of students with student's 3 courses
    for (i = 0; i < 10; i++)
    {
        cout << stnt[i].ma8ima_1 << endl;
        cout << stnt[i].ma8ima_2 << endl;
        cout << stnt[i].ma8ima_3 << endl;
    }

    //Finds the average for each student
    for (i = 0; i < 10; i++)
    {
        if (stnt[i].mesos_oros(a, b, c) < 9.5)
        {
            cout << "apetuxe" << endl;
            count1++;
        }
        else if (stnt[i].mesos_oros(a, b, c) > 18.5)
        {
            cout << "petuxe" << endl;
            count2++;
        }
    }

    //prints how excelled and who failed
    cout << "apetuxan: " << count1 << " " << "foitites" << "se pososto " << " " << (count1) / (count1 + count2) << '%' << endl;
    cout << "petuxan: " << count2 << " " << "foitites" << "se pososto " << " " << (count2) / (count1 + count2) << '%' << endl;
   
    return 0;
}


"int i" should be defined in the for loops unless there is some reason to need its value outside the for loop.

Variables "a", "b" and "c" first should have better names. It makes the code easier to read. Second ALL variables should be initialized when defined. It is good practice to get use to. Also these variables would be better defined as "double"s.

Andy
Is there a requirement to use OOP/class as this can be accomplished more simply.

However, using a class consider:

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
#include <iostream>
using namespace std;

const size_t NO_STUDENT {10};

// Class student
class student {
public:
	int ma8ima_1 {};
	int ma8ima_2 {};
	int ma8ima_3 {};

	student() {};
	student(float a, float b, float c) : ma8ima_1(a), ma8ima_2(b), ma8ima_3(c) {}

	float average() { return (ma8ima_1 + ma8ima_2 + ma8ima_3) / 3.0; }
} stnts[NO_STUDENT];

int main()
{
	//Inputs the grades for the first course
	cout << "dwse ba8mous sto prwto ma8ima" << endl;
	for (int i = 0; i < NO_STUDENT; ++i)
		cin >> stnts[i].ma8ima_1;

	//Inputs the grades for the second course
	cout << "dwse ba8mous sto deutero ma8ima" << endl;
	for (int i = 0; i < NO_STUDENT; ++i)
		cin >> stnts[i].ma8ima_2;

	//Inputs the grades for the third course
	cout << "dwse ba8mous sto trito ma8ima" << endl;
	for (int i = 0; i < NO_STUDENT; ++i)
		cin >> stnts[i].ma8ima_3;

	//Prints the array of students with student's 3 courses
	for (int i = 0; i < NO_STUDENT; ++i) {
		cout << stnts[i].ma8ima_1 << '\n';
		cout << stnts[i].ma8ima_2 << '\n';
		cout << stnts[i].ma8ima_3 << '\n';
	}

	int count1 {}, count2 {};

	//Finds the average for each student
	for (int i = 0; i < NO_STUDENT; ++i) {
		const float av {stnts[i].average()};

		if (av < 9.5) {
			cout << i + 1 << " apetuxe\n";
			++count1;
		} else
			if (av > 18.5) {
				cout << i + 1 << " petuxe\n";
				++count2;
			}
	}

	//prints who excelled and who failed
	cout << "apetuxan: " << count1 << " foitites" << " se pososto  " << count1 / (NO_STUDENT + 0.0) * 100.0 << "%\n";
	cout << "petuxan: " << count2 << " foitites" << " se pososto  " << count2 / (NO_STUDENT + 0.0) * 100.0<< "%\n";
}


Last edited on
Guys, thank you for your replies!

@Ganado: I didn't quite understand what you said. My logic was to just define my variables and my object array (students), then to fill my object array using the loops so that to input the three courses per student, and then to calculate the average using the mesos_oros (mesos oros actually means average). What exactly do you mean that I use them locally?

@Andy: In relation to my variables, I tried to make them private but when I did that the compiler considered them inaccessible. How can I solve this?

In relation to the mesos_oros function, you wrote that I didn't define it. However, under my class I defined it as :

double student::mesos_oros(double a, double b, double c) // <--- Changed name.
{

return (a + b + c) / 3;
}

Should I redefine it so that the function can work?
@Seeplus: Thank you very much for the code. Could I ask you, do you vahe any idea why the function, as I wrote it, coudn't calculate the average of the objects' numeric values given?
 
if (stnt[i].mesos_oros(a, b, c) < 9.5) {


When this line of code is executed, within a loop, what are the values of a b c each time the loop is executed?
I would like to believe that since I previously filled the object array with a, b, c then throughout the loop it will use these values. So if I understand correctly the loop, as I built it, it just uses a,b and c declared at the start of main, and no with the variables I inputted, since I haven't wrote a code which inputs the previously object array to the function mesos_oros?
Add this before the if statement, and you'll understand better:
1
2
3
4
cout << a << ' ' << b << ' ' << c << '\n';
if (stnt[i].mesos_oros(a, b, c) < 9.5) {
    // ...
}


(Printing is the "poor man's debugger"; I advise to just use a debugger with break points.)
Hello geovoulg,


@Andy: In relation to my variables, I tried to make them private but when I did that the compiler considered them inaccessible. How can I solve this?


When a variable of a class is private only a member function of the class can access the variable. Generally you would use "public" "get" and "set" type functions of the to access the "private" class members.


In relation to the mesos_oros function, you wrote that I didn't define it. However, under my class I defined it as :

double student::mesos_oros(double a, double b, double c)


In your original code lines 13 and 33 both use the name "average". "mesos_oros" may mean "average" to you, but to the compiler they are completely different.


Should I redefine it so that the function can work?

Something has to be changed. Whatever you decide on just be consistent with its use.

seeplus wrote:

When this line of code is executed, within a loop, what are the values of a b c each time the loop is executed?


Your variables (a, b and c) are local variables to "main". Where does their value come from? And what are their values when the function "mesos_oros" is called? Had you given variables (a, b and c) a better name to describe them you might just see that in the function call you would not have to send any variables, but use the variables available in the object of that particular element of the array. Since that is where the real information would be stored.

Andy

Thank you all for your patience and your help.

@Andy: Thank you for your detailed explanations. Regarding the average function, there is a misunderstanding. In my original code it was consistent as mesos_oros. I changed the name and made it "average" so I can upload it here but it seems I forgot to do that in my whole code.

Regarding my variables, I changed my main variables a, b and c to k, l, m but the problem persists.

Thank you again!
Last edited on
Hello geovoulg,

I think that you may be misunderstanding some things here.

When you write: student stnt[10];. You are declaring an array of 10 elements numbered (0) - 9. Each element is a complete class. When first defined they are all the same. As you progress with the program you change the variables in each class in each element of the array. When you are finished each element of the array is different based on the value of the variables, but still has all the functions that go with the class.

When you call the member function "mesos_oros" it is the function of that element of the array you are working on and no other.

Something you might consider for the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Class student
class student
{
    public:
        student();  // <--- Needed. No longer provided by the compiler because of the next line.
        student(double a, double b, double c);

        void SetSetMa8ima_1(double grade);  // <--- 3 needed because of the for loops in "main".
        void SetSetMa8ima_2(double grade);
        void SetSetMa8ima_3(double grade);

        double mesos_oros();  // <--- Changed.

    private:
        double ma8ima_1;
        double ma8ima_2;
        double ma8ima_3;
};  // <--- stnt[10]; This array never used. 


And the functions:
1
2
3
4
5
6
7
8
9
void student::SetMa8ima_1(double grade)
{
    ma8ima_1 = grade;
}

double student::mesos_oros()  // <--- Changed.
{
    return (ma8ima_1 + ma8ima_2 + ma8ima_3) / 3.0;
}


In the for loops in "main":
1
2
3
4
5
6
for (int i = 0; i < MAXSIZE; i++)
{
    // <--- Could use a prompt here.
    cin >> a;
    stnt[i].SetMa8ima_1(a);
}


"MAXSIZE" is defined at the beginning of main as:constexpr int MAXSIZE{ 3 };. This way you do not have to hunt for every for loop that has a 10 in it when it needs to be changed. Also I started with a smaller number to test the program and when it is working just change it back to 10 and "MAXSIZE" will change to 10 everywhere it is used.

Andy
Andy thank you very much for your extensive reply. Much appreciated! :)
Hello geovoulg,

You are welcome.

After working with the code I not only have the 3 set functions there are 3 get functions:
1
2
3
4
5
6
7
8
9
10
void SetMa8ima_1(double grade);  // <--- 3 needed because of the for loops in "main".
void SetMa8ima_2(double grade);
void SetMa8ima_3(double grade);

double GetMa8ima_1()
{
    return ma8ima_1; 
}
double GetMa8ima_2() { return ma8ima_2; }
double GetMa8ima_3() { return ma8ima_3; }

You can write the code either way, but the "double GetMa8ima_1()" may be the easier one to understand for now.

The get functions are used with:
1
2
3
4
5
6
7
    for (int i = 0; i < MAXSIZE; i++)
    {
        cout
            << stnt[i].GetMa8ima_1() << '\n'
            << stnt[i].GetMa8ima_2() << '\n'
            << stnt[i].GetMa8ima_3() << "\n\n";
    }

You can chain the "cout" statement into 1 line in this manner instead of writing a "cout" for each line. This can be handy to help visualize what your output will look like.

In the last for loop there is a gap between 9.5 and 18.5 where nothing happens. This could be a problem not counting something that may be should be counted?

Andy
Topic archived. No new replies allowed.