Need help with a grade processor assignment using arrays..

Pages: 1234
So my teacher is giving us a week to do this and I dont really have time to read all the material in the text. Ive been skimming it. I need help as I'm not sure the order of things and how to do them. When the code executes its suppose to look like this:

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
 Welcome to the Grade Processor Application
    This app will generate the number of scores desired by the user
    and then perform and display several functions on this data.
    
    How many scores do you want to process: 100
    
    All scores:
       62   71   64   83   60   94   83   60   74   81
       98   70   73   25   80   84   74   74   78   75
       48  100   92   85   72   89   86   86   89   70
       89   75   68   79   72   96   73   73   18   73
       67   60   88   95   93   83   14   74   95   94
       76   89   30   78   91   78   61   32   75   77
       76   76   68   79   28   63   66   75   11   16
       62   84   73   91   81   71   78   64   77   72
       73   97   68    9   86   92   68   93   64   72
       91   77   80   74   90   18   87   71   90   90
    
    
    Average of all scores:                 72.47
    Number of scores above this average:   64
    Letter grade for all scores:           C
    
    Average of all scores without lowest:  73.11
    Number of scores above this average:   58
    Letter grade after dropping lowest:    C
    
    Minimum of all scores:                 9
    Maximum of all scores:                 100
    
    Displaying histogram for 100 scores:
    A: ******************
    B: *******************
    C: ************************************
    D: ****************
    F: ***********
    
    Score occurrence counts > 1:
      95:   2	  94:   2	  93:   2	  92:   2	  91:   3	
      90:   3	  89:   4	  86:   3	  84:   2	  83:   3	
      81:   2	  80:   2	  79:   2	  78:   4	  77:   3	
      76:   3	  75:   4	  74:   5	  73:   6	  72:   4	
      71:   3	  70:   2	  68:   4	  64:   3	  62:   2	
      60:   3	  18:   2	
    
    
    Thanks for using the Grade Processor application.
    
=============================================================================
=============================================================================


I know the use of arrays is needed, and she gives us hints as to what to use, but I am completely lost. She already gave us one function int generateOneScore() and the code the goes with it.This function uses the random number generator to generate a test score that falls into a somewhat typical distribution for grades.

She then gives us hints and headers...I just need help as to what goes in them..


Last edited on
Here are the hints..
void getNumberOfScores( int& )

One Parameters: type int, passed by reference
Description: Prompts the user to enter the number of scores that will be used during this execution of the application
USE: Caller invokes this function to get the number of scores to process at the beginning of the application's execution

void generateScores( int list[], int n )
Two Parameters: array of integers and number of integers currently stored in the array
Description: This function will write n scores into the array of scores named, list. The scores are generated one at a time by calling generateOneScore.
USE: Caller invokes this function to populate the array, list, with n scores

double calcAverage (int list[], int n, bool drop)

Three Parameters: array of integer scores, number of scores currently in list, and boolean flag, drop
Description: Calculate the average of the first n scores stored n the array. If drop is true, do not include the lowest score in this calculation. If drop is false, include all n scores. When this function needs to drop the lowest score, it should call the function, findLowest (see below). The average should be accurate to fractional values and NOT always be an integer: in other words, watch the expression you use to do the calculation of the average.
USE: Caller invokes this function to have computed the average of the first n elements of an array; may do so with or without lowest grade.

int findLowest(int list[], int n)
Two parameters: array of integers and an integer holding the number of values currently in the array
Description: searches the list to find the smallest (lowest or minimum) value, returns that value
USE: This function is called when the caller need to have the lowest value in the array returned.

int findHighest(int list[], int n)

Two parameters: array of integers and an integer holding the number of values currently in the array
Description: searches the list to find the highest (largest or maximum) value, returns that value
USE: This function is called when the caller need to have the highest value in the array returned.

int countAboveAverage (int list[], int n, double average)

Three Parameters: array of integers, number of integers stored in the array, and average of values in array
Description: count and return the number of values in the array that are above the average.
USE: Caller invokes this function to have computed the number of the first n scores of the list that are greater than the average of those n scores.
Note: This function does NOT call a function to calculate the average. The caller passes the value of the average in as the third parameter.

void displayScores(int list[], int n)
Two Parameters: array of integers and number of integers stored in the array
Description: print the n scores found in the array, list. Print 10 on each line. If n is not divisible by 10, then there should be fewer than 10 scores on the last line only. See example output.
USE: Caller invokes this function to display the contents of the array of scores

