Dynamic array passed to function by pointer

Howdy all,

Thanks for helping out and taking a look at this with me.

I'm currently using MS visual studio 2010 and am getting these compile errors:

Error 3 error LNK1120: 2 unresolved externals

Error 1 error LNK2019: unresolved external symbol "void __cdecl showArray(float *,int)" (?showArray@@YAXPAMH@Z) referenced in function _main

Error 2 error LNK2019: unresolved external symbol "void __cdecl sortArray(float *,int)" (?sortArray@@YAXPAMH@Z) referenced in function _main

I'm not sure why I'm getting these errors.

Just for context, here is the assignment prompt:

Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.
Input Validation: Do not accept negative numbers for test scores.


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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <iomanip>
using namespace std;


 // Function prototypes
void mainMenu();
float calcAverage(float, float, int);
void sortArray(float *, int);
void showArray(float *, int);

int main()
{
	int numScores = 0;
	float *testScores = new float[numScores]; //allocate storage space for the array
	float  average = 0.0,
		   total = 0.0;

	mainMenu();


	cout << "Please enter how many tests you would like to score: ";
	cin >> numScores;
	while (numScores < 0)
	{
		cout << "\n\nError! Please enter a positive number: ";
		cin >> numScores;
	}
	
	cout << "\n\nPlease enter the scores for each test: \n";
	for (int i = 0; i < numScores; i++)
	{
		cout << "\nTest Score: ";
		cin  >> testScores[i];
	
		while (testScores[i] < 0.0 || testScores[i] > 100.0)
		{
			cout << "\nYou cannot enter a score higher than 100 or lower than 0!";
			cin.clear();
			cin.ignore();
			cin >> testScores[i];
		}  
		total += testScores[i];
	}   //end for loop
	
	// Get the average
	calcAverage(total, average, numScores);
	system("cls");
	cout << "The average of all the scores is: " << average
		 << "\n\nThe sorted test scores are as follows: \n\n";

	// Sort the values
	sortArray(testScores, numScores);	

	// Display the scores
	cout << "The sorted values are:\n";
	showArray(testScores, numScores);

	delete [] testScores;    //de-allocate storage space for the array

	cout << "\n\n\n\t\tThanks for using this Test Scores program!"; 

	system("pause");

	return 0;
}
//scratch paper:
//how many test scores are you entering?
//cin >> test scores to array
//assign all negatives a value of 0
//sort them in ascending order
//calculate the average score
//display the sorted list and the average
//use pointer notation, not array notation

//////////////////////////////////////////////////////////////////////////////////////////////

/*********************************************************
 *                     Main Menu						*
 *  this is the main menu function. as a void function, *
 *              it does not return any value			*
*********************************************************/
void mainMenu()
{
	system("cls");
	cout << "\n\n..................................................\n"
		 << "Welcome to the Test Scores #1 Program!\n"
		 << "..................................................\n\n";
}


 /*************************************************************
 *					 calcAverage							 *
 * This function displays calculates the average of all the  *
 *			scores inputted by the user						 *
 *************************************************************/
float calcAverage(float total, float average, int numScores )
 {
	average = total / numScores;
	return average;
 }

 /************************************************************
 *						sortArray							*
 * This function performs an ascending-order bubble sort on *
 * array. The parameter size holds the number of elements   *
 *					in the array.							*
 ************************************************************/
 void sortArray(float *testScores[], int numScores)
 {
	 float *temp;
	 bool swap;

	 do
	 { swap = false;
		 for (int count = 0; count < (numScores - 1); count++)
		 {
			if (*testScores[count] > *testScores[count + 1])
			{
			 temp = testScores[count];
			 testScores[count] = testScores[count + 1];
			 testScores[count + 1] = temp;
			 swap = true;
			}

		 }
	 } while (swap);
 }

 /*************************************************************
 *						showArray							 *
 * This function displays the contents of array. The		 *
 * parameter size holds the number of elements in the array. *
 *************************************************************/
 void showArray(float *testScores[], int numScores)
 {
	 for (int count = 0; count < numScores; count++)
	 {
		 cout << testScores[count] << " ";
		 cout << endl;
	 }
 }
