Pointers and Dynamic Arrays

I have been working on a homework problem for my C++ class and have been having trouble. The problem asks to create a program with a dynamically allocated array to hold test scores. The scores must be sorted and the average of them must be calculated. Then both the sorted list and the average should be displayed.

My program compiles without error but when I run the program and put in two test scores values, say 100 and 90, the program returns my test scores as 0 and my average as 0. I think the problem has to do with how I am using the pointers. I am not sure exactly what data I am manipulating.

Any help would be appreciated. Thanks.

#include <iostream>

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using namespace std;

void sortArray(double [], int);
void showArray(double [], int);

int main()
{
    double *scores,      // To dynamically allocate an array
            total = 0.0, // Accumulator
            average;     // To hold average test score
    int numTests,        // To hold number of tests
        count;           // Counter

    // Get the number of scores
    cout << "\nEnter the number of test scores: ";
    cin >> numTests;

    // Dynamically allocate an array large enough to hold that many tests
    scores = new double [numTests];

    // Get the scores for each test
    cout << "\nEnter the test scores:\n";
    for (count = 0; count < numTests; count++)
    {
        cout << "Test " << (count + 1) << ": ";
        cin >> scores[numTests];
        while (scores[numTests] < 0)
        {
            cout << "Invalid test score. Enter only positive test scores." << endl;
            cout << "Test " << (count + 1) << ": ";
            cin >> scores[numTests];
        }
    }

    // Call function to sort array
    sortArray(scores, numTests);

    // Calculate the total of the scores
    for (count = 0; count < numTests; count++)
    {
        total += scores[count];
    }

    // Calculate the average of the test scores
    average = total / numTests;

    // Display results
    cout << "\nTests scores: ";
    showArray(scores, numTests);
    cout << "Average score: " << average << endl;

    // Free dynamically allocated memory
    delete [] scores;
    scores = 0;
    return 0;
}

void sortArray(double array[], int size)
{
    int temp;
    bool swap;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (array[count] > array[count + 1])
            {
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    }
    while (swap);
}

void showArray(double array[], int size)
{
    for (int count = 0; count < size; count++)
        cout << array[count] << " ";
    cout << endl;
}


Enter the number of test scores: 2  

Enter the test scores:
Test 1: 100
Test 2: 90

Tests scores: 0 0 
Average score: 0
logout

[Process completed]

Tell me whats wrong with Line 23-33
Last edited on
cin >> scores[numTests];
Try changing this line (well, theres 2 technically) to:
cin >> scores[count];
It seemed kind of mean to me to out right tell him. It's kind of like shouting "Your Fly Is Down!" in front of someone they like you know? Pointing out the obvious oversight?

For a detailed explaination the way you had it written you kept assigning the input to the same point in the array because the value of numTests never changed.
Ah, of course! Thank you.
Topic archived. No new replies allowed.