char findLetterGrade(double grade)

One Parameter: value of type, double
Description: determine the letter grade (type, char) for the numeric score, grade
USE: Caller invokes this function to determine the letter grade appropriate for a given numeric score

void displayHistogram( int list[], int n )
Two Parameters: array of integers and number of integers stored in the array
Description: Displays a histogram of all scores in terms of the letter grade for each score. The display heading states how many scores ( n ) are in the histogram, then displays one star for each score that is an A on line one of the histogram. On line two there should be one star for each score that is a B. Proceed this way for all five letter grades (A, B, C, D, F). Each line should begin with a label containing the letter grade for that line. See several examples in the example output. Make sure your output is exactly the same.
USE: Invoked by main to display the histogram of all scores


void displayDuplicateScores( int list[], int n )

Two Parameters: array of integers and number of integers stored in the array
Description: For all scores appearing in the list more than one time, this function displays those scores and their occurrence counts. See several examples in the example output.
USE: Invoked by main to display the occurrence counts for scores appearing in the list more than one time
Last edited on
Hello Dmtg93,

I would start by defining some variables in main followed by a call to "srand" to seed the RNG. If you are using Windows, I once read, that "srand" should be followed with a call to "rand();" before you actually use it in your program.

Then I would work on the functions "getNumberOfScores", "generateScores" and "displayScores". Once those are working properly you can add to "displayScores" when you need to and the other functions can be added as you need them.

The output you have shown will give you an idea of what to work on next.

The section that prints "all scores" is a good place to use "setw()" In the "cout" statement. "setw()" requires the header fie "iomanip"

Hope that helps,

Andy
Hello Dmtg93,

Did I scare you off or did you give up on the program? You have more information to write this program than most people get. What have you do so far?

Andy
Handy Andy wrote:
Did I scare you off

IMO your post was clear and helpful and couldn't scare anybody. Perhaps the OP wants to try by his/her own before asking for further help.
Did I scare you off or did you give up on the program? You have more information to write this program than most people get. What have you do so far?


