Ok, for C and not C++?
Just loop through an existing unsorted array to pick out the largested number?
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
#include %26lt;limits.h%26gt;
int main()
{
int nums[] ={8,3,7,5,1,9,6,2,0,4};
int counter, maxNum=INT_MIN;
for (counter=0; counter%26lt;=sizeof(nums); counter++)
if (maxNum %26lt; nums[counter]) maxNum = nums[counter];
/* -or- maxNum=Max(maxNum, nums[counter]); */
printf("%d is the largest number in the array\n",maxNum);
return 0;
}
C programme for largest element in an array?
#include%26lt;stdio.h%26gt;
#define size 10
int main()
{
int i,a[size],largest;
printf("Enter the elements in the array\n");
for(i=0;i%26lt;size;i++)
{
scanf("%d",%26amp;a[i]);
}
largest=a[0];
for(i=0;i%26lt;size;i++)
{
largest=a[i]%26gt;largest?a[i]:largest;
}
printf("Largest element is %d\n",largest);
return 0;
}
floral
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment