1.一维数组遍历
int main()
{
int a[10] = {0,1,2,5,8,3,4,0,10,-5};
int i;
for (i = 0; i < 10; i++)//数组遍历
{
printf("数组第%d是%d\n", i + 1, a[i]);
}
return 0;
}
2.一维数组求最小值及下标
int main()
{
int a[10] = {0,1,2,-10,8,3,4,0,10,-5};
int i,j;
for (i = 0; i < 10; i++)//数组遍历
{
printf("数组第%d是%d\n", i + 1, a[i]);
}
int min = 0;
for (j =1; j <10; j++)
{
if (a[j]
3.一维数组求最大值及下标
int main()
{
int a[10] = {0,1,2,-10,8,3,4,0,10,-5};
int i,j;
for (i = 0; i < 10; i++)//数组遍历
{
printf("数组第%d是%d\n", i + 1, a[i]);
}
int min = 0;
for (j =1; j <10; j++)
{
if (a[j] >a[min])
min = j;
}
j=min;
printf("%d,%d\n",j,a[j]);
return 0;
}
4.二维数组遍历求最大值及下标
int main()
{
int col, i, j, row;
int a[3][2];
printf("请输入6个数:");
for (i = 0; i < 3; i++)//输入3*2数组
for (j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < 3; i++)//遍历3*2数组
{
for (j = 0; j < 2; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
col = row = 0;//求数组最大值,并输出下标
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
if (a[i][j] > a[col][row])
{
col = i;
row = j;
}
printf("最大值为%d,i为%d,j为%d,第%d行,第%d列", a[col][row], col, row,col+1,row+1);
return 0;
}
5.二维数组遍历求最小值及下标
int main()
{
int col, i, j, row;
int a[3][2];
printf("请输入6个数:");
for (i = 0; i < 3; i++)//输入3*2数组
for (j = 0; j < 2; j++)
scanf("%d", &a[i][j]);
for (i = 0; i < 3; i++)//遍历3*2数组
{
for (j = 0; j < 2; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
col = row = 0;//求数组最小值,并输出下标
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
if (a[i][j]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)