Data Structures Practical 2
Question 1
Modify the linear search program to find and print all occurrences of the target value in the array.
multisearch.c
C
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[100], i, size, num, count = 0;
clrscr();
printf("Enter the number of elements in the array: ");
scanf("%d", &size);
printf("Enter %d elements:\n", size);
for(i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element you want to search: ");
scanf("%d", &num);
printf("\n--- Search Results ---\n");
for(i = 0; i < size; i++)
{
if(arr[i] == num)
{
printf("Element found at position %d\n", i + 1);
count++;
}
}
if(count > 0) {
printf("\nTotal occurrences found: %d", count);
} else {
printf("\nElement %d is not present in the array.", num);
}
getch();
}
Output
