What am I doing wrong? find_max function

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.

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

using namespace 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];
        /*if (nums[i]<nums[i+1])
        {
            int temp=nums[i];
            nums[i] = nums[i+1];
            nums[i+1]=temp;
            return temp;
        }*/

    }

}
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.

2. return the number

Full program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace 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!
Topic archived. No new replies allowed.