Arrays and Array parameters

Really stuck on this practice assignment..


The Grader class is keeping track of a set of exam scores.  Scores can be added
 one at a time thru calls to addScore( ... ) or it can add a set of scores thru 
calls to addScores( ... ).  Once scores have been collected, the Grader class can 
find the best and worst scores it has seen so far thru calls to findBiggest( ) and 
findSmallest( ).  Both of these methods are read-only operations, so you won't be 
able to change or update anything inside the Grader instance object.  Processing 
arrays lead to lots of loops and that is what will be necessary in these methods.  
As for the MAX_SIZE constant, I would recommend you just set it to a very large 
number (say 100...) and move on.


So far this is the code I have

Grader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef GRADER_H
#define GRADER_H
#include <iostream>
#include <cstdlib>

class Grader {

Grader( );

void addScore( int score );
void addScores( int scores[], 
				int size );
void clear(); 

int findBiggest( ) const;
int findSmallest( ) const;
int my_Values[ MAXSIZE ];
int my_valuesSeenSoFar;

};
#endif


Grader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "Grader.h"

Grader::Grader( ){
}

void Grader::addScore( int score ){
}
void Grader::addScores( int scores[], 
				int size ){
}
void Grader::clear(){
}

int Grader::findBiggest( ) const{
}
int Grader::findSmallest( ) const{
}
int Grader::my_Values[ MAXSIZE ]{
}
int Grader::my_valuesSeenSoFar{
}


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

int main( )
{
Grader g;
double d[5]= {99,70,85,93,84};
double e[4]= {100,81,60,91};

g.addScore( 75 );
g.addScore( 82);
g.addScores( d, 5 ); 

cout << "Best Score = " << g.findBiggest( ) << endl;
/// should give value 99
cout << "Worst Score = " << g.findSmallest( ) << endl;
/// should give value 70
g.clear( );

g.addScore( 50 );
g.addScore( 74 );
g.addScores( e, 4 ); 

cout << "Best Score = " << g.findBiggest( ) << endl;
/// should give value 100
cout << "Worst Score = " << g.findSmallest( ) << endl;
/// should give value 50 
}



I suppose what's causing me the most confusion is how to go about programming the addScores constructor.

How might I program it so I can bring numbers into a "Grader g;" to run a code such as "g.addScore( 75 );" and have 75 added to a list of scores

Thanks a bunch!

I suppose what's causing me the most confusion is how to go about programming the addScores constructor.


I can see where that might be confusing. There is no addScores constructor.

A constructor is a member function that initializes an object. It shares the same name as the class. So, for a class called Grader the constructor(s) will be named Grader. The constructor would be a great place to start, since an empty one is not correct for this class. Although, fixing the access level for your class members so the code in main compiles should probably be fairly high on the list of priorities. Every member of Grader is private in the class definition you presented above.

I've gone through and tidied up the code a bit

Grader.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
#ifndef GRADER_H
#define GRADER_H
#define MAXSIZE 100
#include <iostream>
#include <cstdlib>

class Grader {

public:
Grader( );

void addScore( int score );
void addScores( int scores[], 
				int size );
void clear(); 


int findBiggest( ) const;
int findSmallest( ) const;

private:
int my_Values[ MAXSIZE ];
int my_valuesSeenSoFar;

};
#endif


Grader.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#define MAXSIZE 100
#include "Grader.h"

Grader::Grader( ){
}

void Grader::addScore( int score ){
}
void Grader::addScores( int scores[], 
				int size ){
}
void Grader::clear(){
}

int Grader::findBiggest( ) const{
}
int Grader::findSmallest( ) const{
}


Driver.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
#include <iostream>
#include "Grader.h"
using namespace std;
int main( )
{
Grader g;
double d[5]= {99,70,85,93,84};
double e[4]= {100,81,60,91};

g.addScore( 75 );
g.addScore( 82);
g.addScores( d, 5 ); 

cout << "Best Score = " << g.findBiggest( ) << endl;
/// should give value 99
cout << "Worst Score = " << g.findSmallest( ) << endl;
/// should give value 70
g.clear( );

g.addScore( 50 );
g.addScore( 74 );
g.addScores( e, 4 ); 

cout << "Best Score = " << g.findBiggest( ) << endl;
/// should give value 100
cout << "Worst Score = " << g.findSmallest( ) << endl;
/// should give value 50 
}


