I m trying to find the biggest number in the array but problem is that it keeps returning one

``````#include <stdio.h>
int FindBiggest(int *arr,int choice)
{
int i;
int num=0;
for(i=0;arr[i]<=sizeof arr;i++) {
if(num<arr[i])
num=arr[i];
}
return num;
}
int main(void)
{
int arr[]={0,5,3};
int result;
result=FIND_NUM(arr,BIGGEST);
printf("%d\n",result);
getchar();
return 0;
}``````

even though in my function i set the number to 0 first then after that we check with the if num<array whish ofcourse it is now it we set the num to array untill there no we found our biggest num i dunno its problem :s

Is there any code you aren't posting? what is FIND_NUM and BIGGEST? You can't use sizeof to work out the length of an array passed to a pointer, instead you have to allow the caller to pass the length of the array as a parameter.

yah i changed that i also changed in function the if better and still not working

``````#include <stdio.h>
int FindBiggest(int *arr)
{
int i;
int num=0;
for(i=0;arr[i]<=3;i++) {
if(arr[i]>num)
num=arr[i];
}
return num;
}
int main(void)
{
int arr[]={0,5,3};
int result;
result=FindBiggest(arr);
printf("%d\n",result);
getchar();
return 0;
}``````

now ofcourse 1st num is bigger than 0 whish should set num to 5 whats wrong now :S?

your problem is here: `for(i=0;arr[i]<=3;i++)` your index is 'i' yet your conditional is based on the value of 'arr' at the index 'i'

fix that.

.

``````#include <stdio.h>

int FindBiggest(int *arr, [B]int length[/B])
{
int i;
int num = 0;

for (i = 0; [B]i < length[/B]; i++) {
if ( arr[i] > num )
num = arr[i];
}

return num;
}

int main(void)
{
int arr[] = {0, 5, 3};
[B]int length = sizeof(arr) / sizeof(arr[0]);[/B]
int result;

result = FindBiggest( arr, [B]length[/B] );
printf( "%d\n", result );

getchar();
return 0;
}``````

I highlighted the changes, also you were looping through too many elements in the array by using <=.

i made it work now but what the diff between array and just normal i?

``````#include <stdio.h>
int FindBiggest(int *arr)
{
int i;
int num=0;
for(i=0;i<3;i++) {//used to be for(i=0;array[i<3;i++)
if(arr[i]>num)
num=arr[i];
}
return num;
}
int main(void)
{
int arr[]={0,5,3};
int result;
result=FindBiggest(arr);
printf("%d\n",result);
getchar();
return 0;
}``````

in first line in the for loop ?

>what the diff between array and just normal i?
It's two completely different things, i is just an integer, array is the element which is located at the index of i.

thnaks man but i got one more question i made also a find biggest and smallest but it alawys get last element

``````#include <stdio.h>
#define BIGGEST 1
#define Smallest 0
int FindBiggest(int *arr,int choice)
{
int i;
int num=0;
for(i=0;i<3;i++) {
if(choice){//find BIGGEST NUM
if(arr[i]>num)
num=arr[i];
}
else
{
num=1000;
if(arr[i]<num)
num=arr[i];
}

}
return num;
}
int main(void)
{
int arr[]={0,5,3};
int result;
result=FindBiggest(arr,Smallest);
printf("%d\n",result);
getchar();
return 0;
}``````

If you are working out the biggest number, you have to start off with the smallest possible value and keep looking for a bigger value.

To find the smallest, you have to do the opposite. Start off with the biggest possible value, and keep looking for a smaller one.

Also it is more efficient to put the for loop inside the if block instead of the other way round, here is the corrected code:

``````#include <stdio.h>
#include <limits.h>

#define Biggest   1
#define Smallest  0

int FindBiggest(int *arr, int length, int choice)
{
int i;
int num = 0;

if ( choice == Biggest ) {
for (i = 0; i < length; i++) {
if ( arr[i] > num ) { // Look for a bigger value
num = arr[i];
}
}
}

else if ( choice == Smallest ) {
num = INT_MAX; // Make it start off with the biggest possible value
for (i = 0; i < length; i++) {
if ( arr[i] < num ) { // Look for a smaller value
num = arr[i];
}
}
}

return num;
}

int main(void)
{
int arr[] = {2, 1, 3};
int length = sizeof(arr) / sizeof(arr[0]); // Works out the length of arr
int result;

result = FindBiggest(arr, length, Smallest);
printf("%d\n", result);

getchar();
return 0;
}``````

i made also a find biggest and smallest but it alawys get last element

because for the smallest, you set num=1000 every single time you go through the loop. arr will always be less than 1000 each time, so it stores it in num, each time.

EDIT: i see William already re-wrote your code for you. consider yourself (un)lucky, because i would have made you fix it yourself.

thanks alot guys for helping me out

>i see William already re-wrote your code for you. consider yourself (un)lucky, because i would have made you fix it yourself.
Perhaps I am too generous sometimes :P

I would have done it slightly different what what William posted -- initialize the value of num with the first element of the array on line 10, then line 21 of his code could be deleted.

commented: Good idea. +10

i would have done it completely different than what william posted.

and it would be just as trivial.