So this is my first post on here and I'm looking for some help since my professor never responds to emails. I have to write a program that prompts and reads a list of non-negative numbers (ints) into an array, prints the array, finds the maximum value in the array, adds the maximum valueto each array number and then prints the maximum number and the modified array.The input list is terminated by a -99. I came up with the following code so far.
// Function prototypes
void read_list(int array[], int & elements, const int SIZE);
void print_array(const int array[], const int elements);
int find_max (const int array[], const int elements);
void array_add(int arrayMax[], int array[], const int elements);
int main()
{
int elements(0);
int arrayMax[0];
const int SIZE(20);
int array[SIZE];
read_list(array, elements, SIZE);
if (elements >= 0)
{
// Display original list
cout << endl << "Original list: ( "<< elements <<" numbers):"<< endl;
print_array(array, elements);
// Compute max
arrayMax = find_max(array, elements);
cout<<"The maximum value = "<< arrayMax <<endl;
// Add max toeach value in the array
array_add(arrayMax, array, elements);
// Display modified list
cout << endl << "Modified list ( "<< elements <<" numbers):"<< endl;
print_array(array, elements);
}
else {
cout << "The list is empty." << endl;
}
return 0;
}
// Function read_list
void read_list(int array[], int & elements, const int SIZE)
{
int numbers(0);
// Read in list
cout << "Enter non-negative numbers (ints) terminated by -99 " << endl;
cin >> numbers;
elements = 0;
// Read in at most SIZE elements
while (elements < SIZE && input != 0) {
array[elements] = numbers;
elements++;
cin >> numbers;
}
}
// print_array
void print_array(const int array[], const int elements);
{
for (int i = 0; i < elements-1; i++)
{
cout << array[i] << ", ";
}
for (int i = elements-1; i < elements; i++)
{
cout << array[i] << ".";
}
cout << endl;
}
//find max
int find_max(const int array[], const int elements);
{
int arrayMax = a[0];
for (int i=1; i < elements; i++)
{
if(array[i] > arrayMax)
{
arrayMax = a[i];
}
}
return(arrayMax);
}
//add the max to each value
void array_add(int arrayMax[], int array[], const int elements);
{
for (int i=0; i < elements; i++)
{
array[i]= array[i] + arrayMax;
}
}
When I try to compile the program I get these error messages:
array.cpp: In function ‘int main()’:
array.cpp:24:14: error: storage size of ‘arrayMax’ isn’t known
int arrayMax[];
^
array.cpp:25:11: error: ‘SIZE’ was not declared in this scope
int array[SIZE];
^
array.cpp:28:13: error: ‘array’ was not declared in this scope
read_list(array, elements, SIZE);
^
array.cpp: In function ‘void read_list(int*, int&, int)’:
array.cpp:66:29: error: ‘input’ was not declared in this scope
while (elements < SIZE && input != 0) {
^
array.cpp: At global scope:
array.cpp:75:1: error: expected unqualified-id before ‘{’ token
{
^
Can anyone point me in the right direction or has any tips on how to fix it?
Some small points (this is pedantic so you can ignore it):
1: You have no indentation so subordinate structures are difficult to make out.
2: You have inadequate comments.
You should correct these errors.
More to the point:
1 2 3 4 5
[static] [const] int <variable> = <value>; // you have the wrong syntax.
// Compute max
arrayMax = find_max(array, elements); // you are assigning a variable to an array. try
int max = find_max(array, elements);
1 2 3
while (elements < SIZE && input != 0) { // input needs to be defined I would also suggest:
while ((elements < SIZE) && (input != 0)) { // for clarity
As to scoping. Nothing is seen outside of a function. If you want a variable to be seen in multiple functions, it must be outside of and before all functions. The only other recourse is to pass it as a parameter.