
Finding Biggest Value in array (C Programming)?
I have integer data in data.dat. Assume the data b[19] = {1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1}. Then I write the program to read the data from the file. Success! but, I can’t find the biggest value. This is the part of finding the biggest value:
a=0 /*init value of a = 0 so it can check the value below*/
for(i=0;i<19;i++) /*20 data in the array of b*/
{
if(a>b[i]){ /*check the condition*/
printf(“This is the biggest value: %dn”,a)
}
a = b[i]; /*store the current value of b to a so it can check in the next loop*/
}
but i get back these result:
This is the biggest value: 10
This is the biggest value: 9
This is the biggest value: 8
This is the biggest value: 7
This is the biggest value: 6
This is the biggest value: 5
This is the biggest value: 4
This is the biggest value: 3
This is the biggest value: 2
I know the problem: it only check the value before not all.
Please help me…..
oSourceM has put it wrong.. SOrry mate!!
but the right code wud go like this….
big = b[0];
for (i=0;i<=19;i++)
{
if (big
big=b[i];
}
printf("biggest value = %dn",big);
the variable big contains the largest value in the array...
if the array consist n elements...
big = b[0];
for (i=0;i<=n;i++)
{
if (big
big=b[i];
}
Enjoy!! I hope you got what you needed.....

July 13th, 2010
admin
Posted in
Tags: 
