當前位置:首頁 » 編程語言 » 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-04-23 04:53:00 瀏覽:65
文件夾加固 發布:2025-04-23 04:52:11 瀏覽:914
android消息傳遞 發布:2025-04-23 04:50:45 瀏覽:796
sky伺服器錯誤什麼意思 發布:2025-04-23 04:50:02 瀏覽:379
三星usb存儲設備在哪 發布:2025-04-23 04:43:31 瀏覽:499
把什麼塗在密碼鎖上能看到密碼 發布:2025-04-23 04:29:40 瀏覽:242
sql2000密碼忘記 發布:2025-04-23 04:22:03 瀏覽:21
安卓手機退出應用怎麼絲滑 發布:2025-04-23 04:17:46 瀏覽:107
小米全盤加密 發布:2025-04-23 04:14:24 瀏覽:741
pac腳本代理伺服器地址 發布:2025-04-23 04:08:44 瀏覽:954