Fill array using function. Error: assignment of read only location

i am trying to populate a presorted array (sorted while as it is being populated). I get the error below. referrring to lines 63 and 66

In function ‘int makeSortArray(const int*, int)’:
error: assignment of read-only location ‘*(arr + ((long unsigned int)(((long

Can someone tell me why I'm getting this error? please?
Does this have to do with passing by reference or anything related?

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
#include <iostream>
#include <cmath>

using namespace std;
const int arraySize =10;
const int sentinel = 0;

int index;
int arr[arraySize]; //array as array index  
int count = 0;  
int sum = 0;
double median=0;



//function prototypes
int makeSortArray(const int arr[], int count);
void output(const int arr[], int count);
void revOutput (const int arr[], int count);


int main ()
{   
  makeSortArray(arr, count);
  output(arr, count);
  
  return 0;
}  

  //1. function for input
int makeSortArray (const int arr[], int count)
{
  int pos = 0;
  bool found = false;
  int num;
    cout << "Enter up to 10 positive integers, 0 to quit: \n";

cin >> num;
while(num != sentinel)
{
  if (count == arraySize)    
    cout << "You have 10 digits, more entries will be ignored. Enter 0 to quit";
  else if (num<0)
    cout << "Please enter a positive interger or 0 to quit" << endl;
  else
  {  // find the insert position. 
	      while(!found&&pos<count)
	      {
		      if(arr[pos]<=num)
		      {
			      pos++;
		      }
		      else
		      {
			      found=true;
		      }
	      }
	      found=false;//reset the found
	      //move arr[pos]~arr[count-1]to arr[pos-1]~arr[count]
	      int i=count;
	      while(i>pos)
	      {
		      arr[i]=arr[i-1];  // ERROR POINTS HERE
		      i--;
	      }
	      arr[pos]=num;//insert the positive integer// ERROR POINTS HERE
	      pos=0;//reset the pos;
	      count++;//the number of input add one
      }    
  cin >> num;   

}
      
  
}


//2. function to output the array to screen
void output(const int arr[], int count)
{
  cout << "Output Function: \n ";
  for (index=0; index<arraySize; index++)
    cout << arr[index] <<" " << endl;
}
Last edited on
arr is const, this makes it non-modifiable. change

int makeSortArray (const int arr[], int count)
to
int makeSortArray (int arr[], int count)
Topic archived. No new replies allowed.