一、实验目的和要求1.掌握一维数组的定义。2.掌握二维数组的定义。3.掌握C语言数组的引用方法。4.掌握数组的输入和输出方法。二、实验内容和步骤1.编写程序,测试下列数组的定义方式是否正确,如果不正确,请说明原因并改正。(1)测试程序如下。#include int main(){int num;scanf("%d",&num);int score[num];return 0;}(2)测试程序如下。#define N 10#include int main(){int score[N];scanf("%d",&score[3]);printf("%d",score[3]);return 0;}(3)测试程序如下。#include int main(){int score[];scanf("%d",&score[1]);return 0;}(4)测试程序如下。#include int main(){int score[2*3-2];scanf("%d",&score[1]);printf("%d",score[1]);return 0;}(5)测试程序如下。#define I 3#define J 4#include int main(){int a[I][J];scanf("%d",&a[1][2]);printf("%d",a[1][2]);return 0;}(6)测试程序如下。#define N 3#include int main(){int a[][N];scanf("%d",&a[1][2]);printf("%d",a[1][2]);return 0;}
运行下面的C程序,并分析运行结果。(1)分析运行结果一#includeint main(){char letter[5]={'a','b','c','d','e'};int i;for(i=0;i<5;i++)printf("%c",letter[i]);return 0;}(2)分析运行结果二#includeint main(){static int n[][4]={{1,2},{3,4,5},{6,7,8,9}};int i,j;for(i=0;i<3;i++)for(j=0;j<4;j++)printf("%d",n[i][j]);return 0;}(3)分析运行结果三#includeint main(){static int n[10]={1,0,0,0,0,0,0,0,0,0};int i,j;for(i=0;i<10;i++)for(j=i;j<10;j++)n[i]=n[i]+n[j];for(i=0;i<10;i++)printf("%d",n[i]);return 0;}(4)分析运行结果四#includeint main(){int matrix[5][5];int i,j;for(i=0;i<5;i++){matrix[i][i]=1;for(j=0;j<5;j++)if(i!=j)matrix[i][j]= matrix[4-j][4-i]+ matrix[j][j];}for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%5d ",matrix[i][j]);printf("\n");}return 0;}(5)分析运行结果五#includeint m[3][3]={{1},{2},{3}};int n[3][3]={1,2,3};int main(){printf("%d\n",m[1][0]+n[0][0]); /*1.结果*/printf("%d\n",m[0][1]+n[1][0]); /*2.结果*/return 0;}(6)分析运行结果六#includeint main(){char str[3][10]={"This","is","test!"};printf("str[2]=%s,str[2][1]=%c,str=%s\n",str[2],str[1][1],str);return 0;}