These are your function declarations:
9
10
void sortArray(float *, int);
void showArray(float *, int);

Here is how you've defined them:
109
110
void sortArray(float *testScores[], int numScores)
{
135
136
void showArray(float *testScores[], int numScores)
{

See the difference?
Thanks for the fast response. I've changed the code and it now compiles, but crashes when it gets to the 'calcAverage' function:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <iomanip>
using namespace std;


 // Function prototypes
void mainMenu();
float calcAverage(float, float, int);
void sortArray(float *, int);
void showArray(float *, int);

int main()
{
	int numScores = 0;
	float *testScores = new float[numScores]; //allocate storage space for the array
	float  average = 0.0,
		   total = 0.0;

	mainMenu();


	cout << "Please enter how many tests you would like to score: ";
	cin >> numScores;
	while (numScores < 0)
	{
		cout << "\n\nError! Please enter a positive number: ";
		cin >> numScores;
	}
	
	cout << "\n\nPlease enter the scores for each test: \n";
	for (int i = 0; i < numScores; i++)
	{
		cout << "\nTest Score: ";
		cin  >> testScores[i];
	
		while (testScores[i] < 0.0 || testScores[i] > 100.0)
		{
			cout << "\nYou cannot enter a score higher than 100 or lower than 0!";
			cin.clear();
			cin.ignore();
			cin >> testScores[i];
		}  
		total += testScores[i];
	}   //end for loop
	
	// Get the average
	calcAverage(total, average, numScores);
	system("cls");
	cout << "The average of all the scores is: " << average
		 << "\n\nThe sorted test scores are as follows: \n\n";

	// Sort the values
	sortArray(testScores, numScores);	

	// Display the scores
	cout << "The sorted values are:\n";
	showArray(testScores, numScores);

	delete [] testScores;    //de-allocate storage space for the array

	cout << "\n\n\n\t\tThanks for using this Test Scores program!";

	system("pause");

	return 0;
}
//scratch paper:
//how many test scores are you entering?
//cin >> test scores to array
//assign all negatives a value of 0
//sort them in ascending order
//calculate the average score
//display the sorted list and the average
//use pointer notation, not array notation

//////////////////////////////////////////////////////////////////////////////////////////////

/*********************************************************
 *                     Main Menu						*
 *  this is the main menu function. as a void function, *
 *              it does not return any value			*
*********************************************************/
void mainMenu()
{
	system("cls");
	cout << "\n\n..................................................\n"
		 << "Welcome to the Test Scores #1 Program!\n"
		 << "..................................................\n\n";
}


 /*************************************************************
 *					 calcAverage							 *
 * This function displays calculates the average of all the  *
 *			scores inputted by the user						 *
 *************************************************************/
float calcAverage(float total, float average, int numScores )
 {
	average = total / numScores;
	return average;
 }

 /************************************************************
 *						sortArray							*
 * This function performs an ascending-order bubble sort on *
 * array. The parameter size holds the number of elements   *
 *					in the array.							*
 ************************************************************/
 void sortArray(float *testScores, int numScores)
 {
	 float temp;
	 bool swap;

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

		 }
	 } while (swap);
 }

 /*************************************************************
 *						showArray							 *
 * This function displays the contents of array. The		 *
 * parameter size holds the number of elements in the array. *
 *************************************************************/
 void showArray(float *testScores, int numScores)
 {
	 for (int count = 0; count < numScores; count++)
	 {
		 cout << testScores[count] << " ";
		 cout << endl;
	 }
 }
1
2
	int numScores = 0;
	float *testScores = new float[numScores]; //allocate storage space for the array 
¿and how much space that will be?
Topic archived. No new replies allowed.