返回指针值的函数

练习03-18-02

有三个学生,四门课的成绩,要求在用户输入用户的学生序号以后,能输出该学生的全部学习成绩。

输出样例:

The matrix of scores:
  12.00  11.00  52.00  79.00
  86.00  67.00  55.00  59.00
  77.00  16.00  20.00  88.00
Enter the number of the student:
2
The scores of NO.2 are:
  77.00  16.00  20.00  88.00
--------------------------------
Process exited after 3.375 seconds with return value 0
请按任意键继续. . .

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	float *search(float (*p)[4],int n);
	int i,j;
	float score[3][4];
	srand((unsigned)time(NULL));
	printf("The matrix of scores:\n");
	for(i=0;i<3;++i){
		for(j=0;j<4;++j){
			score[i][j]=rand()%100;
			printf("%7.2f",score[i][j]);
		}putchar('\n');
	}
	printf("Enter the number of the student:\n");
	int k;
	scanf("%d",&k);
	printf("The scores of NO.%d are:\n",k);
	float *p=search(score,k);
	for(i=0;i<4;++i){
		printf("%7.2f",*(p+i));
	}
	return 0;
}
float *search(float (*p)[4],int n)
{
	float *p1;
	p1=*(p+n);
	return p1;
}

输出成绩不及格学生的成绩

The matrix of scores:
  18.00  81.00  65.00  30.00
  49.00  44.00  63.00  14.00
  35.00  27.00  64.00  32.00

The scores of No.1 are:   18.00  81.00  65.00  30.00
The scores of No.2 are:   49.00  44.00  63.00  14.00
The scores of No.3 are:   35.00  27.00  64.00  32.00
--------------------------------
Process exited after 0.05601 seconds with return value 7
请按任意键继续. . .

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	float *search(float (*p)[4]);
	int i,j;
	float score[3][4];
	srand((unsigned)time(NULL));
	printf("The matrix of scores:\n");
	for(i=0;i<3;++i){
		for(j=0;j<4;++j){
			score[i][j]=rand()%100;
			printf("%7.2f",score[i][j]);
		}putchar('\n');
	}
	float *p;
	for(i=0;i<3;++i){
		p=search(score+i);
		if(p==*(score+i)){
			printf("\nThe scores of No.%d are: ",i+1);
			for(j=0;j<4;++j){
				printf("%7.2f",*(p+j));	
			}

		}
	}
}
float *search(float (*p)[4])
{
	float *p1=NULL;
	int i;
	for(i=0;i<4;++i){
		if(*(*p+i)<60){
			p1=*p;
			break;
		}
	}
	return p1;
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值