Sunday, July 12, 2009

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%26lt;19;i++) /*20 data in the array of b*/


{


if(a%26gt;b[i]){ /*check the condition*/


printf("This is the biggest value: %d\n",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.....

Finding Biggest Value in array (C Programming)?
oSourceM has put it wrong.. SOrry mate!!





but the right code wud go like this....





big = b[0];


for (i=0;i%26lt;=19;i++)


{


if (big%26lt;b[i])


big=b[i];


}


printf("biggest value = %d\n",big);





the variable big contains the largest value in the array...





if the array consist n elements...





big = b[0];


for (i=0;i%26lt;=n;i++)


{


if (big%26lt;b[i])


big=b[i];


}





Enjoy!! I hope you got what you needed.....
Reply:a=0; c=0; //c for the biggest value


for (i=0;i%26lt;19;i++)


{


a=b[i];


// checks if the first value from array is bigger


// than the second and etc.


if (a%26gt;b[i+1])





{c=a;}


}


printf ("print here the biggest value")





hope You will get the point.





Good luck


No comments:

Post a Comment