Advertisement
apl-mhd

binarygpFinal

Aug 3rd, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int solution(int N);
  4.  
  5.  
  6. int main(){
  7.  
  8.  
  9.     printf(" %d ", solution(10));
  10.  
  11.  
  12.  
  13.     return 0;
  14. }
  15.  
  16.  
  17. int solution(int N){
  18.  
  19.  
  20.  
  21.  
  22.  
  23.     int number = 15, count = 0, i, j, temp, gapIndex = 0, gapCount = 0;
  24.  
  25.    int  binaryConvertArray[100], binaryGap[100];
  26.  
  27.    /*decimal to binary convert section*/
  28.  
  29.     while(number !=0 ){
  30.  
  31.         binaryConvertArray[count] = number % 2;
  32.  
  33.         number /=2;
  34.         count++;
  35.  
  36.  
  37.     }
  38.  
  39.  
  40.     /* binary array reverse section*/
  41.  
  42.     j = count -1, i = 0;
  43.  
  44.     while(i < j){
  45.  
  46.         temp = binaryConvertArray[i];
  47.  
  48.         binaryConvertArray[i] = binaryConvertArray[j];
  49.         binaryConvertArray[j] = temp;
  50.         i++;
  51.         j--;
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.     for(i = 0; i < count; i++){  /*print binary to decimal number */
  58.  
  59.      printf(" %d ", binaryConvertArray[i]);
  60.  
  61.     }
  62.  
  63.     printf("\n");
  64.  
  65.  
  66.     /*searching gap indexr*/
  67.  
  68.     for(i = 1; i < count; i++){
  69.  
  70.         if(binaryConvertArray[i] == 0){
  71.  
  72.              gapCount++;
  73.  
  74.  
  75.         }
  76.  
  77.         if(binaryConvertArray[i] == 1){
  78.  
  79.            // printf("gapcount =  %d \n", gapCount);
  80.  
  81.             binaryGap[gapIndex] = gapCount;
  82.             gapCount = 0;
  83.             gapIndex++;
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90.     for(i = 0; i < gapIndex; i++){  /*print binary to decimal number */
  91.  
  92.      printf("a %d \n", binaryGap[i]);
  93.  
  94.     }
  95.  
  96.  
  97.     /*bouble sort find largest binary gap lol, eto pera kena???*/
  98.  
  99.     for(i = 0; i < gapIndex; i++){
  100.  
  101.         for(j = 0; j < gapIndex - 1; j++){
  102.  
  103.             if( binaryGap[j] < binaryGap[j+1]){
  104.  
  105.                 temp = binaryGap[j];
  106.  
  107.                binaryGap[j] = binaryGap[j+1];
  108.  
  109.                binaryGap[j+1] = temp;
  110.             }
  111.  
  112.         }
  113.     }
  114.  
  115.     printf("\nhigest to low gap\n");
  116.  
  117.     for(i = 0; i< gapIndex; i++){
  118.  
  119.         printf(" %d ", binaryGap[i]);
  120.  
  121.     }
  122.     printf("\n");
  123.  
  124.  
  125.  
  126.     if(gapIndex > 1){
  127.  
  128.  
  129.         if(binaryGap[0] > binaryGap[1]){
  130.  
  131.             return binaryGap[0];
  132.         }
  133.  
  134.         else{
  135.  
  136.           return  binaryGap[1];
  137.         }
  138.     }
  139.  
  140.     else if(binaryGap[0] == 0){
  141.  
  142.         return 0;
  143.     }
  144.  
  145.     else{
  146.  
  147.         return binaryGap[0];
  148.     }
  149.  
  150.  
  151.  
  152. //    if(binaryGap[0] == 0){
  153.  
  154.  
  155. //        return 0;
  156. //    }
  157.  
  158.  
  159. //     if (binaryGap[0] > binaryGap[1]) {
  160.  
  161. //         return binaryGap[0];
  162. //    }
  163.  
  164. //     else{
  165.  
  166. //         return []
  167. //     }
  168.  
  169.  
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement