-1

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");
}
3
Contributors
10
Replies
48
Views
1 Year
Discussion Span
Last Post by rproffitt
Featured Replies
  • 1

    This doesn't look like your code. It looks to be from 7 years ago at https://www.daniweb.com/programming/software-development/threads/186020/help-using-dynamic-memory-allocation-and-pointers Rather than passing this off as your work, why not share that up front? Then tell the forum what is eluding you. Your post sounds as if you want others to do your work … Read More

  • 1

    I see tutorials with https://www.google.com/search?q=utorials+based+on+parrallell+arrays&ie=utf-8&oe=utf-8#q=tutorials+based+on+parallel+arrays+c%2B%2B 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. Read More

0

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.

0

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];
            }
        }
    }
0

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.

0

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

0

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);

}
0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.