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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!