Getting error, plz help?

I'm still getting one error not sure why?
Error 1 error C4700: uninitialized local variable 'arranged' used 74
Thanks!

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>
#include <iomanip>
using namespace std;

//Function prototypes
void sortTestScores(int *TestScores, int size_Test);
double avgTestScore(int *TestScores, int size_Test);
void printTestScores(int *TestScores, int size_Test);
int main()
{
//Define variables
int *TestScores;
int size_Test, score;
double average;
//Get the number of test scores you want to average
cout << "How many test scores do you want to enter?";
cin >> size_Test;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new int[size_Test];
//Get test scores
cout << "Enter " << size_Test << " positive test scores below:" << endl;
for (int i=0; i<size_Test; i++)
{
//Display score
cout << "Score " << i + 1 << ": "; 
cin >> score;
// Input validation. Only numbers between 0-100
while (score<0 || score>100)
{
cout << "You must enter a score that is positive" << endl;
cout << "Please enter again: ";
cin >> score;
}
TestScores[i]=score;
}
//Dsiplay the results
cout << "Test Scores: ";
printTestScores(TestScores, size_Test);
sortTestScores(TestScores, size_Test);
cout << "The test scores, sorted in ascending order, are: \n";
printTestScores(TestScores, size_Test);
average = avgTestScore(TestScores, size_Test);
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;
system ("pause");
return 0;
}
//Accepts a dynamic array of test scores and size of array, then sorts in ascending order
//Sort function implementation
void sortTestScores(int *TestScores, int size_Test)
{
int *last=TestScores+size_Test; //get last location of array
for (int *first = TestScores; first < last-1; first++)
{
	for(int *next=first+1; next<last; next++) 
	{
		if (*next<*first)
		{
		int temp=*first;
		*first=*next;
		*next=temp;
		}
	}
}
}
//calculates and returns average of test scores
double arrAvgScore (double *TestScores, int size_Test)
{
int total = 0; 
double average;
int *arranged;
for (int i=0; i < size_Test; i++)
{
total+= *arranged;
arranged++;
}
average= double(total) / size_Test;
return average;
}
//Prints test scores stored in array
void printTestScores(int *TestScores, int size_Test)
{
int *arranged=TestScores;
for (int i=0; i < size_Test; i++)
{
cout << *arranged << " " << arranged << endl;
       arranged++; 
}
}

Last edited on
On line 71 you create a pointer, pointing at some random memory containing some random value.

On line 74, you read that random memory and use that random value.

This is very, very bad. Do not read random memory. The OS will stop you with a segFault, and even if it doesn't, you have no idea what value is in it.
Oddly, function arrAvgScore() is unused, it is never referenced anywhere in the rest of the code.
Ok, I changed it to avgTestScores but I still don't know what to change the arranged too though?
Do I not need a pointer there?
Last edited on
If a function is never called, it hardly matters what it is named.

However, a more conventional version of the function might be something like this:
1
2
3
4
5
6
7
8
9
10
// Calculates and returns average of test scores
double arrAvgScore (int *TestScores, int size_Test)
{
    double total = 0; 
    
    for (int i=0; i < size_Test; i++)
        total += TestScores[i];
    
    return total / size_Test;
}


Last edited on
so then how would I print that? do I just use TestScores++ or something llke that?
Last edited on
so then how would I print that?

Well, you already are printing the average,
double average; // line 14

42
43
44
average = avgTestScore(TestScores, size_Test);
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;

though one school of thought recommends to avoid declaring an uninitialised variable as at line 14. Better to wait until you are ready to give it a value.

do I just use TestScores++ or something llke that?

Sorry, I don't see what you're getting at here.
ok, I just initialed it to TestScores but I'm getting different errors:
Error 2 error LNK1120: 1 unresolved externals 1
Error 1 error LNK2019: unresolved external symbol "double __cdecl avgTestScore(int *,int)" (?avgTestScore@@YANPAHH@Z) referenced in function _main

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
90
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Function prototypes
    void sortTestScores(int *TestScores, int size_Test);
    double avgTestScore(int *TestScores, int size_Test);
    void printTestScores(int *TestScores, int size_Test);
    
    int main()
    {
    //Define variables
    int *TestScores;
    int size_Test, score;
    double average;
    //Get the number of test scores you want to average
    cout << "How many test scores do you want to enter?";
    cin >> size_Test;
    //Dynamically allocate an array large enough to hold that many scores
    TestScores = new int[size_Test];
    //Get test scores
    cout << "Enter " << size_Test << " positive test scores below:" << endl;
    for (int i=0; i<size_Test; i++)
    {
    //Display score
    cout << "Score " << i + 1 << ": "; 
    cin >> score;
    // Input validation. Only numbers between 0-100
    while (score<0 || score>100)
    {
    cout << "You must enter a score that is positive" << endl;
    cout << "Please enter again: ";
    cin >> score;
    }
    TestScores[i]=score;
    }
    //Dsiplay the results
    cout << "Test Scores: ";
    printTestScores(TestScores, size_Test);
    sortTestScores(TestScores, size_Test);
    cout << "The test scores, sorted in ascending order, are: \n";
    printTestScores(TestScores, size_Test);
    average = avgTestScore(TestScores, size_Test);
    cout << fixed << showpoint << setprecision(2);
    cout << "The average of all the test scores is " << average << endl;
    system ("pause");
    return 0;
    }
    //Accepts a dynamic array of test scores and size of array, then sorts in ascending order
    //Sort function implementation
    void sortTestScores(int *TestScores, int size_Test)
    {
    int *last=TestScores+size_Test; //get last location of array
    for (int *first = TestScores; first < last-1; first++)
    {
    	for(int *next=first+1; next<last; next++) 
    	{
    		if (*next<*first)
    		{
    		int temp=*first;
    		*first=*next;
    		*next=temp;
    		}
    	}
    }
    }
    //calculates and returns average of test scores
    double avgTestScores(int *TestScores, int size_Test)
    {
    int total = 0; 
    double average;
    int *arranged=TestScores;
    for (int i=0; i < size_Test; i++)
    {
    total+= *arranged;
    arranged++;
    }
    average= double(total) / size_Test;
    return average;
    }
    //Prints test scores stored in array
    void printTestScores(int *TestScores, int size_Test)
    {
    int *arranged=TestScores;
    for (int i=0; i < size_Test; i++)
    {
    cout << *arranged << " " << arranged << endl;
           arranged++; 
    }
    }

you only declare double avgTestScore(int *TestScores, int size_Test);, but haven't define it. You have to define it in another file or in this file.
Last edited on
thanks, I got it to work but I'm getting an out like this though:
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
Enter 6 positive test scores below:
Score 1: 89
Score 2: 94
Score 3: 100
Score 4: -78
You must enter a score that is positive
Please enter again: 78
Score 5: 84
Score 6: 65
Test Scores:
89 004BB260
94 004BB264
100 004BB268
78 004BB26C
84 004BB270
65 004BB274
The test scores, sorted in ascending order, are:
65 004BB260
78 004BB264
84 004BB268
89 004BB26C
94 004BB270
100 004BB274
The average of all the test scores is 85.00
Press any key to continue . . .
Last edited on
The output seems ok. right?
I'm getting these stuff: 004BB260 that shouldn't be there.
Should be:
Test Scores:
89
94
100
78
84
65
The test scores, sorted in ascending order, are:
65
78
84
89
94
100
The average of all the test scores is 85.00
cout << *arranged << " " << arranged << endl;
here you print the score which "arranged" points to and the pointer address of "arranged".
hoping this will help you.
Last edited on
oh, Thanks!
Topic archived. No new replies allowed.