adding array problem

these are my problem, however I have a compiler error with this program.

it says that

array.cpp: In function ‘int main()’:
array.cpp:39: error: incompatible types in assignment of ‘int’ to ‘int [(((long unsigned int)(((long int)A) + -0x00000000000000001)) + 1)]’
array.cpp: In function ‘void array_add(const int*, int, int)’:
array.cpp:108: error: assignment of read-only location ‘*(a + ((long unsigned int)(((long unsigned int)i) * 4ul)))’

I am trying to fix it many ways, and I think the error on 108 is the one that main problem.

here's my program..


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

using namespace std;

const int SIZE(20);
int A(0);
  
// Function prototypes
void read_list(int a[], int & elements);
void print_array(const int a[], const int elements);
int find_max(const int a[], const int elements);
void array_add(const int a[], const int elements,int array_max);


int main()
{
  int elements(0);
  int max_array[A];
  int array[SIZE];

  read_list(array, elements);
	
  if (elements > 0)
  {
	// Display original list
	cout << endl << "Original list( "<< elements <<" values):"<< endl;
	print_array(array, elements);
	
	// Compute max
	max_array = find_max(array, elements);
	cout<<"the maximum value is "<<max_array<<endl;

	// Display modified list
	cout << endl << "modified list( "<< elements <<" values):"<< endl;
	print_array(array, elements);

  }
  else {
	cout << "The list is empty." << endl;
  }
  
  return 0;
}

// Function read_list
void read_list(int a[], int & elements)
{
  int input(0);
  
  // Read in list
  cout << "Enter non-negative numbers (ints) terminated by -99 " << endl;
  cin >> input;

  elements = 0;
  // Read in at most SIZE elements
  while (elements < SIZE && input != -99) {
    a[elements] = input;
    elements++;
    cin >> input;
  }
}

// print_array
void print_array(const int a[], const int elements)
{
	for (int i = 0; i < elements-1; i++) 
		{ 
			cout << a[i] << ","; 
		}
	
	for (int i = elements-1; i < elements; i++) 
		{ 
			cout << a[i] << ".";
		}	
	cout << endl;
}

//find max
int find_max(const int a[], const int elements)
{
	int array_max = a[0];	
	
	for (int i=1; i < elements; i++)
		{
			if(a[i] > array_max)
			{
				array_max = a[i];
			}			
		}
	return(array_max);
}

//add array
void array_add(int a[], const int elements, const int array_max)
{
	
	for (int i=0; i < elements; i++)
	{
		a[i]= a[i] + array_max;
	}
}
Last edited on
find_max() returns a single int, which on line 32 you're trying to assign to an array. max_array should probably be an int.
Topic archived. No new replies allowed.