0% found this document useful (0 votes)
93 views

Assignment 4

The document contains 3 programs that demonstrate linear and binary search algorithms in C. Program 1 accepts integer elements from the user, stores them in an array, accepts a search value, and uses linear search to check if the value is in the array. Program 2 does the same but uses binary search on a sorted array with a recursive function. Program 3 generates a random array, accepts a search value, and uses non-recursive binary search to check if the value is in the array.

Uploaded by

bot khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Assignment 4

The document contains 3 programs that demonstrate linear and binary search algorithms in C. Program 1 accepts integer elements from the user, stores them in an array, accepts a search value, and uses linear search to check if the value is in the array. Program 2 does the same but uses binary search on a sorted array with a recursive function. Program 3 generates a random array, accepts a search value, and uses non-recursive binary search to check if the value is in the array.

Uploaded by

bot khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Set A

1) Write a C program to accept n elements from user store it in an array. Accept a value from the user
and use linear/Sequential search method to check whether the value is present in array or not. Display
proper message.

#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
2. Write a C program to accept n elements from user store it in an array. Accept a value from the user and
use binary search method to check whether the value is present in array or not. Display proper message.
(Students should accept sorted array and use Recursive function).
#include <stdio.h>
int binaryScr(int a[], int low, int high, int m)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if (a[mid] == m)
return mid;
if(a[mid] > m)
return binaryScr(a, low, mid - 1, m);
return binaryScr(a, mid + 1, high, m);
}
return -1;
}
int main(void)
{
int a[] = { 12, 13, 21, 36, 40 };
int i,m;
for(i=0;i<5;i++)
{
printf(" %d",a[i]);
}
printf(" n");
int n = sizeof(a) / sizeof(a[0]);
printf("Enter the number to be searchedn");
scanf("%d", &m);
int result = binaryScr(a, 0, n - 1, m);
(result == -1) ? printf("The element is not present in array"):printf("The element is present at index
%d",result);
return 0;
}
3. Write a ‘C’ program to create a random array of n integers. Accept a value of n from user and use
Binary search algorithm to check whether the number is present in array or not. (Students should accept
sorted array and use Non-Recursive function also use random function).
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}

SET B:
1. Write a ‘C’ program to accept the names of cities and store them in array. Accept the city name from
user and use linear search algorithm to check whether the city is present in array or not
2 Write a C program to accept n elements from user store it in an array. Accept a value from the user
and use recursive binary search method to check whether the value is present in array or not. Display
proper message. (use any sorting method to sort the array)

3 Read the data from file ‘sortedcities.txt’ containing sorted names of n cities and their STD codes.
Accept a name of the city from user and use linear search algorithm to check whether the name is present
in the file and output the STD code, otherwise output “city not in the list”.
#include<stdio.h>
#include<string.h>

typedef struct city


{
char name[20];
int code;
}record;
record city[100];
int read_file(record *a)
{
int i=0;
FILE *fp;
if((fp=fopen("sortedcities.txt’ ","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",a[i].name,&a[i].code);
i++;
}}
return (i-1);
}

void l_search(record *a,int n,char x[20])


{
int i;
for(i=0;i<n;i++)
{
if(strcmp(a[i].name,x)==0)
{
printf("\n%s=%d\n",a[i].name,a[i].code);
break;
}
}
if(i==n)
printf("\ncity not found\n");
}

main()
{
char x[20];
int n;
n=read_file(city);
printf("\nenter city name\n");
gets(x);
l_search(city,n,x);
}

You might also like