Test Scores #2 Modify the program of Programming Challenge 2 to allow the user to enter name-score pairs using two parralell arrays. For each student taking a test, the user types the student’s name followed by the student’s integer test score. Modify the sorting function so it takes an array holding the student names and an array holding the student test scores. When the sorted list of scores is displayed, each student’s name should be displayed along with his or her score. In stepping through the arrays, use pointers rather than array subscripts.

here is the code i have so far. please help.

``````#include <iostream>
#include <iomanip>

using namespace std;
void arraySelectSort(double *, int);
void showArrayPtr(double *, int);
void showAverage(double, int);
int main()
{
double *scores,
total = 0.0,
average = 0.0;

int numScores;

cout << "Please enter the number of test scores you would like to use";
cin >> numScores;

scores = new double[numScores];
if (scores == NULL)
return 0;

cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}

for (int count = 0; count < numScores; count++)
{
total += scores[count];
}

arraySelectSort(scores, numScores);

cout << "The test scores in ascending order are: \n";
showArrayPtr(scores, numScores);
showAverage(total, numScores);

delete[] scores;
return 0;
}
void totalaverage(double *, int)
{
}
void arraySelectSort(double *array, int size)
{
int startScan, minIndex;
double  minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index]  < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrayPtr(double *array, int size)
{
for (int count = 0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
void showAverage(double total, int numScores)
{
double average;

average = total / numScores;

cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;

system("pause");
}
``````

oh yea. sorry i forgot to cite the info. im not sure how we are supposed to use parralell arrays since it wasn't discussed in class. as for the code its very similar to what they have in the book too so i doubt the prof will mind. if ya could help that would be much appreciated.

here is what i got so far using tutorials based on parrallell arrays.

``````void getData(int testScore[], char studentName[]) {

int index;

cout << "Enter size for arrays: ";
cin >> size;

for (index = 0; index <= size - 1; index++)
{
cout << "Enter" << (index + 1) << "th student name: "
<< ": ";
cin >> studentName[size];
}

for (index = 0; index <= size - 1; index++)
{
cout << "Enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];

while (0 < testScore[index] || testScore[index] > 100) {
cout << "Re-enter test score for " << studentName[index]
<< ": ";
cin >> testScore[index];
}
}
}
``````

Anyhow your post is too close to "Will you fix my code?"

Avoid that and tell what line of code is causing trouble for you.

basically, the program is running fine now. but it isn't taking in the studentName data. the program crashes right after the score validation and average occurs.

So did you single step your code in your dev system? system("pause"); may not be such a good idea for portability.

the prof always asks us to use system pause. as for the program i have updated a lot of it and have only 5 errors now. we are supposed to use parralell arrays to design the program to sort scores acending order according to name user is prompted to enter. i am now having errors using strcpy_s. here is the code snippet.

``````void sortData(int size, char studentName[][6])
{
bool swap;
int temp[6];

do {
swap = false;
for (int count = 0; count < (size - 1); count++) {
if (strcmp(studentName[count], studentName[count + 1]) > 0) {
strcpy_s(temp, studentName[count]);
strcpy_s(studentName[count], studentName[count + 1]);
strcpy_s(studentName[count + 1], temp);
swap = true;
} // end if
} // end for
} while (swap);

}
``````

Read the error messages. Look at the strcpy_s requirements. How many arguments are you supposed to provide? How many do you you provide? Are they the same?

i took out the strcpy_s out and everything works fine. except for the fact that it still doesn't prompt the user to enter in the name for each test score.

As to system pause, that's a subject of many prior discussions. Example: http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong

The prof may be running folk through the mill, stamping out future programmers. The better ones learn to avoid this.

As why it doesn't prompt the user, it's your code so redesign, rewrite it so it does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.