Practice Programs:
1) Write a C program to linearly search an element in a given array. (Use
Recursion).
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int arr[10];
int n, i, fVal, flag = 0;
printf("Enter the size of
array : \n");
scanf("%d", &n);
printf("Enter the Element in
Array : \n");
for (i = 0; i < n; i++)
{
printf("Enter value of
index[%d] : ", i);
scanf("%d",
&arr[i]);
}
printf("Enter the value to
Find : \n");
scanf("%d", &fVal);
i = 0;
while (i < n)
{
if (arr[i] == fVal)
{
flag = 1;
printf("The element
is found !! \n index = %d ", i);
break;
}
}
if (flag == 0)
{
printf("The element is
not found ! \n");
}
getch();
return 0;
}
==========================================================================
2) Read the data from file ‘employee.txt’ containing names of n employees,
their qualificationand salary. Accept a name of the employee from the user and
by using linear search algorithm check whether the name of employee is present
in the file or not if present display salary of that employee, otherwise
display “Employee not found”.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define Max_Len 20
typedef struct emp
{
char name[Max_Len];
char que[Max_Len];
float salary;
} employee;
int searchName(employee arr[], char empName[], int size);
int flag = 0;
int main()
{
FILE *fp;
char empName[Max_Len];
int i, size, f;
employee emp[Max_Len];
fp = fopen("02_employee.txt",
"r");
for (i = 0; !feof(fp); i++)
{
fscanf(fp, "%s %s
%f", &emp[i].name, &emp[i].que, &emp[i].salary);
printf("%s %s %f
\n", emp[i].name, emp[i].que, emp[i].salary);
}
size = i;
printf("Enter the Employee
Name for Searching : ");
gets(empName);
f = searchName(emp, empName,
size);
if (flag == 0)
{
printf("The employee is
not found !!\n");
}
else
{
printf("The employee is
found at %d Position !!\n", f + 1);
printf("Employee Details
is : \n");
printf("The salary of %s
employee is %.2f", emp[f].name, emp[f].salary);
}
fclose(fp);
getch();
return 0;
}
int searchName(employee arr[], char empName[], int size)
{
int i;
for (i = 0; i < size; i++)
{
if (strcmp(arr[i].name,
empName) == 0)
{
flag = 1;
return i;
}
else
{
flag = 0;
}
}
return flag;
}
==========================================================================
3) Read the data from file ‘player.txt’ containing names of n Player, their
game_played andage. Accept a name of the player from the user and by using
binary search algorithm check whether the name of player is present in the file
or not if present display game_played and age of that player, otherwise display
“player not found”.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define max_len 20
typedef struct player
{
char name[max_len];
int game;
int age;
} player;
int binary(player arr[], int low, int high, char value[]);
int main()
{
FILE *fp;
player p[max_len];
char value[max_len];
int i = 0, size, result;
fp =
fopen("03_player.txt", "r");
while (!feof(fp))
{
fscanf(fp, "%s %d
%d", &p[i].name, &p[i].game, &p[i].age);
printf("%s %d %d
\n", p[i].name, p[i].game, p[i].age);
i++;
}
size = i;
printf("Enter the find value
\n");
scanf("%s",
&value);
result = binary(p, 0, size - 1,
value);
// printf("%d",
result);
if (result > 0)
{
printf("The player is
Found !! \n");
printf("Player game
played = %d\n", p[result].game);
printf("Player Age =
%d\n", p[result].age);
}
else
{
printf("The player is
not Found !! \n");
}
fclose(fp);
getch();
return 0;
}
int binary(player arr[], int low, int high, char value[])
{
int mid, cmp;
while (low <= high)
{
mid = (low + high) / 2;
cmp = strcmp(value,
arr[mid].name);
if (cmp == 0)
{
return mid;
}
if (cmp == -1)
{
high = mid - 1;
}
if (cmp == 1)
{
low = mid + 1;
}
}
return -1;
}
=======================================================================
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>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int search(int arr[], int size, int value);
int main()
{
int arr[10];
int i, n, value, size, result;
printf("Enter the size of
array : \n");
scanf("%d", &n);
printf("Enter element in
Array :\n");
for (i = 0; i < n; i++)
{
printf("Enter the value
of index[%d] : ", i);
scanf("%d",
&arr[i]);
}
printf("\nEnter the integer
Searching Element : ");
scanf("%d",
&value);
size = i;
search(arr, size, value);
if (result < 0)
{
printf("The element is
not found !! \n");
}
else
{
printf("The element is
found !!");
}
getch();
return 0;
}
int search(int arr[], int size, int value)
{
int i;
for (i = 0; i < size; i++)
{
if (arr[i] == value)
{
return i;
}
}
return -1;
}
==================================================================
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>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void mergeSort(int arr[], int low, int high);
void merge(int arr[], int low, int mid, int high);
int binarySearch(int arr[], int low, int high, int value);
int main()
{
int arr[10];
int i, n, value, size, result;
printf("Enter the size of
array : \n");
scanf("%d", &n);
printf("Enter element in
Array :\n");
for (i = 0; i < n; i++)
{
printf("Enter the value
of index[%d] : ", i);
scanf("%d",
&arr[i]);
}
mergeSort(arr, 0, n - 1);
printf("\nEnter the integer
Searching Element : ");
scanf("%d", &value);
size = i;
result = binarySearch(arr, 0,
size, value);
if (result < 0)
{
printf("The Element is
not Found !!");
}
else
{
printf("The element is
found at %d position", result + 1);
}
getch();
return 0;
}
void mergeSort(int arr[], int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1,
high);
merge(arr, low, mid, high);
}
}
void merge(int arr[], int low, int mid, int high)
{
int i, j, k, copy[10];
i = low;
j = mid + 1;
k = 0;
while (i <= mid && j
<= high)
{
if (arr[i] < arr[j])
{
copy[k++] = arr[i++];
}
else
{
copy[k++] = arr[j++];
}
}
while (i <= mid)
{
copy[k++] = arr[i++];
}
while (j <= high)
{
copy[k++] = arr[j++];
}
for (int j = low, k = 0; j <=
high; k++, j++)
{
arr[j] = copy[k];
}
}
int binarySearch(int arr[], int low, int high, int value)
{
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] == value)
{
return mid;
}
if (mid < value)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
===============================================================
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>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int binarySearch(int arr[], int low, int high, int value);
int main()
{
int arr[10];
int i, n, value, size, result;
printf("Enter the size of
array : \n");
scanf("%d", &n);
printf("Enter the sorted
element in Array :\n");
for (i = 0; i < n; i++)
{
printf("Enter the value
of index[%d] : ", i);
scanf("%d",
&arr[i]);
}
printf("\nEnter the integer
Searching Element : ");
scanf("%d",
&value);
size = i;
result = binarySearch(arr, 0,
size, value);
if (result < 0)
{
printf("The Element is
not Found !!");
}
else
{
printf("The element is
found at %d position", result + 1);
}
getch();
return 0;
}
int binarySearch(int arr[], int low, int high, int value)
{
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] == value)
{
return mid;
}
if (mid < value)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
=========================================================================
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.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int search(char arr[][10], int size, char value[]);
int main()
{
int n, i, result;
char value[10];
printf("Enter the size of Array
: \n");
scanf("%d", &n);
char name[n][10];
printf("Enter the names of
Cities : \n");
for (i = 0; i < n; i++)
{
printf("Enter %d value :
", i + 1);
scanf("%s",
name[i]);
}
printf("Enter the City for
Searching : \n");
scanf("%s", value);
result = search(name, n, value);
if (result < 0)
{
printf("The City is not
Found !!");
}
else
{
printf("The City id
Found at %d position !!", result + 1);
}
getch();
return 0;
}
int search(char arr[][10], int size, char value[])
{
int i;
for (i = 0; i < size; i++)
{
if (strcmp(arr[i], value) ==
0)
{
return i;
}
}
return -1;
}
==========================================================================
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)
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int binarySearch(int arr[], int low, int high, int value);
int main()
{
int arr[10];
int i, n, value, size, result;
printf("Enter the size of
array : \n");
scanf("%d", &n);
printf("Enter element in
Array :\n");
for (i = 0; i < n; i++)
{
printf("Enter the value
of index[%d] : ", i);
scanf("%d",
&arr[i]);
}
printf("\nEnter the integer
Searching Element : ");
scanf("%d",
&value);
size = i;
result = binarySearch(arr, 0,
size, value);
if (result < 0)
{
printf("The Element is
not Found !!");
}
else
{
printf("The element is
found at %d position", result + 1);
}
getch();
return 0;
}
int binarySearch(int arr[], int low, int high, int value)
{
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] == value)
{
return mid;
}
if (mid < value)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
=========================================================================
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 <conio.h>
#include <stdlib.h>
#include <string.h>
#define max_len 20
struct city
{
char name[max_len];
int STD;
};
int search(struct city arr[], int size, char f_value[]);
int main()
{
FILE *fp;
char f_value[max_len];
struct city name[max_len];
int i = 0, size, result;
fp =
fopen("03_sortedCities.txt", "r");
for (i = 0; !feof(fp); i++)
{
fscanf(fp, "%s %d",
&name[i].name, &name[i].STD);
}
printf("Enter the City name
for Search : \n");
scanf("%s", f_value);
size = i;
result = search(name, size,
f_value);
if (result < 0)
{
printf("The City is not
found !! \n");
}
else
{
printf("The City is
found at %d position\n", result + 1);
printf("The City is %s
that STD code is %d", name[result].name, name[result].STD);
}
fclose(fp);
getch();
return 0;
}
int search(struct city arr[], int size, char f_value[])
{
for (int i = 0; i < size; i++)
{
if (strcmp(arr[i].name,
f_value) == 0)
{
return i;
}
}
return -1;
}
======================================================================
SET C:
1) Write a C program to read the data from file 'cities.txt' containing
names of 10 cities and their STD codes. Accept a name of the city from user and
use Binary 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 <conio.h>
#include <stdlib.h>
#include <string.h>
#define max_len 20
struct city
{
char cities[max_len];
int STD;
};
int binarySearch(struct city arr[], int low, int high, char f_value[]);
int main()
{
FILE *fp;
char f_value[max_len];
struct city city_name[max_len];
int i = 0, size, result;
fp =
fopen("01_cities.txt", "r");
for (i = 0; !feof(fp); i++)
{
fscanf(fp, "%s %d",
&city_name[i].cities, &city_name[i].STD);
printf("%s %d\n",
city_name[i].cities, city_name[i].STD);
}
printf("Enter the City
cities for Search : \n");
scanf("%s", f_value);
size = i;
result = binarySearch(city_name,
0, size, f_value);
if (result < 0)
{
printf("The City is not
found !! \n");
}
else
{
printf("The City is
found at %d position\n", result + 1);
printf("The City is %s
that STD code is %d", city_name[result].cities, city_name[result].STD);
}
fclose(fp);
getch();
return 0;
}
int binarySearch(struct city arr[], int low, int high, char f_value[])
{
int mid, cmp;
while (low <= high)
{
mid = (low + high) / 2;
cmp = strcmp(arr[mid].cities,
f_value);
if (cmp == 0)
{
return mid;
}
if (cmp < 0)
{
low = mid + 1;
}
if (cmp > 0)
{
high = mid - 1;
}
}
return -1;
}
=========================================================================
2) Write a C program to read the data from file 'student.txt' containing
names of 10 students and their roll no. Accept a name of the student from user
and use Binary search algorithm to check whether the name is present in the
file and output the roll no, otherwise output “Student name not in the list”.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define max_len 20
struct student
{
int roll_no;
char name[max_len];
};
int binarySearch(struct student arr[], int low, int high, char f_value[]);
int main()
{
FILE *fp;
char f_value[max_len];
struct student stud[max_len];
int i = 0, size, result;
fp =
fopen("02_student.txt", "r");
for (i = 0; !feof(fp); i++)
{
fscanf(fp, "%d %s",
&stud[i].roll_no, &stud[i].name);
printf("%d %s\n",
stud[i].roll_no, stud[i].name);
}
printf("Enter the Student
name for Search : \n");
scanf("%s", f_value);
size = i;
result = binarySearch(stud, 0,
size, f_value);
if (result < 0)
{
printf("The City is not
found !! \n");
}
else
{
printf("The City is
found at %d position\n", result + 1);
printf("The Student is
%s that roll no. is %d", stud[result].name, stud[result].roll_no);
}
fclose(fp);
getch();
return 0;
}
int binarySearch(struct student arr[], int low, int high, char f_value[])
{
int mid, cmp;
while (low <= high)
{
mid = (low + high) / 2;
cmp = strcmp(arr[mid].name,
f_value);
if (cmp == 0)
{
return mid;
}
if (cmp < 0)
{
low = mid + 1;
}
if (cmp > 0)
{
high = mid - 1;
}
}
return -1;
}
================================================================================
0 Comments