您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 股票报告 > 大连理工c语言第五次上机作业参考答案
第五次上机作业:(请用指针实现)1.数据移位有n(n=10)个整数,使前面各数顺序向后移m(m=n)个位置,最后m个数变成最前面的m个数,如图所示。编写函数shift(a,n,m)实现上述功能,在主函数中输入n个整数和输出调整后的n个数。SAMPLEOUTPUTInputn(n=10):10Input10integers:12345678910Outputtheoriginal:12345678910Inputm(m=10):4Outputtheshifted:78910123456#includestdio.h#defineN10voidshift(inta[],intn,intm);voidmain(){inti,n,m;inta[N];printf(Inputn(n=10):);scanf(%d,&n);printf(Input%dintegers:\n,n);for(i=0;in;i++)scanf(%d,a+i);printf(Outputtheoriginal:\n);for(i=0;in;i++)printf(%d,*(a+i));printf(\n);printf(Inputm(m=%d):,n);scanf(%d,&m);shift(a,n,m);printf(Outputtheshifted:\n);for(i=0;in;i++)printf(%d,*(a+i));printf(\n);}voidshift(inta[],intn,intm){inti,j,t;for(i=0;im;i++){t=a[n-1];for(j=n-2;j=0;j--)a[j+1]=a[j];a[0]=t;}}mn2.字符串排序在主函数中输入10个不等长的字符串放入二维字符数组中,编写函数sort()利用指针数组对其排序,在主函数中输出排好序的字符串。#includestdio.h#includestring.h#defineN10#defineM80voidsort(char*s[]);voidmain(){chars[N][M],*strp[N];inti;printf(Input10strings:\n);for(i=0;iN;i++)gets(s[i]);for(i=0;iN;i++)strp[i]=s[i];sort(strp);printf(Outputsortedstrings:\n);for(i=0;iN;i++)puts(strp[i]);}voidsort(char*s[]){inti,j,k;char*p;for(i=0;iN-1;i++){k=i;for(j=i+1;jN;j++)if(strcmp(s[k],s[j])0)k=j;f(i!=j){p=s[k];s[k]=s[i];s[i]=p;}}}3.学生成绩统计有一个班级30个学生5门课,编写函数实现下列功能:(1)函数average()求第一门课的平均分;(2)函数findfail()找出有2门以上(含2门)课程不及格的学生,输出其学号和全部课程成绩及平均成绩;(3)函数findgood()找出平均成绩在90分以上或全部课程成绩在85分以上的学生。在主函数中输入数据并输出结果。#includestdio.h#defineN30#defineM5intaverage(inta[][M+3]);voidfindfail(inta[][M+3]);voidfindgood(inta[][M+3]);voidmain(){ints[N][M+3],i,j;intave;printf(Input5scoresforeachstudent:\n);for(i=0;iN;i++){s[i][0]=i+1;printf(%d:,i+1);for(j=1;j=M;j++)scanf(%d,&s[i][j]);printf(\n);}ave=average(s);printf(Theaverageofthefirstcourseis%d\n,ave);findfail(s);printf(Thestudentswhofailedin2ormorecourses:\n);for(i=0;iN;i++){if(s[i][M+2]=='N'){for(j=0;jM+2;j++)printf(%-5d,s[i][j]);printf(\n);}}findgood(s);printf(Thestudentswhoseaverage90oreachcourse85:\n);for(i=0;iN;i++){if(s[i][M+2]=='Y'){for(j=0;jM+2;j++)printf(%-5d,s[i][j]);printf(\n);}}}intaverage(inta[][M+3]){inti,sum=0;for(i=0;iN;i++)sum+=a[i][1];return(sum/N);}voidfindfail(ints[][M+3]){inti,j,count,sum;for(i=0;iN;i++){count=0;sum=0;for(j=1;j=M;j++){sum+=s[i][j];if(s[i][j]60)count++;}s[i][M+1]=sum/M;s[i][M+2]=0;if(count=2)s[i][M+2]='N';}}voidfindgood(ints[][M+3]){inti,j,count;for(i=0;iN;i++){count=0;for(j=1;j=M;j++)if(s[i][j]=85)count++;if(count==M||s[i][M+1]=90)s[i][M+2]='Y';}}4.矩阵乘法在主函数中输入m和n(0m,n=5),分别定义矩阵AmXn,BnXm和CmXm,从键盘读入矩阵A和B中元素的值。调用函数multiply()计算矩阵C=AXB。并在主函数中输出矩阵C。SAMPLEOUTPUTInputmandn(1~5):43InputA4X3:123456789101112InputB3X4:1234456878910C=AXBis:30364250668196116102126150182138171204248#includestdio.h#defineN5voidmultiply(inta[][N],intb[][N],intc[][N],intm,intn);voidmain(){inta[N][N],b[N][N],c[N][N];inti,j,m,n;printf(Inputmandn(1~5):);scanf(%d%d,&m,&n);printf(InputA%d*%d:\n,m,n);for(i=0;im;i++)for(j=0;jn;j++)scanf(%d,&a[i][j]);printf(InputB%d*%d:\n,n,m);for(i=0;in;i++)for(j=0;jm;j++)scanf(%d,&b[i][j]);multiply(a,b,c,m,n);printf(C=A*B:\n);for(i=0;im;i++){for(j=0;jm;j++)printf(%-5d,c[i][j]);printf(\n);}}voidmultiply(inta[][N],intb[][N],intc[][N],intm,intn){inti,j,k;for(i=0;im;i++)for(j=0;jm;j++){c[i][j]=0;for(k=0;kn;k++)c[i][j]+=a[i][k]*b[k][j];}}
本文标题:大连理工c语言第五次上机作业参考答案
链接地址:https://www.777doc.com/doc-5791985 .html