Comparing values of array after adjusting values

Hi,I have an array called int totalscores[300] that is sorted in descending, so it ranges from highest scores to lowest. How would I attach or link a grade according to what position the scores are in the array?

After linking the total scores to the respective grade, I want to :

1. Adjust the total scores
2. Sort the adjusted total scores in descending order
3. And then compare the grade before and after the scores were adjusted to see how many students were promoted and demoted to a different grade after the adjustment was done by seeing what position the grades are after, so for ex.

If there's 5 scores,

300 , 200 ,100, 90, 43, 20

I would like to link it as
300 - A
200 - B
100 - C
43 - D
20 - F

and then apply an adjustment on the total score, while retaining the linked grade, so perhaps the 300 becames 149, the 200 becomes 320, etc.

149 - A
320 - B
100 - C
50 - D
60 - F

and then sort the scores descending:

320 - B
149 - A
100 - C
60 - F
50 - D

and then compare the values to before it was adjusted and see how many scores got promoted or demoted from A,B,C,D,F.

So far, I thought of making a char array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char grades[300];
for (int i = 0; i<45; i++)
{
    grades[i] = 'A'
}
for (int i = 46; i<104; i++)
{
    grades[i] = 'B'
}
for (int i = 105; i<179; i++)
{
    grades[i] = 'C'
}
for (int i = 180; i<239; i++)
{
   grades[i] = 'D'
}
for (int i = 240; i<299; i++)
{
   grades[i] = 'F'
}


But, I'm stuck on how I be able to link the char array to the int totalscores[300]?
And then after that, I'm not sure how to adjust the scores, while retaining the link and then sorting it again, and comparing it to the original link.

So far, I just have 3 arrays: grades[300], totalscores[300], newt[300].
grades[300] and totalscores[300] correspond to each other via the index.
totalscores[300] and newt[300] correspond to each other via the index as well, but my only missing piece is sorting the newt[300] while keeping track of what grade it initially got.

Last edited on
Hello!

Well, one solution would be to use a dual-selection sort. Meaning you are sorting both your grades - as well as your letter-grades. Here is how this could look like:

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
void sortScores(int *testScores, char *letterGrades, const int numScores)
{
   int startScan = 0,
       minIndex = 0,
       minElem = 0,
       index = 0;

   char tempGrades = ' ';
 
   for (startScan = 0; startScan < (numScores - 1); startScan++)
   {
      minIndex = startScan;
      minElem = *(testScores + startScan);
      tempGrades = *(letterGrades + startScan);

      for (index = startScan + 1; index < numScores; index++)
      {
         if (*(testScores + index) < minElem)
         {
            minElem = *(testScores + index);
            tempGrades = *(letterGrades + index);
            minIndex = index;
         }
      }

      *(testScores + minIndex) = *(testScores + startScan);
      *(letterGrades + minIndex) = *(letterGrades + startScan);
      *(testScores + startScan) = minElem;
      *(letterGrades + startScan) = tempGrades;
   }
}


This would sort both in ascending order. So, whenever you would change - something - you would call the function, then output the result.

Edit: You also don't need so many for loops just to get your letter grades.

You will only ever have your five letter grades 'A', 'B', 'C', 'D', 'F'. You need a switch statement, and the range of scores to which each letter grade should be assigned to.
Last edited on
Topic archived. No new replies allowed.