當前位置:首頁 » 編程語言 » c語言數組元素查找

c語言數組元素查找

發布時間: 2024-06-13 22:43:22

A. c語言實現整型數組中查找指定元素的函數

#include<stdio.h>
int search(int a[], int n, int searchValue) {
int i;
for(i=0; i<n; i++) if(a[i]==searchValue) return i;
return -1;
}
int main() {
int i;
int a[10],find,idx;
for(i=0; i<10; i++) {
printf("Input a[%d]:",i);
scanf("%d",&a[i]);
}
printf("Input searchValue:");
scanf("%d",&find);
idx=search(a,10,find);
if(idx!=-1) printf("pos=%d",idx);
else printf("not found");
}

B. 在c語言中輸入數組兩個數組,查找重復元素並輸出怎麼寫啊

可以一次讀入N個數據。可以考慮以回車結束讀入的一組。
參考如下寫法:
#include "stdio.h"
#define Max 100
int X[Max]={0,},Y[Max]={0,};

int main()
{
int i=0,j=0;
int a,b;
char c=0;

printf("輸入第一個數組(以空格分開,回車結束)");
while((c!='\n'))
scanf("%d%c",X+i++,&c);

c=0;
printf("輸入第二個數組(以空格分開,回車結束)");
while((c!='\n'))
scanf("%d%c",Y+j++,&c);

for(a=0;a<i;a++)
for(b=0;b<j;b++)
if(X[a]==Y[b])
printf("%d \t",X[a]);

return 0;
}

C. 在一個數組中查找一個數,用C語言怎麼寫代碼

#include<卜激stdio.h>

intmain(void)

{

inti,j,k=0,sz[10]={5,75,89,428,576,5986,7543,8524,9805,1057};

printf("請輸入要唯讓查找指弊局的數:");

scanf("%d",&j);

for(i=0;i<10;i++)

if(sz[i]==j)

{

printf("sz[%d]=%d ",i,sz[i]);

k++;

}

if(!k)

printf("數組中沒有您要查找的數。 ");

return0;

}

熱點內容
戰地一有什麼不用加速器的伺服器 發布:2025-07-10 08:51:33 瀏覽:404
linux怎麼看自己伺服器的ip 發布:2025-07-10 08:51:29 瀏覽:135
怎麼寫腳本刷視頻 發布:2025-07-10 08:49:51 瀏覽:381
安卓微信在哪裡設置鈴聲 發布:2025-07-10 08:48:05 瀏覽:232
dropbear編譯 發布:2025-07-10 08:27:35 瀏覽:684
我的世界電腦建造伺服器推薦 發布:2025-07-10 08:13:08 瀏覽:401
如何提高存儲數據的速度 發布:2025-07-10 07:55:57 瀏覽:259
規范c語言代碼 發布:2025-07-10 07:55:57 瀏覽:516
在線砍價源碼 發布:2025-07-10 07:55:56 瀏覽:796
編程工作年限 發布:2025-07-10 07:44:42 瀏覽:143