I have tried running my code with both the commented statement and the non-commented statement and they both give me the number 2293484 as the result.
The program is supposed to give me the highest number in the array. Any help would really be appreciated, explanation would be even more so. Thanks in advance. I am doing this as a study material for my test which covers loops.
2 things that are wrong, although one is commented out.
1. Your for-loop.for (int i=0;i>5;i++) // should be i < 5, not i > 5
i is 0, you want it to run as long as i is less than 5, not as long as i is bigger than 5.
#include <iostream>
usingnamespace std;
int find_max(int nums[]);
int main( )
{
int nums[5] = {2, 18, 1, 27, 16};
cout << "\nThe maximum value is " << find_max(nums)<< endl;
return 0;
}
int find_max(int nums[])
{
int temp=0;
for (int i=0;i<5;i++)
{
if (nums[i]>temp)
temp=nums[i];
}
return temp;
}
Woah thanks. Just realized that dumb mistake =) ; I knew about the return temp too, but deleted it before posting for debugging purposes. Thanks a lot. Now I will try to sort them out and see how it goes. If there was a way to give you thumbs up I would but I am new here and I don't think there is a way (as far as I think I know). Thanks a million!