Conversion error on simple pointer program.

Hello!

I'm currently working on a simple program on pointers and encountered an issue.

The instruction for the program reads:

1
2
3
4
5
6
/*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.*/


So what I've done here is created a pointer to a double: arrayPtr.
I want to dynamically allocate an array with arrayPtr = new double[testNum]
however when I try to compile, I get an error line 32:
"cannot convert `double*' to `double**' for argument `1' to `void selectionSort(double**, int)'".

I've been trying to tweak things around for a while and I don't seem to be able to find out what
needs changing and/or what's wrong.

Any help is very much appreciated.


Here is my code:

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
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

void pause();
void selectionSort( double *[], int);
//int average();


int main()
{
	double *arrayPtr,
 	       average;
	
	int testNum;
	
	cout << fixed << showpoint << setprecision(1);
	cout << "\nEnter the number of tests: ";
	cin >> testNum;
	cout << "\n\n";
	
	arrayPtr = new double[testNum];
	
		for (int count = 0; count < testNum; count ++)
		{
			cout << "\tResult for test " << count + 1 << ": ";
			cin >> *(arrayPtr + count);
		}
	
	selectionSort(arrayPtr, testNum);
	
	cout << "\n\nResults in ascending order: ";
	
		for (int count = 0; count < testNum; count ++)
		{
			cout << arrayPtr << "   ";
			arrayPtr++;
		}
	
	//average(arrayPtr, testNum);
	
	cout << "\n\nAverage for results: "
		 << average;
	
	pause();
	
}

void selectionSort(double *arr[], int num)
{
	int startScan,
		minIndex;
		
	double *minValue;
	
		for (startScan = 0; startScan < (num - 1); startScan ++)
		{
			minIndex = startScan;
			minValue = arr[startScan];
			
				for (int index = (startScan + 1); index < num; index ++)
				{
					if (*(arr[index]) < *minValue)
					{
						minValue = arr[index];
						minIndex = index;
					}
				}
			
			arr[minIndex] = arr[startScan];
			arr[startScan] = minValue;
		}
}
		
//void average() {}
	
void pause()
{
	printf("\n%s","Press any key to continue");
	_getch();
	printf("\n\n");
	return;
}


Meanwhile I wish you all a great day.
Last edited on
void selectionSort( double *[], int);
What are you trying to pass to this function? An array of double? A pointer to an array of double?

Since you have a single dimensional array in main() it appears you want to pass this array to your function. Remember the following are equivalent:

1
2
void yourFunction(double  *array);
void yourFunction(double array[]);


Next why are you attempting to use pointer notation instead of the "normal" and clearer array notation:
1
2
cin >> *(arrayPtr + count);  // Pointer notation
cin >> arrayPtr[count];


Next:
1
2
3
4
5
6
void selectionSort(double *arr[], int num)
{
	int startScan,
		minIndex;
		
	double *minValue;

Where are you allocating memory for that pointer (minValue)? Wouldn't a normal double be adequate?

Why the subtraction in the comparison part of your for() loop in this function?

Lastly you should avoid mixing C-stdio functions with C++ streams. In a C++ program prefer C++ streams since they are less error prone.


jlb,

What are you trying to pass to this function? An array of double? A pointer to an array of double?


I was indeed trying to pass an array to the function and not a pointer to the array.


Next why are you attempting to use pointer notation instead of the "normal" and clearer array notation:


I am using the pointer notation as much as possible in order to practice them, as suggested
by the instructions for the program: "Use pointer notation rather than array
notation whenever possible." This program has no purpose for me other than getting familiar
with pointers and the way one uses them.


Where are you allocating memory for that pointer (minValue)? Wouldn't a normal double be adequate?


You're right a double makes more sense for minValue since I don't have to do
anything with its address.


Why the subtraction in the comparison part of your for() loop in this function?

The fonction selectionSort() sorts the different doubles contained in arrPtr[] in ascending order.
It goes through arrPtr[0] through arrPtr[ num-1 ] -with the for() loops- and assigns the first smallest
double to arrPtr[0], the second smallest double to arrPtr[1] etc.. I decided to run the loop only until
the second to last element of the array, since if all elements but one have been sorted in ascending
order, the last one will inevitably be the highest double in this case.


Lastly you should avoid mixing C-stdio functions with C++ streams. In a C++ program prefer C++ streams since they are less error prone.


Thank you for the advice about C-stdio functions, what else do you recommend using? (I only use them for my void pause() function in every program so far).

I managed to fix my code by the way, thanks to your remarks, here is the updated version:

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
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

void pause();
void selectionSort( double [], int);
//int average();


int main()
{
	double *arrayPtr;
 	
 	double average;
	
	int testNum;
	
	cout << fixed << showpoint << setprecision(1);
	cout << "\nEnter the number of tests: ";
	cin >> testNum;
	cout << "\n\n";
	
	arrayPtr = new double[testNum];
	
		for (int count = 0; count < testNum; count ++)
		{
			cout << "\tResult for test " << count + 1 << ": ";
			cin >> *(arrayPtr + count);
		}
	
	selectionSort(arrayPtr, testNum);
	
	cout << "\n\nResults in ascending order: ";
	
		for (int count = 0; count < testNum; count ++)
		{
			cout << *arrayPtr << "   ";
			arrayPtr++;
		}
	
	//average(arrayPtr, testNum);
	
	cout << "\n\nAverage for results: "
		 << average;
	
	pause();
	
}

void selectionSort(double arr[], int num)
{
	int startScan,
		minIndex;
		
	double minValue;
	
		for (startScan = 0; startScan < (num - 1); startScan ++)
		{
			minIndex = startScan;
			minValue = arr[startScan];
			
				for (int index = (startScan + 1); index < num; index ++)
				{
					if (arr[index] < minValue)
					{
						minValue = arr[index];
						minIndex = index;
					}
				}
			
			arr[minIndex] = arr[startScan];
			arr[startScan] = minValue;
		}
}
		
//void average() {}
	
void pause()
{
	printf("\n%s","Press any key to continue");
	_getch();
	printf("\n\n");
	return;
}



Thank you again!
Last edited on
Thank you for the advice about C-stdio functions, what else do you recommend using? (I only use them for my void pause() function in every program so far).

What's wrong with using cout? And instead of getch() why not just use cin.get()?



What's wrong with using cout? And instead of getch() why not just use cin.get()?

Um well I have to say that I feel stupid not having thought about that. I guess I just learned
to do it that way and never really thought about it. But thanks so much it's actually less tedious
to have one less #include to write every time!

Cheers, and have a great day!
Topic archived. No new replies allowed.