Array minimum

I need to exclude the minimum of the 4 test scores that are inputted so that the output average is without it.

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

//User Libraries

//Global Constants - Math/Physics Constants, Conversions,
//                   2-D Array Dimensions

//Function Prototypes
char letterG(float score){
        if(score >= 90 && score <=100)
            return 'A';
        else if(score >= 80 && score <=90)
            return 'B';
        else if(score >= 70 && score <=80)
            return 'C';
        else if(score >= 60 && score <=70)
            return 'D';
        else
            return 'F';
    }
//Execution Begins Here
int main(int argc, char** argv) {
    //set random number seed
    
    //Declare Variables
    string names [5];
    float scores [5][4];
    char grades[5];
    float testAv[5];
    float score;
    
    for(int i=0;i<5;i++){
        cout<<"\nEnter "<<(i+1)<<" student "<<" name:   ";
        cin>>names[i];
        cout<<"Enter his or her 4 test scores:  ";
        for(int j=0;j<4;j++){
            cin>>scores[i][j];
            while(scores[i][j]>100||scores[i][j]<0){
                cout<<"\nTest score should be between 0 & 100"<<endl;
                cout<<"Enter test score:  ";
                cin>>scores[i][j];
            }
        }
    }
    for(int i=0;i<5;i++){
        testAv[i] = 0;
        for(int j=0;j<4;j++)
            testAv[i] += scores[i][j];
        testAv[i] /=4;
        grades[i] = letterG(testAv[i]);
        
    }
    
    
    
    cout<<"\nStudent Name Average Score Letter Grade"<<endl;
    for(int i=0;i<5;i++){
        cout<<names[i]<<"        "<<testAv[i]<<"         "<<grades[i]<<endl;
    }
    
    //Initialize Variables
    
    //Process/Map inputs to outputs
    
    //Output data
    
    //Exit stage right!
    return 0;
}
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
#include <iostream>
#include <string>
#include <iomanip>

const std::size_t NSCORES = 4 ;
using scores_array = double[NSCORES] ;

double minimum_score( const scores_array& scores )
{
    double min_score = scores[0] ;
    for( std::size_t i = 0 ; i < NSCORES ; ++i )
        if( scores[i] < min_score ) min_score = scores[i] ;
    return min_score ;
}

double average_without_min_score( const scores_array& scores )
{
    static_assert( NSCORES > 1 ) ; // there must be at least two scores

    // sum up all the scores
    double sum = 0 ;
    for( double v : scores ) sum += v ;

    sum -= minimum_score(scores) ; // remove the minimum score
    return sum / (NSCORES-1) ; // and return the average without it
}

char letter_grade( double average_score )
{
    if( average_score >= 90 ) return 'A' ;
    else if( average_score >= 80 ) return 'B' ;
    else if( average_score >= 70 ) return 'C' ;
    else if( average_score >= 60 ) return 'D' ;
    else return 'F' ;
}

