第五次上机作业:(请用指针实现)
数据移位
有n(n<=10)个整数,使前面各数顺序向后移m(m<=n)个位置,最后m个数变成最前面的m个数,如图所示。编写函数shift(a,n,m)实现上述功能,在主函数中输入n个整数和输出调整后的n个数。
SAMPLE OUTPUT
Input n(n<=10):10
Input 10 integers:1 2 3 4 5 6 7 8 9 10
Output the original:1 2 3 4 5 6 7 8 9 10
Input m(m<=10):4
Output the shifted:7 8 9 10 1 2 3 4 5 6
#include
#define N 10
void shift(int a[ ],int n,int m);
void main( )
{int i,n,m;
int a[N];
printf("Input n(n<=10):");
scanf("%d",&n);
printf("Input %d integers:\n",n);
for(i=0;i
scanf("%d",a+i);
printf("Output the original:\n");
for(i=0;i
printf("%d ",*(a+i));
printf("\n");
printf("Input m(m<=%d):",n);
scanf("%d",&m);
shift(a,n,m);
printf("Output the shifted:\n");
for(i=0;i
printf("%d ",*(a+i));
printf("\n");
}
void shift(int a[ ],int n,int m)
{int i,j,t;
for(i=0;i
{t=a[n-1];
for(j=n-2;j>=0;j--)
a[j+1]=a[j];
a[0]=t;
}
}
字符串排序
在主函数中输入10个不等长的字符串放入二维字符数组中,编写函数sort()利用指针数组对其排序,在主函数中输出排好序的字符串。
#include
#include
#define N 10
#define M 80
void sort(char *s[ ]);
void main()
{char s[N][M],*strp[N];
int i;
printf("Input 10 strings:\n");
for(i=0;i
gets(s[i]);
for(i=0;i
strp[i]=s[i];
sort(strp);
printf("Output sorted strings:\n");
for(i=0;i
puts(strp[i]);
}
void sort(char *s[ ])
{
int i,j,k;
char *p;
for(i=0;i
{k=i;
for(j=i+1;j
if(strcmp(s[k],s[j])>0)
k=j;
f(i!=j)
{p=s[k];
s[k]=s[i];
s[i]=p;
}
}
}
学生成绩统计
有一个班级30个学生5门课,编写函数实现下列功能:
(1)函数average()求第一门课的平均分;
(2)函数findfail()找出有2门以上(含2门)课程不及格的学生,输出其学号和全部课程成绩及平均成绩;
(3)函数findgood()找出平均成绩在90分以上或全部课程成绩在85分以上的学生。
在主函数中输入数据并输出结果。
#include
#define N 30
#define M 5
int average(int a[ ][M+3]);
void findfail(int a[ ][M+3]);
void findgood(int a[ ][M+3]);
void main( )
{int s[N][M+3],i,j;
int ave;
printf("Input 5 scores for each student:\n");
for(i=0;i
{s[i][0]=i+1;
printf("%d:",i+1);
for(j=1;j<=M;j++)
scanf("%d",&s[i][j]);
printf("\n");
}
ave