1)定义数组时全体初始化:
int a[5]={1,2,3,4,5}
2)定义数组时部分初始化:
int a[5]={1,2}
相当于:
int a[5]={1,2,0,0,0}
3)定义数组后,从键盘输入:
int i,a[5]
for(i=0i<5i++)
scanf("%d",&a[i])
4)定义数组后,从文件输入:
FILE *fp
fp=fopen("in.txt","r")
int i,a[5]
for(i=0i<5i++)
fscanf(fp,"%d",&a[i])
5)定义数组后,使用随机函数赋值:
int i,a[5]
for(i=0i<5i++)
a[i]=rand()%100
6)定义数组后,从别的数组复制(或别的计算方法获得):
int i,a[5]
for(i=0i<5i++)
a[i]=b[i] //b[]数组已经定义,并已赋值
#include <stdio.h>int search(int A[], int n, int key)
{
int low, high, mid
low = 0
high = n - 1
while (low <= high) {
mid = (low + high) / 2
if (A[mid] == key) {
return mid
}
if (A[mid] < key) {
low = mid + 1
}
if (A[mid] > key) {
high = mid - 1
}
}
return -1
}
int main()
{
int arry[] = { 12, 23, 34, 56, 78, 98, 256, 789 }
int key
printf("请输入需要查找的数字:")
scanf("%d",&key)
int ret = search(arry, sizeof(arry),key)
if (-1 == ret) {
printf("未找到所需要查找的数字:")
}
else {
printf("找到所需数字,其下标为%d\n",ret)
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)