int main()
{
    const std::size_t NSTUDENTS = 5;

    std::string names[NSTUDENTS] { "Johnny Bull", "Robert Cherry", "Frank Nugent",
                                   "Hurree Singh", "Harry Wharton" } ;

    scores_array scores[NSTUDENTS] { { 40, 80, 90, 80 }, { 50, 40, 70, 80 },
                                     { 95, 90, 40, 90 }, { 70, 80, 90, 50 },
                                     { 60, 50, 40, 50 } } ;

    // TO DO: add code to accept names and scores from stdin
    //        note: may need to add validation that scores are within range

    std::cout << "Greyfriars School, Friardale, Kent\n"
              << "----------------------------------\n\n"
              << std::fixed << std::setprecision(2) ;

    for( std::size_t i = 0 ; i < NSTUDENTS ; ++i )
    {
        const double avg_score = average_without_min_score( scores[i] ) ;
        std::cout << std::setw(3) << i+1 << ". "
                  << std::setw(15) << names[i]
                  << std::setw(7) << avg_score
                  << std::setw(3) << letter_grade(avg_score) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/673b7007dc53e4dc
I need it to work with my code, and i cant use doubles only floats
Hello iRatedRetro,

This program looks like a class assignment based on the comments in the code. If so the full assignment would be helpful.

As I see your program you have not followed the comments that are in the program.

One of the comment //Process/Map inputs to outputs . Is this a "std::map" or the 2D array? I lean to the 2D array based on the code.

That brings the next question //Output data . Output to what a file or the screen?

I fixed up your code and added some comments in the program:
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
#include <iostream>
#include <limits>

using namespace std;

//User Libraries  // looks like it should be line 1

//Global Constants - Math/Physics Constants, Conversions,
//                   2-D Array Dimensions
// <--- Where are they?

//Function Prototypes
// <--- I see a function, but not prototype. This generally means that the function comes after main or in another file.

char letterG(float score)
{
	if (score >= 90 && score <= 100)
		return 'A';
	else if (score >= 80 && score <= 90)
		return 'B';
	else if (score >= 70 && score <= 80)
		return 'C';
	else if (score >= 60 && score <= 70)
		return 'D';
	else
		return 'F';
}

//Execution Begins Here
int main(int argc, char** argv)  // <--- I do not see where you are using 'argc" or "argv". Unless you have a
                                 //      future use they are not needed.
{
	//set random number seed
	// <--- Do you need this aand where is it? You do not have the proper header files either. <ctime> and <cstdlib>

	//Declare Variables
	string names[5];
	float scores[5][4];
	char grades[5];
	float testAv[5];
	float score;

	for (int i = 0; i<5; i++)
	{
		cout << "\nEnter " << (i + 1) << " student " << " name:   ";
		cin >> names[i];
		cout << "Enter his or her 4 test scores:  ";
		for (int j = 0; j<4; j++)
		{
			cin >> scores[i][j];

			while (scores[i][j]>100 || scores[i][j]<0)
			{
				cout << "\nTest score should be between 0 & 100" << endl;
				cout << "Enter test score:  ";
				cin >> scores[i][j];
			}
		}
	}

	for (int i = 0; i<5; i++)
	{
		testAv[i] = 0;
		for (int j = 0; j<4; j++)
			testAv[i] += scores[i][j];
		testAv[i] /= 4;
		grades[i] = letterG(testAv[i]);

	}



	cout << "\nStudent Name Average Score Letter Grade" << endl;

	for (int i = 0; i<5; i++)
	{
		cout << names[i] << "        " << testAv[i] << "         " << grades[i] << endl;
	}

	//Initialize Variables
	// <--- What is this for? Should be higher in the program.

	//Process/Map inputs to outputs
	// <--- What is this for? And what do you intend?

	//Output data
	// <--- Output to the screen or a file?


	// <--- You may find this useful.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	//Exit stage right!
	return 0;
}

My hope is that this will help you understand what is wrong.

There are many here who could fix your problem, but that will not help if the whole of the program is wrong.

It would nice to see what you started with before you added your code.

Hope that helps,

Andy
So this is the question

PART ONE
Grade Book
A teacher has five students who have taken four tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her four test scores.

Test Score L etter Grade
90–100 A
80–89 B
70–79 C
60–69 D
0–59 F
Write a program that uses an array of string objects to hold the five student names, an array of five characters to hold the five students’ letter grades, and five arrays of four double s to hold each student’s set of test scores.
The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average. Input Validation: Do not accept test scores less than 0 or greater than 100.


THIS IS THE QUESTION I AM WORKING ON NOW↓↓↓↓↓I DID WHAT IS ABOVE ALREADY
PART TWO
Grade Book Modification
Modify the grade book application in Programming Challenge 13 so it drops each student’s lowest score when determining the test score averages and letter grades.
Hello iRatedRetro,

You have a good start to your program, but some of it is wrong. I would not scrap the program you have, but rearrange it better.

First I would start with:
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
//User Libraries

//Global Constants - Math/Physics Constants, Conversions,
//                   2-D Array Dimensions
// 2D array dimensions.
constexpr size_t MAXROWS{ 5 };
constexpr size_t MAXCOLS{ 4 };

//Function Prototypes  // <-- Leads one to believe there is more than one prototype.

//Execution Begins Here
int main()  // <--- I do not see where you are using 'argc" or "argv". Unless you have a future use they are not needed.
{
	//set random number seed
	// <--- Do you need this and where is it? You do not have the proper header files either. <ctime> and <cstdlib>

	//Declare Variables

	//Initialize Variables

	//Process/Map inputs to outputs. Do you mean process the 2D array?

	//Output data


	// <--- You may find this useful.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	//Exit stage right!
	return 0;
}

// <--- Put functions here. 


I would start with this and put your code where it is needed. The code for inputting the data is OK works. I would also consider putting this code in a function and just call it from main.

The for loops for the average I would put in a function then copy it for a second function to drop the lowest score and return that average.

Your function for "letterG()", but if I may suggest I would call it "LetterGrade()" or "GetLetterGrade" as it is more descriptive of what it does. Notice how the function name starts with a capital letter. For me this is a way to tell you that it is a function and not a variable which should start with a lower case letter.

Your if/else if statements are on the right track, but off a bit. Looking at the code a score of 90 could be either an "A" or "B". Not what you want. look at what is required:
1
2
3
4
5
6
Test Score Letter Grade
90–100 A
80–89 B
70–79 C
60–69 D
0–59 F

The >= 90 followed by <= 90 does not make any sense. The if/else if statements would work better as:
1
2
3
4
if      (score > 89 && score <= 100)   return "A";
else if (score >79 && score < 90)      return "B";
else if (score > 69 && score < 80)     return "C";
// <--- The rest here. 

Also look at JLBorges's approach. It is shorter and will work the same as above. It is worth considering

In a previous message you said and i cant use doubles only floats. But your instructions clearly start five arrays of four doubles to hold each student’s set of test scores. That says to me that the 2D array should be of type "double".

Your other variables are OK and I did not find anything wrong with them. I did add one variable of type "double" I called "minScore" that I used when dropping the lowest score before the average. I put this in main, but if you put the code for this average in a function only the function will need this variable.

I have my ideas on this program that I will work on. If there is anything that you have a question on let me know.

Hope that helps,

Andy
Hello iRatedRetro,

Just to help you along we know that the code for input works, so instead of having to type something every time you test the program I set this up for testing:
1
2
3
4
5
6
7
8
9
string names[MAXROWS]{ "Andy", "Bob", "Jane", "John", "Sue" };
double scores[MAXROWS][MAXCOLS]
{
	{90.0 ,95.0, 100.0, 98.0},
	{70.0, 90.0, 85.0, 89.0},
	{80.0, 82.0, 85.0, 88.0},
	{90.0, 92.0, 95.0, 98.0},
	{75.0, 77.0, 79.0, 81.0}
};

Followed by commenting out the code for input.

Just a tip to make testing easier along with the advantage that the advantage that the output will produce the same numbers during testing. Figuring the answers with a calculator will let you know when the program works correctly.

Hope that helps,

Andy
Topic archived. No new replies allowed.