Trouble with arrays: Finding the lowest number

Some notes before I describe my problem.

I am writing a gradebook program.

The setup for the program is this :

A teacher has 5 students who have taken 4 tests each. The teacher uses a basic grading scale to assign letter grades to the students based on their average of these 4 tests.

scale :
90-100 --- A
80-89 --- B
70-79 --- C
60-69 --- D
0-59 --- F

my conditions are as followed :
1. uses an array of string objects to hold student names
2. uses an array of characters that will hold the letter grade(the letter only, ex.A,B,C)
3. uses five arrays of four doubles to hold each student's set of test scores.

4. user should input each name and each test score.
5. calculate the average of each student's test scores and be given a letter grade based on the average.

End conditions.

My problem.

As you will see by the below code, I'm halfway through the program and I'm already stuck.

The part I am stuck on is the findLowest condition.
I understand the concept of looping through each student in the array, but it gets hazy when I also need to loop through their grades and pick out the lowest one. The way I have done this in the past was to use a lot of IF statements, but I was not using arrays then. Right now it is a way longer than expected loop. (the "student number and grade: " part in the findLowest function was me trying to experiment on what information was being sent)

I'm not asking for anyone to write the program for me.
What I'm looking for is a helpful bump in the right direction.

Also, I am very new to writing code so please pick out any areas of improvement that you see with my style or setup.

Any and all help will be greatly 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
#include <iostream>
#include <iomanip>

using namespace std;

const int NUM_TESTS = 4, STU_NUM = 5, STU_LENGTH = 20;

void getNames(char [][STU_LENGTH]);
//void calcAverage();
//void display();
void getScores(char [][STU_LENGTH], char [], double [][NUM_TESTS]);
void findLowest(double tests[][NUM_TESTS]);



int main()
{
    char names[STU_NUM][STU_LENGTH];
    double tests[STU_NUM][NUM_TESTS];
    char gradeLetter[STU_NUM];
    
    getNames(names);
    getScores(names,gradeLetter,tests);
    findLowest(tests);
    
    system("PAUSE");
    return 0;
    
}

// Function that will ask for and store names

void getNames(char names[][STU_LENGTH])
{
    for(int i=0;i < STU_NUM;i++)
    {
        cout << "Please enter student " << i+1 << " name : ";
        cin.getline(names[i],STU_LENGTH);
    }    
}


// Function that will ask for and store scores for each student

void getScores(char names[][STU_LENGTH], char gradeLetter[], double tests[][NUM_TESTS])
{
  

  for(int i = 0; i < STU_NUM; i++)
  {
     
     for(int j = 0; j < NUM_TESTS; j++)
     {
        cout << "Please enter " << names[i] << " grades";
        cout << (j+1) << ": ";
        cin >> tests[i][j];
     }
     
 }   

}

// Function that will find each student's lowest grade and drop it.
// Will pass to the calcAverage function.

void findLowest(double tests[][NUM_TESTS])
{
    for(int i = 0;i < STU_NUM;i++)
    {
        cout << "This is student: " << i;
        
        for(int j=0;i < NUM_TESTS;j++)
        {
          cout << "student number and grade: "  << tests[i][j];
        } 
    }
}   
Find lowest element of array:

1) Store arr[0] in variable, lowest.
2) for int i = 1 to size - 1
if arr[i] < lowest, lowest = arr[i]

3) lowest now equals the smallest element of the array.

Only takes one loop.
or you can #include <algorithm> and use the function sort( array name, arrayname + size) to sort your array
Wouldn't I need two loops?

One to loop through the 5 students and one to loop through their 4 grades?

And what did you mean for your 2) (for) loop? , ResidentBiscuit?

like...
for(int i=1;size - 1;i++)

??
Well, The good news is that I made some progress.

The bad news is that I got stuck again when I add up the test scores. The weird thing is that everything adds up correctly for every student EXCEPT the first set of grades entered.

Why is this happening? I'm losing my mind.


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

using namespace std;

const int NUM_TESTS = 4, STU_NUM = 5, STU_LENGTH = 20;

void getNames(char [][STU_LENGTH]);
void getScores(char [][STU_LENGTH], char [], double [][NUM_TESTS]);
void findLowest(double tests[][NUM_TESTS], double lowTests[]);
void calcAverages(double tests[][NUM_TESTS],double lowTests[]);
//void display();





int main()
{
    char names[STU_NUM][STU_LENGTH];
    double tests[STU_NUM][NUM_TESTS];
    char gradeLetter[STU_NUM];
    double lowTests[STU_NUM];
    
    getNames(names);
    getScores(names,gradeLetter,tests);
    findLowest(tests, lowTests);
    calcAverages(tests,lowTests);
    
    cout << "\n";
    system("PAUSE");
    return 0;
    
}

// Function that will ask for and store names

void getNames(char names[][STU_LENGTH])
{
    for(int i=0;i < STU_NUM;i++)
    {
        cout << "Please enter student " << i+1 << " name : ";
        cin.getline(names[i],STU_LENGTH);
    }    
}


// Function that will ask for and store scores for each student

void getScores(char names[][STU_LENGTH], char gradeLetter[], double tests[][NUM_TESTS])
{
  

  for(int i = 0; i < STU_NUM; i++)
  {
     
     for(int j = 0; j < NUM_TESTS; j++)
     {
        cout << "Please enter " << names[i] << " grades";
        cout << (j+1) << ": ";
        cin >> tests[i][j];
     }
     
 }   

}

// Function that will find each student's lowest grade and drop it.
// Will pass to the calcAverage function.

void findLowest(double tests[][NUM_TESTS], double lowTests[])
{
    
    
    for(int i=0;i < STU_NUM;i++)
    {
        lowTests[i] = tests[i][0];
        
        for(int j=1;j < NUM_TESTS;j++)
        {
            if(lowTests[i] > tests[i][j])
            {
                lowTests[i] = tests[i][j];
            }
        }
    }             
}
    
void calcAverages(double tests[][NUM_TESTS],double lowTests[])
{
    double averages[STU_NUM];
    double tempSum[STU_NUM];
    

    cout << "The test scores are: ";
    
    for(int i=0;i < STU_NUM;i++)
    {
        for(int j=0;j < NUM_TESTS;j++)
        {
            tempSum[i] += tests[i][j];
        }    
    }        
           
    cout << "The sums are: ";
    for(int p=0;p < STU_NUM;p++)
    {
        cout << tempSum[p] << "  ";
    }  
    
}
tempSum[i] += tests[i][j];

You never actually assigned any values to the array tempSum, so what is likely occuring is that it is creating an unexpected value at tempSum[i], then using that value for the first operation you perform. Hence why the first grade entered is always flawed- it's adding up all of the grades plus whatever value it decided to stick in there to begin with.
Topic archived. No new replies allowed.