Passing array elements to another for categorizing ?

I am trying to pass an array of student grade values into a 2nd array.
This 2nd array will compare the grade values and output a grade letter to the corresponding value. I must use 2 separate single dimension dynamic arrays

I was able to create the 1st array now I am stuck creating the 2nd.
How should I code it? the 2nd array has the grade categorizing so would i use a switch statement in that array?

I have posted the 1st array here.

#include <iostream>
#include<iomanip>
#include <cstdlib>
using namespace std;

int main()
{
int n;
cout << "Please enter size of array: ";
cin >> n;

//1 dim array for scores by random number gen
int* scoreArray = new int[n];
srand((unsigned)time(0));

cout << "Student No." << setw(11) << "Score"<< endl;
for (int i = 0; i <= n-1; i++)
{
scoreArray[i] = (rand() % 100) + 1;
cout << setw(7) << i << setw(13) << scoreArray[i] << endl;
}

// mean and stardard deviation will be used to help in grade letter finding
//Mean
double mean = 0.0;
double dSum = scoreArray[1];
for (int i = 0; i < n; ++i)
{
dSum += scoreArray[i];
}
mean = dSum / n;
cout << "\n\nMean= " << mean << endl;

//Standard Deviation
double sum_Deviation = 0.0;
double standard_Deviation = 0.0;
for (int i = 0; i <n; ++i)
{
sum_Deviation += (scoreArray[i] - mean)*(scoreArray[i] - mean);
}
standard_Deviation=sqrt(sum_Deviation /n);
cout << "Standard Deviation= " << standard_Deviation << endl;





//2nd array takes scores and outputs grade letters.
cout<<"Student no." <<setw(11)<<"Grade" <<endl;
// unsure what to do for this array



delete[] scoreArray; // deallocate array memory prevent memory leak
return 0;
}
Any help is greatly appreciated thank you for your time
Last edited on
You'll set up your second array the same way you did your first. Allocate memory for it then iterate through each element in it. I guess the question you need to ask yourself to finish is how are you going to determine a letter grade and how would you repeat that process in code?
I edited the original post. In order to determine the letter grade the score number will be compared against formulas using the mean and standard deviation code.

Example if score number< mean - ((3/2) * standard_dev) output Grade: F

Then do an if check for the other letters. one idea i had was to use a switch case.

But then I ran into the issue of how to pass my 1st arrays values into the second for letter output. and then where to place the switch.
Last edited on
You have to allocate space for your second array, iterate through the first, convert all its elements to letters, then store them in the second array. Let's say we were using a 90-80-70-60-50 grading scale:
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
//allocating space for second array
char gradeArray = new char[n];

//iterating through first
for (int i = 0; i < n; i++)
{
    //all these if statements convert numeric score to letter grade
    if ( scoreArray[i] >= 90 )
        //if the condition evaluates true, we store the corresponding letter grade in our second array
        gradeArray[i] = 'A';

    else if ( scoreArray[i] >= 80 )
        gradeArray[i] = 'B';

    else if ( scoreArray[i] >= 70 )
        gradeArray[i] = 'C';

    else if ( scoreArray[i] >= 60 )
        gradeArray[i] = 'D';

    else
        gradeArray[i] = 'F';
}

for (int i = 0; i < n; i++)
{
    //output results
}


I hope this can be used as an example and clears some things up. :3
Goodness yes thank you very much. I'll post again once i try out this new addition ^^. i've been working at this for 3-4 hours straight haha.

for the output portion is it possible to output the student number, the value and the grade all at once? like in a 3 column table?

Yeah. That's absolutely possible. You'd just add another column to the table you're already outputting, and since ith indexes match in both arrays, you can do it all in one for-loop.
Thank you for your help I was able to output it the way i needed here is my version of the grader loop. I'm unsure where i should place the delete [] commands. should i leave it here at the bottom? It's a shame arrays start at 0 otherwise i'd try to start my student numbers at 1.

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
//allocating space for second array
	char * gradeArray =  new char[n];
	//iterating through first array to gather student scores
	for (int i = 0; i < n; i++)
	{
		//all these if statements convert numeric score to letter grade
		if (scoreArray[i] <(mean-(1.5*standard_Deviation)) )
			//if the condition evaluates true, we store the corresponding letter grade
                         // in our second array
			gradeArray[i] = 'F';

	else if ((mean-(1.5*standard_Deviation) ) <= scoreArray[i]    && 
                    scoreArray[i]  < ( mean-(0.5*standard_Deviation)))
			gradeArray[i] = 'D';

		else if (   (mean-(0.5*standard_Deviation) ) <= scoreArray[i]   && 
                           scoreArray[i] < (mean+ (0.5*standard_Deviation) ))
			gradeArray[i] = 'C';

		else if ( (mean + (0.5*standard_Deviation)) <=scoreArray[i]    &&  
                           scoreArray[i]  <  (mean + (1.5*standard_Deviation)))
			gradeArray[i] = 'B';

		else if ((mean+(1.5*standard_Deviation))<= scoreArray[i])
			gradeArray[i] = 'A'; 
	}

	//output section
	for (int i = 0; i < n; i++)
	{
		cout << setw(7) << i << setw(13) << scoreArray[i]<<setw(5)<<gradeArray[i] << endl;
	}
	cout << "\n\nMean= " << mean << endl;
	cout << "Standard Deviation= " << standard_Deviation << endl;

	delete[] gradeArray;
	delete[] scoreArray;	// deallocate array memory prevent memory leak 
	system ("pause");
	return 0;
}

Last edited on
Typically you'll just delete when you're done needing that block of memory, so the end works in this case. If you wan t to start from "Student 1" you could do something cheeky like:
cout << setw(7) << i+1 << setw(13) << scoreArray[i]<<setw(5)<<gradeArray[i] << endl;
Wow I didn't know you could do that haha.
Keene do you know about doubly linked lists? I just posted my current code in the gen c++ forum for my project and it's a bit confusing haha.
Topic archived. No new replies allowed.