Confusing errors in array

Hello, I was wondering if anyone had any ideas for how to fix the errors that I am getting.
The code compiles correctly, but there are a few error messages when I run it in Visual Studio.
It says that the variables 'largest' and 'smallest' are being used without initializing. If you ignore the error message, it runs fine.

The other problem I am having is that in the "highest" function, I am getting a strange number....the debugger is giving me the correct numbers for average and lowest, but the highest is coming back as -8.58993e.

Thank you for your time, it's a huge help.

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
86
87
88
89
                                                                      

#include <iostream> 
using namespace std;

double average (double*, int);
double highest (double*, int);
double lowest (double*, int);

int main ()
{

    int numTestScores;
    int count;
    double *testScorePtr;
    double testAverage;
    double testHighest;
    double testLowest;
    
    cout << "How many test scores will you enter? \n";
    cin >> numTestScores;
    
    while (numTestScores < 0)
     {
        cout << "The number can't be negative. Please enter another number.\n";
        cin >> numTestScores;
     }
     
     testScorePtr = new double [numTestScores];
    
    cout << "Enter the test scores below.\n";
    	for (count = 0; count < numTestScores; count++)
    	{
    		cout << "Test " << (count + 1) << ": ";
    		cin >> *(testScorePtr + count);
        
        	
       		while (*(testScorePtr + count) < 0)
        	{
        		// Get input again.
            	cout << "Please enter a valid test score.\n";
            	cin >> *(testScorePtr + count);
        	}
    	}

     
     testAverage = average(testScorePtr, numTestScores);
     testHighest = highest(testScorePtr, numTestScores);
     testLowest = lowest(testScorePtr, numTestScores);
     
     
     cout << "The average of those scores is:  " << testAverage << endl;
     cout << "The highest of those scores is:  " << testHighest << endl;
     cout << "The lowest of those scores is:  " << testLowest << endl;
 }
 
 double average (double* score, int numScores)
 {
 	double avg;
    double total = 0.0;
	for (int count = 0; count < numScores; count++)
	{
		total += score[count];
	}
    avg = total / numScores;
	return avg;
 }
 
 double highest (double* score, int numScores)
 {
   double largest, count;
   for (count = 1; count < numScores; count++)
		{
			if (score[count] < largest)
			largest = score[count];
		}
return largest;
 }
 
 double lowest (double* score, int numScores)
 {
      double smallest, count;
   for (count = 1; count < numScores; count++)
		{
			if (score[count] > smallest)
			smallest = score[count];
		}
return smallest;
 }   
Had some sign errors with < and > also count needs to be an int not a double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double highest(double* score, int numScores)
{
	double largest = score[0];
	for (int count = 1; count < numScores; count++)
	{
		if (score[count] > largest)
			largest = score[count];
	}
	return largest;
}

double lowest(double* score, int numScores)
{
	double smallest = score[0];
	for (int count = 1; count < numScores; count++)
	{
		if (score[count] < smallest)
			smallest = score[count];
	}
	return smallest;
}


Also don't forget to delete what you new.

int main(){ returns 0;}
Last edited on
The uninitialized variables are a serious error - it means those variables contain garbage and will very likely give incorrect results. The "strange" number you get is a direct consequence of that.

There are a couple of other issues too. The array subscript count must be an integer, not a floating-point type. The first element of the array is ignored in functions highest() and lowest(). The < and > signs are used the wrong way round (lines 74 and 85).

You can fix both the uninitialised variable and the ignoring of the first element by setting the initial value equal to the first array element.
1
2
3
4
5
6
7
8
9
10
11
double highest (double* score, int numScores)
{
    double largest = score[0];
    int count;
    for (count = 1; count < numScores; count++)
    {
        if (score[count] > largest)
            largest = score[count];
    }
    return largest;
}

... and similarly for the other function.
Last edited on
Topic archived. No new replies allowed.