C 在数组中查找元素的步骤

C 在数组中查找元素的步骤,c,arrays,C,Arrays,在这段代码中,如果我必须找到一个元素“7”,它将指向array=2的位置, 但是如何得到多个位置,如果数组有[4,7,7,8,9],那么答案应该指向数组=1和数组=2的位置 #include<stdio.h> int main() { int i; int a[5]={4,5,7,8,9}; int ele,temp=0,pos=0; printf("Enter the element to be search\n"); scanf("%d",&ele);

在这段代码中,如果我必须找到一个元素“7”,它将指向array=2的位置, 但是如何得到多个位置,如果数组有[4,7,7,8,9],那么答案应该指向数组=1和数组=2的位置

#include<stdio.h>


int main()
{
 int i;
 int a[5]={4,5,7,8,9};
 int ele,temp=0,pos=0;
 printf("Enter the element to be search\n");
 scanf("%d",&ele);


 // searching for the element

 for (i=0; i<5; i++)
 {
    a[i]=a[i];
     if (a[i]==ele)
     {
       temp=1;
       pos=i;
     }


  }

   if (temp==1)
   printf("Element found %d , position==%d,",ele,pos);
   else
   printf("Element not found\n");
} 
#包括
int main()
{
int i;
int a[5]={4,5,7,8,9};
内部元件,温度=0,位置=0;
printf(“输入要搜索的元素\n”);
scanf(“%d”、&ele);
//搜索元素

对于(i=0;i您的pos变量只是一个整数,而不是一个数组,因此它将只存储一个值。相反,将其设置为一个数组,然后将每个创建的结果的值存储到pos数组中。

试试这个

#include<stdio.h>


int main()
{
 int i;
 int a[5]={4,5,7,8,9};
 int found_indices[5]; // array used to store indices of found entries..
 int count = 0; //n entries found;
 int ele;
 printf("Enter the element to be search\n");
 scanf("%d",&ele);


 // searching for the element

 for (i=0; i<5; i++)
 {
     //a[i]=a[i];
     if (a[i]==ele)
     {
       found_indices[count ++] = i; // storing the index of found entry
     }
  }

   if (count!=0) {
       for (i=0; i<count; i++)
           printf("Element found %d , position==%d,", ele, found_indices[i]);
   }
   else
       printf("Element not found\n");
}
#包括
int main()
{
int i;
int a[5]={4,5,7,8,9};
int found_index[5];//用于存储已找到项的索引的数组。。
int count=0;//找到n个条目;
INTELE;
printf(“输入要搜索的元素\n”);
scanf(“%d”、&ele);
//搜索元素

对于(i=0;i您可以在循环中放置一些
printf
(即,在第一个
if
的主体中)谢谢,请您详细描述如何收集结果值。