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;
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.
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:
//allocating space for second array
char gradeArray = newchar[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';
elseif ( scoreArray[i] >= 80 )
gradeArray[i] = 'B';
elseif ( scoreArray[i] >= 70 )
gradeArray[i] = 'C';
elseif ( 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
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.
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;