find the max value in an array ?
can anyone tell me whats wrong with this program ?
i've just learned what are arrays so don't know much about it, i want to write a program that prints the max value inside an array.
i wrote this:

``````int main(void)
{
int temp[2];
int i,max=0;
for (i=0;i<2;i++)
{
printf ("Enter value for Temperature %d:",i);
scanf ("%d",&temp[i]);
}
for (i=0;i<2;i++)
{
max=temp[i];
if (temp[i]>max)
printf ("%d ",temp[i]);
}
getche ();
}``````

but this is not working :(

you're using i as the iterator for both of the nested for loops! ;)

And why are using two loop for this? You only have 2 elements in your array to fill up.

Can't you get rid of the inner for loop?

The way to do this is to set max equal to the temp, ONLY if the temp entered is greater than the value of max.

make sense?

that is not the nested loop, it is just used to print the values (as mentioned in the book), anyways here it is the program without it,

``````int main(void)
{
int temp[2];
int i,max=0;
for (i=0;i<2;i++)
{
printf ("Enter value for Temperature %d:",i);
scanf ("%d",&temp[i]);
}
{
max=temp[i];
if (temp[i]>max)
printf ("%d ",temp[i]);
}
getche ();
}``````

how can i print the max value ?

Xufyan, you are going to *have* to learn to indent your code and especially your braces, like you are sane - even if you're not ;)

``````for(i = 0; i < whatEver; i++)
{
//other code in here, then:
}``````

Note how the braces line up with the first line of code - their "parent line". Your eye just flows, right to the major and minor lines of code, and it only gets better as you code more often.

You indent your code like that, and you'll see the answer. You print max temp right after the last closing brace of the for loop.

Why?

Because you're done entering data.

commented: lol. i agree, that drives me nuts. +7

You only need one for loop:

``````for(i = 0; i < 2; i++)
{
Ask for the user to enter a temp
user enters a temp
You check the user's entered number to see if it's greater than
max  temp

Now print the number user entered

}
print max temp``````
commented: thanks :-) +0

thanks :)
i've done the program and it is working, can you tell me now that why my program isn't working when i'm not assigning '0' to `max` ?

here is the program,

``````int main(void)
{
int num[5];
int i,max=0;
for (i=0;i<5;i++)
{
printf ("Enter value for Temperature %d:",i);
scanf ("%d",&num[i]);
if (num[i]>max)
max=num[i];
}
printf ("%d ",max);

getche ();
}``````

this program is not working when i'm assigning no value for max, why ?
and i didn't understand how my program is checking that the entered numbers are greater than max temp bec0z max is assigned for 0

You only need one for loop:

``````for(i = 0; i < 2; i++)
{
Ask for the user to enter a temp
user enters a temp
You check the user's entered number to see if it's greater than
max  temp

Now print the number user entered

}
print max temp``````

problem solved, thanks alot :) i've understood
another question:
how to print minimum value ?
for minimum value should i've to assigned the largest value for min while initializing

# include<stdio.h>

int main()
{ int a[10];

int max= retmax(a);

printf("max="%d"",max);
}

int retmax(int a)
{
int i,j;

for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a>a[j])
{int temp;
temp=a;
a=a[j];
a[j]=temp;
}

}
return a[10];
}
}

}

}

commented: Don't you know how to properly code tag and format you posted code? -2

try it n dear teamp stands fr temporary not for temperature

``````int main(void)
{
int temp[2];
int i,max;
for (i=0;i<2;i++)
{
printf ("Enter value in an array %d:",i);
scanf ("%d",&temp[i]);
}
max=temp[0];
for (i=1;i<2;i++)
{
if (temp[i]>max)
max=temp[i]
}
printf("max no in array=%d",max);
getche ();
}
``````

but this is not working :(

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

int main()
{ int a[10];

int max= retmax(a);

printf("max="%d"",max);
}

int retmax(int a)
{
int i,j;

for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{  int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

}
return a[10];
}
}

}

}``````

this will help in finding both maximum and minimum

``````int main(void)
{
int temp[2];   // two element array
int i,max;
for (i=0;i<2;i++)    // read 2 numbers into the array
{
printf ("\nEnte value %d into array.",i);
scanf("%d",&temp[i]);
}

//Now find max val in the array.
max = temp[0];  // first element in the array is stored as max
for (i=1;i<2;i++)    /
{
if(temp[i] > max)  // if second element is bigger than the first
{
//these pair of braces are not compulsory,for a single statement.
max = temp[i] ;   // every statement must end with a ;
}
}
printf("Max value = %d",max);
return 0;      // as main is int type
}
/*   Note : In one of the above post, you are missing a ; at line 14,   otherwise it is OK  - Biju Joseph N, Kerala, India*/
``````
``````#include<stdio.h>
#include<conio.h>

int MAX(int []);

int main()
{
int arr[5]={1,2,45,36,65};

int max=MAX(arr);
printf("MAx=%d",max);
return 0;
}

int MAX(int a[])
{ int i,j;

for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{ a[i]=a[i]+a[j];
a[j]=a[i]-a[j];
a[i]=a[i]-a[j];  //swapping

}
}
}

return a[4];
}``````

THIS IS WORKING :):)