Problem with dynamic arrays and pointers

I currently am having trouble getting a program to work that is supposed to create a dynamic array, pass it to a function that sorts it in ascending order, pass it to another function that calculates the average score, and should also output the list of sorted scores and the average. I've gotten what I hope to be a good sort function created and I believe I'm inputting right, but I guess I don't understand enough about pointers to see what I'm messing up here. It compiles and originally was giving me a glibc error, which I found out means that I'm messing up allocation of memory somewhere? I just need someone to see what I need to do to fix either the input of scores or allocation of memory for the array. It might also be a problem when I pass it to the 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
#include <iostream>

using namespace std;

int sortAscend(int arraySort[],int sortSize)
{
   int u, arraySwitch;//Variable to keep track of counts in the array
   bool sortCheck;//Checks to see if the sort is finished or not
   sortCheck = false;//Starts off at false
   
   do
   {
      sortCheck = false;
      //keeps it false through each iteration and will trigger an exit 
      
      for(u = 1; u < sortSize; u++)
      {
         if (arraySort[u-1] > arraySort[u])
         {
            arraySwitch = arraySort[u];
            arraySort[u] = arraySort[u-1];
            arraySort[u-1] = arraySwitch;
            sortCheck = true;
         }
      }
   }
   while(sortCheck);
   
   
}
      
      
      

int main()
{
   int* array = NULL;//Creates the integer that will later be an array
   int size;//Will store in the size of the array
   int loopCount;//Will keep count in the for loop
   int sortOut;//Used in the for loop for outputting the sorted array
   
   cout << "Please enter how many scores will be input: ";
   
   cin >> size;//Stores size of array
   
   array = new int[size];//Creates dynamic array
   
   cout << "\n" << "Please enter the scores seperated by spaces: ";
   
   for(loopCount = 0; loopCount < size; loopCount++)
   {
      cin >> array[loopCount];
   }
   
   sortAscend(&array[size],size);
   
   for(sortOut = 0; sortOut < size; sortOut++)
   {
      cout << array[sortOut] + " ";
   }
   delete [] array;
   array = NULL;
   return 0;
   
}
     
You have a couple of problems with your code.

First sortAscend(&array[size],size); should be sortAscend(array, size);.

Second what are you trying to do here: cout << array[sortOut] + " ";?
It probably should be just: cout << array[sortOut] << " ";

Close.

The 'array' is a pointer to an array. More specifically to first element of the array. The array has 'size' elements.

Dereferencing the pointer we can reach the individual elements / their values.

For example, array[0] is the first element
and array[size-1] is the last element.

Who is array[size] then? An out of range error, for we have no idea what is in that memory.

You, however, do not access that non-existent element in main(). You merely take its address &array[size]. That too could be a violation.

There is an another, "pointer math" syntax for dereferencing an element: *(array+size)
Taking address is thus: &(*(array+size))
But why dereference and then take address, when the address is already there before referencing: array+size


What goes wrong?

You give address array+size to a function that thinks that the address is of first element in size-long array. It is not. The first element of the array that you have allocated is at array.

Rewrite line 55: sortAscend( array, size );


Notes:

* You have two loops in main(). Both loop through the array. Why do they use different loop counter?

* NULL is old macro. Current C++ has nullptr that has subtle benefits.

*
1
2
3
int sortAscend(int arraySort[], int sortSize)
// means same as
int sortAscend(int * arraySort, int sortSize)

Oh and don't forget if you promise the compiler that a function will return a value, return a value.

Topic archived. No new replies allowed.