Reputation: 31
This is my code:
using namespace std;
// Function prototypes
int highestElement(int [], int);
void doubleArray(int [], int);
void showValues(int [], int);
int main()
{
const int ARRAY_SIZE = 7;
int set[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7};
// Display the initial values.
cout << "The arrays values are:\n";
showValues(set, ARRAY_SIZE);
// Double the values in the array.
doubleArray(set, ARRAY_SIZE);
// Display the resulting values.
cout << "After calling doubleArray the values are:\n";
showValues(set, ARRAY_SIZE);
cout << "The highest element in the array is " << highestElement << endl;
return 0;
}
//*****************************************************
// Definition of function doubleArray *
// This function doubles the value of each element *
// in the array passed into nums. The value passed *
// into size is the number of elements in the array. *
//*****************************************************
void doubleArray(int nums[], int size)
{
for (int index = 0; index < size; index++)
nums[index] *= 2;
}
//**************************************************
// Definition of function showValues. *
// This function accepts an array of integers and *
// the array's size as its arguments. The contents *
// of the array are displayed. *
//**************************************************
void showValues(int nums[], int size)
{
for (int index = 0; index < size; index++)
cout << nums[index] << " ";
cout << endl;
}
// Definition of highestElement (what I need help fixing)
int highestElement(int numbers [], int size)
{
int count;
int highest;
highest = numbers[0];
for (count = 1; count < size; count++)
{
if (numbers[count] < highest)
{
highest = numbers[count];
}
}
}
My errors:
main.cpp:29:54: warning: the address of ‘int highestElement(int*, int)’ will always evaluate as ‘true’ [-Waddress]
cout << "The highest element in the array is " << highestElement << endl;
^~~~~~~~~~~~~~
main.cpp: In function ‘int highestElement(int*, int)’:
main.cpp:75:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
My output is:
The arrays values are:
1 2 3 4 5 6 7
After calling doubleArray the values are:
2 4 6 8 10 12 14
The highest element in the array is 1
The problem is that no matter what I do, the highestElement always equals 1. I know I'm doing something wrong, but I haven't been able to solve it on my own. I'm trying to make the highest element of the array be outputted. I sincerely await a helpful reply.
Upvotes: 2
Views: 58
Reputation: 1
You are not calling the function highestElement()
, so it is giving above errors.
It should be like :
cout << "The highest element in the array is " << highestElement(set, ARRAY_SIZE) << endl;
This is also pointed out by the Error Message
warning: the address of ‘int highestElement(int*, int)’ will always evaluate as ‘true’
Your code is not calling the function and using the return value, but instead is printing the pointer to the function.
Upvotes: 0
Reputation: 12658
First you need to pass the arguments as declared for the function highestElement
.
cout << "The highest element in the array is " << highestElement(set, ARRAY_SIZE) << endl;
Second, since you are looking for max in the array you need to check if current element is greater
than older:
if (numbers[count] > highest)
{
highest = numbers[count];
}
Third, you need to return highest
from the function highestElement()
to the main caller.
return highest;
Upvotes: 2
Reputation: 3408
You are not calling the function highestElement
with the correct arguments.
The line:
cout << "The highest element in the array is " << highestElement << endl;
should change to:
cout << "The highest element in the array is " << highestElement(set, ARRAY_SIZE) << endl;
Also the implmentation of highestElement
should return the highest value
And the check should be for greater instead of smaller
Upvotes: 0