SAVITIBAI PHULE UNIVERSITY OF PUNE
S. Y. B.B.A. (C.A.) Semester III
Practical Examination
Lab Course: (CA-306) Computer Laboratory Based on 302, 304 and 305 Data Structure, Angular JS/ PHP, Big Data / Block Chain
/* Q.1 B) 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<stdlib.h>
#define MAX 30
void CreateArray(char A[][MAX], int n)
{
int i = 0;
fflush(stdin);
printf("\n\nEnter the City names to store into the Array: \n");
for ( i = 0; i < n; i++)
fgets(A[i], sizeof(A[i]), stdin);
}
void DisplayArray(char A[MAX][MAX], int n)
{
int i = 0;
printf("\n\nThe City names are: \n");
for ( i = 0; i < n; i++)
printf("%s", A[i]);
}
void LinearSearch(char A[][MAX], int n, char key[])
{
int i = 0, flag = 0;
for ( i = 0; i < n; i++){
if ( strcmp(A[i], key) == 0){
flag = 1;
break;
}
}
if (flag == 1)
printf("\n\nThe %s city search at %d position in the Array", key, i+1);
else
printf("\n %s city not found in the Array\n", key);
}
int main(){
char A[MAX][MAX], key[MAX];
int num = 0, i=0;
printf("Enter How many cities we want to in the array: ");
scanf("%d", &num);
CreateArray(A, num);
DisplayArray(A, num);
printf("\n\nEnter City name we want to search in the array: ");
fgets(key, sizeof(key), stdin);
LinearSearch(A, num, key);
printf("\n\n");
system("pause");
return 0;
}
No comments:
Post a Comment