Hi there sorry I have been trying to work on it. So far I have been trying to write code for each header(is that what they're called?)

I have this so far, written in Word Pad until I can bring everything together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

/****************************************************************
Function: getNumberOfScores(int&)
Description: getNumberOfScores prompts the user to enter the number of scores that will be used during this execution of the application
*******************************************************************
void getNumberOfScores( int &userNum) {

// possibly use do while loop?
do{
cout  <<  "How many scores do you want to process?"
cin >> userNum
while (userNum < 2 || userNum > 100)
{
cout << "ERROR: Enter a value in the range 2-100: ";
cin >> userNum;
} */

This one I believe to be okay right now, it's just asking for an input.

1
2
3
4
5
6
7
8
9
10
11
12
13
/**************************************************************
Function: generateScores( int list[], int n ) 
Description: This function will write n scores into the array of scores named, list. The scores are generated one at a time by calling generateOneScore.
******************************************************************

void generateScores( int list[], int n ) {
// write an array and call generateOneScore 100 times?

generateOneScore();
for (i=0; i<n; i++)
{
cout << list[i] << endl;
} */

This one I am having problems with right now. I need to call the function generateOneScore in here somewhere and have it generate n number of scores that the user inputs. I asked my teacher and she told me that after I call generateOneScore, I need to jump to the displayScores function to output the scores in the array.

I haven't really worked on my main function, I just wanted to focus on all the other ones before I attempt to edit main.

Last edited on
Also here is what is in gernateOneScore

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
/////////////////////////////////////////////////////////////////////////////
// This function uses the random number generator to generate a test score
// that falls into a somewhat typical distribution for grades.  To use this
// function, first, randomly seed the generator in your main function once
// and then call this function as needed. Seed by calling:  srand(time(0))
/////////////////////////////////////////////////////////////////////////////
int generateOneScore() {
   const int A = 14;  // ~14% of scores are A
   const int B = 39;  // ~25% of scores are B
   const int C = 75;  // ~36% of scores are C
   const int D = 89;  // ~14% of scores are D
   const int F = 100; // ~11% of scores are F
   const int MIN_A = 90;
   const int MIN_B = 80;
   const int MIN_C = 70;
   const int MIN_D = 60;
   
   // generate a number bewteen 1 and 100, inclusive
   // to determine which letter grade to generate
   int whichLetter = rand() % 100 + 1;  // 1..100
   
   // Set min and max based on the letter grade
   int min, max;
   if (whichLetter <= A) {
      min = MIN_A;
      max = 101;
   }
   else if (whichLetter <= B) {
      min = MIN_B;
      max = MIN_A;
   }
   else if (whichLetter <= C) {
      min = MIN_C;
      max = MIN_B;
   }
   else if (whichLetter <= D) {
      min = MIN_D;
      max = MIN_C;
   }
   else {
      min = 0;
      max = MIN_D;
   }
   // Generate a random score for the chosen letter grade
   int score = rand() % (max-min) + min;
   return score;
}
Hello Dmtg93,

That is some progress. You have thought of one part with the "getNumberOfScores" function that I did not think of at first, and yes it is function not a header.

A header file is a file with an extension of ".h", usually C header file, ".hpp", a good extension for a C++ header file, or a file name like "iostream", with no extension, which is a C++ header file.

Your "getNumberOfScores" function is the right concept, but the wrong approach. The "do" does not have a matching "while" and is not needed. And yu are missing some semi-colons at the end of several lines. The "while" part works, so I think this would work better:

1
2
3
4
5
6
7
8
9
10
11
void getNumberOfScores(int &userNum)
{
	cout << "\n How many scores do you want to process? ";  // <--- Notice the "\n " and extra space at the end. Just looks better
	cin >> userNum;

	while (userNum < 2 || userNum > 100)
	{
		cout << "\n ERROR: Enter a value in the range 2-100: ";
		cin >> userNum;
	}  //  End while
}  //  End function 


The "generateScores" function is partly right. The comment "// write an array and call generateOneScore 100 times?" should be "// write an array and call generateOneScore n times." And the line cout << list[i] << endl; needs to be list[i] = geerateOneScore as the comment suggests.

Your function "generateOneScore" is more involved than I first wrote, but you have more information to work with than I have and I did not know you wanted to do it this way, so I will have to load up that code and see how it works out.

Arrays are zero based starting with the first element being zero. This became a problem for me when I wrote the function to print the the array. So instead of starting my array at element zero I started at element one and made the array one larger I have since thought about using a counting variable to achieve the ten numbers per line, but have yet to work on that yet. The code to print ten numbers per line is the easy part, starting at zero is the hard part. Just a thought for now.

Hope that helps,

Andy
Okay, thank you I updated the generateScores function!

generateOneScore was provided for us to use as a freebie. I'm trying to keep up with the reading and the notes but they still aren't making any sense and arrays aren't sticking that well. This assignment is all about arrays haha

Here is what I have so far for displayScores..
1
2
3
4
5
6
7
8
9
10
11
/***************************************************************
Function: displayScores(int list[], int n)
Description:  print the n scores found in the array, list. Print 10 on each line. If n is not divisible by 10, then there should be fewer than 10 scores on the last line only. See example output.
*****************************************************************************
void displayScores(int list[], int n){
for (int i = 0; i < n;) {
  cout << list[i]=generateOneScore << " ";
  if ((i + 1) % 10 == 0) {
    cout << endl;
  }
} */

I'm pretty sure this is saying that it wants to generate a new line every 10 scores, but if there is a remainder on the next line, lets say there's 6 scores on the last line..will that line still print the 6 scores?
Hello Dmtg93,

I will ahve to test this code, but I think it should generate a new line every 10. Although line 7 is wrong. You do not need to generate a score every time through the for loop. This part should have already been done earlier in the program. To start with you just need cout << list[i] << " ";.

And yes if there are only six scores on the last line they will print.

Hope that helps,

Andy
Andy,

Awesome thank you for the feedback! I'll update displayScores!

Any idea on how to start double calcAverage?
Hello Dmtg93,

I tested the "displayScores" function and fount that the for loop is written incorrectly. The third parameter is missing, so it just creates an endless loop. And I had to change the "cout" statement as I showed you earlier. After that it worked except that the format of the output did not match your OP, but it is a start.

Andy
Andy,

So I changed the code..maybe something like this?

1
2
3
4
5
6
7
8
9
10
11
/***************************************************************
Function: displayScores(int list[], int n)
Description:  print the n scores found in the array, list. Print 10 on each line. If n is not divisible by 10, then there should be fewer than 10 scores on the last line only. See example output.
*****************************************************************************
void displayScores(int list[], int n){
for (int i = 0; i < n; i++) {
  cout << list[i] << " ";
  if ((i + 1) % 10 == 0) {
    cout << endl;
  }
} */
Last edited on
Hello Dmtg93,

For the "calcAverage" function Consider this

define a variable to keep track of the total of all grades
define a variable for min and set it = to a call to "findLowest" function
a for loop to traverse the list array with
an if statement to deal with the variable "drop"
if true continue
else add to total variable
based on the value of "drop" divide total variable by the correct number of scores
return the average

This should give you a start.

Hope that helps,

Andy

Edit: Your update "displayScore" function is better. Now it does what it should.
Last edited on
Andy,

define a variable for min and set it = to a ca to "findLowest" function
a for loop to traverse the list array with
an if statement to deal with the variable "drop"


What does this mean? Define min like int min and then set min= findLowest?
Hello Dmtg93,

Yes. Sorry about the spelling sometimes my keyboard sticks and I do not catch it.

The way I did the my program I defined "min" in the "calcAverage" function where I used it. Then I used the functions "findLowest" and "findHighest" when I needed a minimum and maximum number later in the program.

it might look something like this: int min = findLowest(aGrades, numOfGrades);. Where "aGrades" is what you call "list". I know you were given some hints, but it does not mean you can not change the variable names to something more meaningful

Hope that helps,

Andy
Last edited on
No worries!

So here is what I have..
1
2
3
4
5
 double calcAverage (int list[], int n, bool drop){
int total
int min =  findLowest(list,n);
int max=findHighest(list,n);
for (

I know you already said I need a for loop and an if statement inside that so would it look like

for (int i = 0; i < n; i++) {

if(drop==1){
cout <<total<< endl;

EDIT: This is what I have now..not sure what to do with bool and how to include it..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 double calcAverage (int list[], int n, bool drop){
int sum=0;
double average=0.0;
int min =  findLowest(list,n);
int max=findHighest(list,n);
for (int i = 0; i < n; i++) {
sum+=list[i];
average= double(sum)/n;
return average;

}

if(drop==1){
cout <<total<< endl;



}
Last edited on
Hi Andy and Dmtg93,
I am actually working on the same assignment, and struggling with the function generateScores(). This seems to be the most important function because the rest deal with the results of it. What I have is this:

1
2
3
4
5
6
7
8
9
void generateScores( int list[], int n ) {
    int score;              
    int generateOneScore(int score);  // calling for one score before the loop
    for (int i = 0; i<n; i++)
    {
        
        list [i] = generateOneScore();   // not sure what the argument should be
    }
}


From what I understand, I need to populate each element of list[] with a score generated from generateOneScore() n times. So, I call generateOneScore (which returns a single int score) each time before the loop that populates array list. I am having troubles populating that list in this line list [i] = generateOneScore(); . I am not sure how to draw the argument from the function generateOneScore to properly populate list.
Hello Dmtg93,

Before you added the "Edit" and new code this is a good start:

1
2
3
4
5
6
7
8
for (int i = 0; i < n; i++)
{

    if(drop==1)
    {
        cout <<total<< endl;  // <--- Not needed here.
    }
}


The code you posted after the "Edit" has some correct code, but some lines are in the wrong place.

Think about line 4. "drop" is defined as a bool therefore it has two possible answers false or true, i.e., 0 or 1. Therefore you can just say if (drop) and it works the same as "drop == 1".

Also the if statement is half complete. If drop is true what else would have to be true in order to skip an element of the array? Then what would you use to skip that iteration of the for loop? And what would you put in the else part of the if/else?

Lines 8 and 9 need to be done outside of the for loop. And line 9 will end the function before lines 13 and 14 can be executed. Lines 13 and 14 need to be inside the for loop as an if/else.

Hope that helps,

Andy

Edit: I got ahead of myself and forgot to mention that at the end of the function "calcAverage" you will need to determine how many numbers to divide by based on the value of "drop".

The return statement could make use of the "Conditional ternary operator ( ? )", see http://www.cplusplus.com/doc/tutorial/operators/#conditional , thus using a single line to return the correct answer.

Last edited on
@airek

Do not jump into someone topic just because it is similar to what you are working on. It will get confusing trying to follow two conversations at the same time. Start your own question and I will look for it. That said:

Lines 2 and 3 are not needed in this function. Line 3 is a function proto type or forward declaration that should be done outside of the function or in a header file.

Line 7 is correct "generateOneScore returns an int which "list[i]" is set = to. If this is not working then you have done something else wrong that I have not seen yet.

Hope that helps,

Andy
Pages: 1234