bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

pc-ip-19.c (2069B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 05 Jan 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /*
      9  * Question 19:
     10  * Write a C program that includes a user-defined function named binarySearch with the signature int binarySearch(int arr[], int size, int target);.
     11  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 
     16 int binarySearch(int[], int, int);
     17 void inputArray(int[], int);
     18 void printArray(int[], int);
     19 
     20 int main()
     21 {
     22     int n, *arr = NULL, target, foundIndex;
     23     printf("Enter the number of element: ");
     24     scanf("%d", &n);
     25     arr = (int *)malloc(n * sizeof(int));
     26     if (arr == NULL)
     27     {
     28         printf("\nMemory allocation failed.");
     29         return 1;
     30     }
     31     printf("\nPlease enter the sorted array element(s): \n");
     32     inputArray(arr, n);
     33     printf("\nGiven array: ");
     34     printArray(arr, n);
     35     printf("\nPlease enter the target element: ");
     36     scanf("%d", &target);
     37     foundIndex = binarySearch(arr, n, target);
     38     if (foundIndex != -1)
     39     {
     40         printf("\nTarget '%d' found at Index %d.", target, foundIndex);
     41     }
     42     else
     43     {
     44         printf("\nUnable to find target '%d'.", target);
     45     }
     46     free(arr);
     47     return 0;
     48 }
     49 
     50 void inputArray(int arr[], int n)
     51 {
     52     int i;
     53     for (i = 0; i < n; i++)
     54     {
     55         printf("Enter element %d: ", i + 1);
     56         scanf("%d", &arr[i]);
     57     }
     58 }
     59 
     60 void printArray(int arr[], int n)
     61 {
     62     int i;
     63     printf("[");
     64     for (i = 0; i < n; i++)
     65     {
     66         printf("%d", arr[i]);
     67         if (i < n - 1)
     68         {
     69             printf(", ");
     70         }
     71     }
     72     printf("]");
     73 }
     74 
     75 int binarySearch(int arr[], int size, int target)
     76 {
     77     int low = 0;
     78     int high = size - 1;
     79     int mid;
     80     while (low <= high)
     81     {
     82         mid = (low + high) / 2;
     83         if (arr[mid] == target)
     84         {
     85             return mid;
     86         }
     87         else if (arr[mid] > target)
     88         {
     89             high = mid - 1;
     90         }
     91         else if (arr[mid] < target)
     92         {
     93             low = mid + 1;
     94         }
     95     }
     96     return -1;
     97 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror