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

Hasti

The document outlines a practical assignment for the course 'Data Structure and Algorithm' focused on implementing a linear search algorithm in C. It includes code for reading input, performing the search, and displaying whether the target number is found in the array. The program prompts the user for the size of the array and the number to search for, returning the index if found or indicating that the number is not present.

Uploaded by

useindus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Hasti

The document outlines a practical assignment for the course 'Data Structure and Algorithm' focused on implementing a linear search algorithm in C. It includes code for reading input, performing the search, and displaying whether the target number is found in the array. The program prompts the user for the size of the array and the number to search for, returning the index if found or indicating that the number is not present.

Uploaded by

useindus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Enrollment : IU2441231811

Name: Hasti jethwa

Course code: CE0417


Course name: Data Structure and
Algorithm

Practical 1
Aim: Write a program to implement
following searching algorithms.
1.1 Linear Search

Code:
#include <stdio.h>
void readInput(int values[], int size)
{
int i;
printf("Please enter array elements(%d) : ", size);
for(i = 0; i < size; i++)
{
scanf("%d", &values[i]);
}
}
int linear_search(int values[], int size, int
targetNum)
{
int i;
for(i = 0; i < size; i++)
{
Enrollment : IU2441231811
Name: Hasti jethwa

if(values[i] == targetNum)
{
return i;
}
}
return 0;
}
int main()
{
clrscr();
int values[100];
int targetNum, i, size;

printf("Enter size of array: ");


scanf("%d", &size);
readInput(values, size);

printf("Enter a Number to search in Array: ");


scanf("%d", &targetNum);

int index = linear_search(values, size,


targetNum);

if(index == 0)
printf("NOT FOUND - The %d number Not
Found in Array\n", targetNum);
else
Enrollment : IU2441231811
Name: Hasti jethwa

printf("FOUND - The %d number Found at


index %d\n", targetNum, index);

return 0;
}

OUTPUT:

You might also like