Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:

Returns -1 if a list is sequential, otherwise the missing element in a non-sequential list is returned.

To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (low, high) and their corresponding values.

Compilation procedure:

gcc -Wall -g testList.c -o testList
./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){
 int mid = (low + high) / 2;
 if(high == low + 1){
 if(array[high] > array[low] +1){
 return array[low] +1;
 }else{
 return -1;
 }
 }
 if(array[mid] - array[low] == mid - low){
 return bSearch(array, mid, high);
 }else if(array[mid] - array[low] > mid - low){
 return bSearch(array, low, mid);
 }else{
 return -1;
 }
}
int main(void){
 int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
 int upperBound = (sizeof(array)/sizeof(array[0]))-1;
 printf("%d\n", bSearch(array, 0, upperBound));
 return 0;
}

My questions:

  1. Why does removing the else{} case cause the compiler to give a warning for it?

  2. Can the base case look more elegant?

  3. For code elegance and optimization, can this code be improved?

I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:

Returns -1 if a list is sequential, otherwise the missing element in a non-sequential list is returned.

To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (low, high) and their corresponding values.

Compilation procedure:

gcc -Wall -g testList.c -o testList
./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){
 int mid = (low + high) / 2;
 if(high == low + 1){
 if(array[high] > array[low] +1){
 return array[low] +1;
 }else{
 return -1;
 }
 }
 if(array[mid] - array[low] == mid - low){
 return bSearch(array, mid, high);
 }else if(array[mid] - array[low] > mid - low){
 return bSearch(array, low, mid);
 }else{
 return -1;
 }
}
int main(void){
 int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
 int upperBound = (sizeof(array)/sizeof(array[0]))-1;
 printf("%d\n", bSearch(array, 0, upperBound));
 return 0;
}

My questions:

  1. Why does removing the else{} case cause the compiler to give a warning for it?

  2. Can the base case look more elegant?

  3. For code elegance and optimization, can this code be improved?

I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:

Returns -1 if a list is sequential, otherwise the missing element in a non-sequential list is returned.

To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (low, high) and their corresponding values.

Compilation procedure:

gcc -Wall -g testList.c -o testList
./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){
 int mid = (low + high) / 2;
 if(high == low + 1){
 if(array[high] > array[low] +1){
 return array[low] +1;
 }else{
 return -1;
 }
 }
 if(array[mid] - array[low] == mid - low){
 return bSearch(array, mid, high);
 }else if(array[mid] - array[low] > mid - low){
 return bSearch(array, low, mid);
 }else{
 return -1;
 }
}
int main(void){
 int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
 int upperBound = (sizeof(array)/sizeof(array[0]))-1;
 printf("%d\n", bSearch(array, 0, upperBound));
 return 0;
}

My questions:

  1. Why does removing the else{} case cause the compiler to give a warning for it?

  2. Can the base case look more elegant?

  3. For code elegance and optimization, can this code be improved?

edited title
Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63

Find a missing element in a sequentialsorted list

added 7 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Find a missing element in a sequential list

I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:

Returns -1, if, a list is sequential, otherwise the missing element in a non-sequential list is returned.

To perform binary search, we need a key to half the problem size. Here the key is, to find the distance between the index (low, high) and their corresponding values.

Compilation procedure:

gcc -Wall -g testList.c -o testList
./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){
 int mid = (low + high) / 2;
 if(high == low + 1){
 if(array[high] > array[low] +1){
 return array[low] +1;
 }else{
 return -1;
 }
 }
 if(array[mid] - array[low] == mid - low){
 return bSearch(array, mid, high);
 }else if(array[mid] - array[low] > mid - low){
 return bSearch(array, low, mid);
 }else{
 return -1;
 }
}
int main(void){
 int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
 int upperBound = (sizeof(array)/sizeof(array[0]))-1;
 printf("%d\n", bSearch(array, 0, upperBound));
 return 0;
}

My questions:

  1. Why does removing the else{} case cause the compiler to give a warning for it?

  2. Can the base case look more elegant?

  3. For code elegance and optimization, can this code be improved?

Find missing element in sequential list

I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:

Returns -1, if, list is sequential otherwise missing element in non-sequential list is returned.

To perform binary search, we need a key to half the problem size. Here the key is, to find the distance between the index (low, high) and their corresponding values.

Compilation procedure

gcc -Wall -g testList.c -o testList
./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){
 int mid = (low + high) / 2;
 if(high == low + 1){
 if(array[high] > array[low] +1){
 return array[low] +1;
 }else{
 return -1;
 }
 }
 if(array[mid] - array[low] == mid - low){
 return bSearch(array, mid, high);
 }else if(array[mid] - array[low] > mid - low){
 return bSearch(array, low, mid);
 }else{
 return -1;
 }
}
int main(void){
 int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
 int upperBound = (sizeof(array)/sizeof(array[0]))-1;
 printf("%d\n", bSearch(array, 0, upperBound));
 return 0;
}

My questions:

  1. Why does removing the else{} case cause the compiler to give a warning for it?

  2. Can the base case look more elegant?

  3. For code elegance and optimization, can this code be improved?

Find a missing element in a sequential list

I've made a program to find the missing element (if there's one) in a sequential list:

Program criteria:

Returns -1 if a list is sequential, otherwise the missing element in a non-sequential list is returned.

To perform binary search, we need a key to half the problem size. Here the key is to find the distance between the index (low, high) and their corresponding values.

Compilation procedure:

gcc -Wall -g testList.c -o testList
./testList

#include<stdio.h>
int bSearch(int array[], int low, int high){
 int mid = (low + high) / 2;
 if(high == low + 1){
 if(array[high] > array[low] +1){
 return array[low] +1;
 }else{
 return -1;
 }
 }
 if(array[mid] - array[low] == mid - low){
 return bSearch(array, mid, high);
 }else if(array[mid] - array[low] > mid - low){
 return bSearch(array, low, mid);
 }else{
 return -1;
 }
}
int main(void){
 int array [] = {22, 23, 24, 25, 26, 27, 28, 30};
 int upperBound = (sizeof(array)/sizeof(array[0]))-1;
 printf("%d\n", bSearch(array, 0, upperBound));
 return 0;
}

My questions:

  1. Why does removing the else{} case cause the compiler to give a warning for it?

  2. Can the base case look more elegant?

  3. For code elegance and optimization, can this code be improved?

Loading
deleted 64 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
deleted 64 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Loading
deleted 86 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
overexchange
  • 3.4k
  • 8
  • 35
  • 63
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /