Question
Write a C program that searches for an element in an array of integers. If the array contains multiple occurrences of the searched element, then
Write a C program that searches for an element in an array of integers. If the array contains multiple occurrences of the searched element, then the output should be of the following form:
The element is found at positions 3, 6, 7
I used the following code, but there's an extra comma coming at the end in the output. And if I remove the comma from printf, then there are no commas at all. The output should have commas in between the elements, except the last element.
printf("The element is found at positions ");
if found for (i = 0; i < n; i++)
{
if (arr[i] == element) {
printf("%d, ", i+1);
found = 1;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started