NO clue what I'm doing wrong- Arrays + Pointers

I have no idea what the heck I'm doing. This isn't homework by the way, I'm doing this to study ahead of class. Basically I have to make it so that this program can take a user-inputted set of numbers and put them in ascending order. I did it before w/out functions but there's something up with how my functions are set up, any and all help is appreciated.

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

	 // Function prototypes
	 void arrSelectSort(int [], int);
	 void showArray(const int[], int);
	 void showArrPtr(int *[], int);

	 int main()
	 {
		 int NUM_DONATIONS; // no longer const
		 cout << "How many donations?" << endl;
		 cin >> NUM_DONATIONS; //take input

		 // An array containing the donation amounts. -- dynamically allocated with new[]
		 double * donations = new double[NUM_DONATIONS]; // Allocate memory
		 
		 cout << "Enter the donations: \n";
		 for (int x = 0; x < NUM_DONATIONS; x++) //this goes exactly the same as before
		 {
			 cout << "Donations #" << (x + 1) << ": ";
			 cin >> donations[x];
		 }


		 //sort the elements of the array to pointers
		 arrSelectSort(donations, NUM_DONATIONS);

		 //Display the donations using the array of pointers. This
		 //will display them in sorted order.
		 cout << "The donations, sorted in ascending order are: \n";
		 showArrPtr(donations, NUM_DONATIONS);

		 //display the donations in their original order.
		 cout << "The donations, in their original order, are: \n";
		 showArray(donations, NUM_DONATIONS);
		 system("pause");
		 return 0;
	 }

	 

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

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

	 

	 void showArrPtr(int * arr[], int size)
	 {
		 for (int count = 0; count < size; count++)
			 cout << *(arr[count]) << " ";
		 cout << endl;
	 }

	 
closed account (z05DSL3A)
First thing:

1
2
3
void arrSelectSort(int [], int);
void showArray(const int[], int);
void showArrPtr(int *[], int);


All take an array of ints (or a pointer to same) but donations is an array of doubles.

Last edited on
Ok I fixed that (thanks, can't believe I didn't notice that).

I'm getting these errors:

2 IntelliSense: argument of type "double *" is incompatible with parameter of type "double ** 990
Error 1 error C2664: 'void showArrPtr(double *[],int)' : cannot convert argument 1 from 'double *' to 'double *[]' 990 1




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

	 // Function prototypes
	 void arrSelectSort(double [], int);
	 void showArray(double[], int);
	 void showArrPtr(double *[], int);

	 int main()
	 {
		 int NUM_DONATIONS; 
		 cout << "How many donations?" << endl;
		 cin >> NUM_DONATIONS; 

		 
		 double * donations = new double[NUM_DONATIONS];
		 
		 cout << "Enter the donations: \n";
		 for (int x = 0; x < NUM_DONATIONS; x++)
		 {
			 cout << "Donations #" << (x + 1) << ": ";
			 cin >> donations[x];
		 }


		
		 arrSelectSort(donations, NUM_DONATIONS);

		 
		 cout << "The donations, sorted in ascending order are: \n";
		 showArrPtr(donations, NUM_DONATIONS);

	
		 cout << "The donations, in their original order, are: \n";
		 showArray(donations, NUM_DONATIONS);
		 system("pause");
		 return 0;
	 }


	 void arrSelectSort(double arr[], int size)
	 {
		  double temp;
		  bool swap;
		  
		 	 do
		  { 
		   swap = false;
		   for (int count = 0; count < size-1; count++)
		 	  {
			   if (arr[count] > arr[count + 1])
		 		  {
				   temp = arr[count];
				   arr[count] = arr[count + 1];
				   arr[count + 1] = temp;
		 		  swap = true;
		 		  }
		 	  }
		   } while (swap);
	 }



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

	 

	 void showArrPtr(double * arr[], int size)
	 {
		 for (int count = 0; count < size; count++)
			 cout << *(arr[count]) << " ";
		 cout << endl;
	 }
closed account (z05DSL3A)
void showArrPtr(double *[],int), the double *[] is effectively a pointer to a point to a double.

In showArray(donations, NUM_DONATIONS); donations is a pointer to a double, so you need to take the address of donations (put a & in front of it, read it as take the address of the variable)
showArray(&donations, NUM_DONATIONS);
Last edited on
God, thanks a lot! I think I need to go re-study this chapter cause I really don't get this. I just have one last problem, not an error, but the "original order" is showing up as addresses, not values. How can I fix this/What's the issue?

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

	 // Function prototypes
	 void arrSelectSort(double [], int);
	 void showArray(double *[], int);
	 void showArrPtr(double [], int);

	 int main()
	 {
		 int NUM_DONATIONS; 
		 cout << "How many donations?" << endl;
		 cin >> NUM_DONATIONS; 

		 
		 double * donations = new double[NUM_DONATIONS];
		 
		 cout << "Enter the donations: \n";
		 for (int x = 0; x < NUM_DONATIONS; x++)
		 {
			 cout << "Donations #" << (x + 1) << ": ";
			 cin >> donations[x];
		 }


		
		 arrSelectSort(donations, NUM_DONATIONS);

		 
		 cout << "The donations, sorted in ascending order are: \n";
		 showArrPtr(donations, NUM_DONATIONS);

	
		 cout << "The donations, in their original order, are: \n";
		 showArray(&donations, NUM_DONATIONS);
		 system("pause");
		 return 0;
	 }


	 void arrSelectSort(double arr[], int size)
	 {
		  double temp;
		  bool swap;
		  
		 	 do
		  { 
		   swap = false;
		   for (int count = 0; count < size-1; count++)
		 	  {
			   if (arr[count] > arr[count + 1])
		 		  {
				   temp = arr[count];
				   arr[count] = arr[count + 1];
				   arr[count + 1] = temp;
		 		  swap = true;
		 		  }
		 	  }
		   } while (swap);
	 }



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

	 

	 void showArrPtr(double  arr[], int size)
	 {
		 for (int count = 0; count < size; count++)
			 cout << arr[count] << " ";
		 cout << endl;
	 }
closed account (z05DSL3A)
One thing to remember is that void Foo(int []); is the same as
void Foo(int *);.

So void Foo(int *bar[]) is the same as void Foo(int **bar). Here you have two levels of indirection, so you would need two levels of dereferencing to get to the value, something like **(bar + index) or *bar[index].

Sorry if this doesn't make much sense, I've been up 20hrs on 4hrs sleep.
Hmmm....I'll read this over again later!! Thanks a bunch for all your help, :^)
Topic archived. No new replies allowed.