Assignment 4
Assignment 4
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>
main()
{
char x[20];
int n;
n=read_file(city);
printf("\nenter city name\n");
gets(x);
l_search(city,n,x);
}