Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
90 views3 pages

Codetantra 1

The document outlines a C program designed to perform a linear search on an array of elements. It details the program's execution flow, including user prompts for inputting the size of the array, the elements, and the key element to search for, along with expected outputs. Several test cases are provided to demonstrate the program's functionality and correctness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views3 pages

Codetantra 1

The document outlines a C program designed to perform a linear search on an array of elements. It details the program's execution flow, including user prompts for inputting the size of the array, the elements, and the key element to search for, along with expected outputs. Several test cases are provided to demonstrate the program's functionality and correctness.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6/5/22, 7:07 PM https://kitsw.codetantra.com/secure/labs-q.jsp?

sNo=2&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=62…

S.No: 2 Exp. Name: Write a C program to Search an element using Linear Search process Date: 2022-04-25

Aim:

Page No:
Write a program to search a key element with in the given array of elements using linear search
process.

ID: B21ME015
At the time of execution, the program should print the message on the console as:

Enter value of n :

For example, if the user gives the input as:

Enter value of n : 3

Next, the program should print the messages one by one on the console as:

Enter element for a[0] :


Enter element for a[1] :
Enter element for a[2] :

if the user gives the input as:

Enter element for a[0] : 89


Enter element for a[1] : 33
Enter element for a[2] : 56

Next, the program should print the message on the console as:

Enter key element :

if the user gives the input as:

Enter key element : 56

then the program should print the result as:

The key element 56 is found at the position 2

2021-2025-MECH-1
Similarly if the key element is given as 25 for the above one dimensional array elements then the program
should print the output as "The Key element 25 is not found in the array".

Note: Do use the printf() function with a newline character ( \n ) at the end.
Source Code:
Kakatiya Institute of Technology and Science

Program509.c

#include<stdio.h>
void read(int[],int);
void search(int[],int,int);
int main()
{
int A[10],n,key;
printf("Enter value of n : ");
scanf("%d",&n);
read(A,n);
printf("Enter key element : ");

https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=2&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=624d7816232d4a06d… 1/3
6/5/22, 7:07 PM https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=2&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=62…
scanf("%d",&key);
search(A,n,key);
}
void read(int A[10],int n)

Page No:
{
int i;
for(i=0;i<n;i++)
{

ID: B21ME015
printf("Enter element for a[%d] : ",i);
scanf("%d",&A[i]);
}
}
void search(int A[10],int n,int key)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(A[i]==key)
{
printf("The key element %d is found at the position %d\n",key,i);
flag=1;
break;
}
}
if(flag==0)
printf("The key element %d is not found in the array\n", key);
}

2021-2025-MECH-1

Execution Results - All test cases have succeeded!


Kakatiya Institute of Technology and Science

Test Case - 1

User Output
Enter value of n : 5
Enter element for a[0] : 45
Enter element for a[1] : 67
Enter element for a[2] : 35
Enter element for a[3] : 28
Enter element for a[4] : 16
Enter key element : 28
The key element 28 is found at the position 3

https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=2&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=624d7816232d4a06d… 2/3
6/5/22, 7:07 PM https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=2&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=62…

Test Case - 2

User Output
Enter value of n : 5

Page No:
Enter element for a[0] : 2
Enter element for a[1] : 7
Enter element for a[2] : 5

ID: B21ME015
Enter element for a[3] : 1
Enter element for a[4] : 4
Enter key element : 2
The key element 2 is found at the position 0

Test Case - 3

User Output
Enter value of n : 4
Enter element for a[0] : 452
Enter element for a[1] : 356
Enter element for a[2] : 754
Enter element for a[3] : 127
Enter key element : 127
The key element 127 is found at the position 3

Test Case - 4

User Output
Enter value of n : 3
Enter element for a[0] : 5
Enter element for a[1] : 7
Enter element for a[2] : 3
Enter key element : 4
The key element 4 is not found in the array

2021-2025-MECH-1
Test Case - 5

User Output
Enter value of n : 3
Enter element for a[0] : 11
Enter element for a[1] : 45
Kakatiya Institute of Technology and Science

Enter element for a[2] : 37


Enter key element : 25
The key element 25 is not found in the array

https://kitsw.codetantra.com/secure/labs-q.jsp?sNo=2&qId=585e4ac40cf2f069ef15727c&bd=AY3RFZHVEQg%3D%3D&lid=624d7816232d4a06d… 3/3

You might also like