I am still having trouble trying to figure out how to program the addScore/addScores

How would you add a number to an array where they can individually be compared?




:pulling hair:
Last edited on
I am still having trouble trying to figure out how to program the addScore/addScores


As I mentioned before, I would begin with the constructor. Until it is correct anything else you do is building on a faulty foundation.
As I mentioned before, I would begin with the constructor. Until it is correct anything else you do is building on a faulty foundation.


I know that empty constructor is incorrect, but I am not sure what exactly to put there-- that's my problem.


would it be something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#define MAXSIZE 100
#include "Grader.h"

Grader::Grader( ){
}

void Grader::addScore( int score ){
	score = my_Values[MAXSIZE];
}
void Grader::addScores( int scores[], int size ){
	scores[size] = my_Values[MAXSIZE];
}
void Grader::clear(){
}

int Grader::findBiggest( ) const{
	for (int i = 0; i < MAXSIZE; i++);
}
int Grader::findSmallest( ) const{
}

?
Last edited on
You have two data members of Grader. The constructor is the place to initialize them if they need it, so do that.

Valid indices for the array my_Values is 0 to MAXSIZE-1. my_values[MAXSIZE] is an element that doesn't exist.
what would score= be then

also,
how do I call int i into scores[i] for example-- is this something i'd even want to do?
what would score= be then


Why would you be setting score to something?

You need to understand what your variables represent if you want to manipulate them. Right now you're just guessing.

What are my_Values and my_valuesSeenSoFar supposed to represent? How can you manipulate them in your class methods to make that happen? What should the initial values be? How many my_valuesSeenSoFar are there when a Grader is created? Would there be more after you add a score? Would there be more after you add an array of scores?

Can you describe what a Grader is and what it's members should be and do without writing code?
Ok...
I'm slowly understanding...

I can posterize what valuesSeenSoFar would do, but I am not quite sure where it might best be utilized in the implementation...

Maybe if there were some way to add the values collected in addScore AND addScores both into ValuesSeenSoFar?


I was able to take what you said and improve the grader.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
#include <iostream>
#define MAXSIZE 100
#include "Grader.h"

Grader::Grader( ){
my_Values [MAXSIZE] = 0;
my_ValuesSeenSoFar = 0;
}

void Grader::addScore( int score ){
	score = my_Values[MAXSIZE];
}
void Grader::addScores( int scores[], int size ){
	scores[size] = my_Values[MAXSIZE];
}
void Grader::clear(){
	my_Values[0];
}

int Grader::findBiggest() const{
	int i;
	for (i = 0; i < MAXSIZE; i++);
	my_Values[i];
}
int Grader::findSmallest( ) const{
//soon to be defined
}
Line 6, 11, 13 is accessing beyond the end of the array of my_Values and scores, as mentioned before.

Line 22 your for loop is empty.

It doesn't seem like you understand what the point of each of the variables is. Tell us what you think they do and we can help you understand their purpose.
I am unsure of how my_Values and my_valuesSeenSoFar are represented within the implementation

the score and scores are obviously each number and the int size is the size of the array
Also

does this improve the findBiggest?

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

#include <iostream>
#define MAXSIZE 100
#include "Grader.h"

Grader::Grader( ){
my_Values [MAXSIZE] = 0;
my_ValuesSeenSoFar = 0;
}

void Grader::addScore( int score ){
	score = my_Values[MAXSIZE];
}
void Grader::addScores( int scores[], int size ){
	scores[size] = my_Values[MAXSIZE];
}
void Grader::clear(){
	my_Values[0];
}

int Grader::findBiggest() const{
	int i;
	for (i = 0; i < MAXSIZE; i++);
	return my_Values[i];
}
int Grader::findSmallest( ) const{
}



EDIT:

because I am unsure of the variables-- i subsequently cannot accurately make a fore loop in the findBiggest

Last edited on
Topic archived. No new replies allowed.