Pages

Related Posts with Thumbnails
Maximum Number Finding By Smallest Comparisons – C Programming >> Codes-N-Tricks
Content feed Comments Feed

Maximum Number Finding By Smallest Comparisons – C Programming

Posted by Soham Saturday, September 18, 2010

This Programs finds maximum and minimum Numbers from an Input array with the smallest number of Comparisons.It basically compare between successive two numbers and stores maximum(or minimum) among them and creates a sub-array with those maximums(or minimums) and performs a recursive algorithm in sub-array i.e again find maximum among two successive and creates sub-array with those.

The very last element in the last sub-array will be maximum(or minimum).


Maximum & Minimum Number
  1. #include<stdio.h>
  2.  
  3. void func(int max[],int min[],int no);
  4. int cnt=0;//counting the comparison
  5. int main()
  6. {
  7. int num[100],max[50],min[50];
  8. int no,nom,i,j=0;
  9.  
  10. printf("\nHow many numbers you want to enter:-");
  11. scanf("%d",&no);
  12. printf("\n");
  13. //inputting the number
  14. for(i=0;i<no;i++)
  15. {
  16.   printf("Enter number %d:-",i+1);
  17.   scanf("%d",&num[i]);
  18. }
  19. //checking if n is odd or not
  20. if(no%2==0)
  21.   {nom=no;}
  22. else
  23. {
  24.   nom=no-1;
  25.   max[nom/2]=num[nom];
  26.   min[nom/2]=num[nom];
  27. }
  28. i=0;
  29. //dividing the max and min array
  30. while(i<nom-1)
  31. {
  32.   if(num[i]>num[i+1])
  33.   {
  34.    max[j]=num[i];
  35.    min[j]=num[i+1];
  36.   }
  37.   else
  38.   {
  39.    max[j]=num[i+1];
  40.    min[j]=num[i];
  41.   }
  42.   cnt++;
  43.   i+=2;
  44.   j++;
  45. }
  46. //calling the function to determine max and min by sending max,min and no. of elements
  47. if(no%2==0)
  48.   func(max,min,no/2);
  49. else
  50.   func(max,min,(no/2)+1);
  51. //output
  52. printf("\nThe total number you have given is %d and the comparisons are %d.",no,cnt);
  53. if(cnt!=0)
  54. {
  55.   printf("\nThe maximum number is %d.",max[0]);
  56.   printf("\nThe minimum number is %d.\n",min[0]);
  57. }
  58. else
  59.   printf("\nError in evaluating Maximum and minimum numbers.\n");
  60. printf("\n");
  61. }
  62.  
  63. void func(int max[],int min[],int no)
  64. {
  65. int i=0,j=0,nom;
  66. if(no%2==0) nom=no;
  67. else
  68. {
  69.   nom=no-1;
  70. }
  71. while(i<nom-1)
  72. {
  73.   //for maxm
  74.   if(max[i]>max[i+1])
  75.    max[j]=max[i];
  76.   else
  77.    max[j]=max[i+1];
  78.   //for minm
  79.   if(min[i]<min[i+1])
  80.    min[j]=min[i];
  81.   else
  82.    min[j]=min[i+1];
  83.   
  84.   i+=2;
  85.   j++;
  86.   cnt+=2;//for two comparisons
  87. }
  88. if(no%2!=0)
  89. {
  90.   max[nom/2]=max[nom];
  91.   min[nom/2]=min[nom];
  92. }
  93. if(nom/2>=1)
  94. {
  95.   if(no%2==0) func(max,min,nom/2);
  96.   else func(max,min,(nom/2)+1);
  97. }
  98. }


Further Discussion will be carried out later.

Any suggestion is solicited.

0 Responses to Maximum Number Finding By Smallest Comparisons – C Programming

Post a Comment

SyntaxHighlighter

E-Mail Subscription

Enter your email address:

Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
Delivered by FeedBurner

Share with Orkut

Loading

Recent Posts

About Us