天天看点

用指向数组的指针作为函数参数(2)

练习03-17-03

从三个学生四门成绩的二维数组中,寻找到有成绩不及格的学生,输出对应成绩:

输出样例:

The original score matrix:
  75.00  31.00  44.00  21.00
  12.00  82.00  37.00  48.00
  51.00  99.00   7.00  32.00
The list of failed student:
The score of NO1:  75.00  31.00  44.00  21.00
The score of NO2:  12.00  82.00  37.00  48.00
The score of NO3:  51.00  99.00   7.00  32.00

--------------------------------
Process exited after 0.054 seconds with return value 0
请按任意键继续. . .

           

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	srand((unsigned)time(NULL));
	float score[3][4];
	int i,j;
	printf("The original score matrix:\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("The list of failed student:\n");
	void find_fail(float (*p)[4],int n);
	find_fail(score,3);
	return 0;
}
void find_fail(float (*p)[4],int n)
{
	int i,j,flag=0;
	void search(float (*p)[4],int n);
	for(i=0;i<3;++i){
		int count=0;
		for(j=0;j<4;++j){
			if(*(*(p+i)+j)<60)	++count;
		}
		if(count>0){
			search(p+i,i);
		}
	}
}
void search(float (*p)[4],int n)
{
	printf("The score of NO%d:",n+1);
	int i;
	for(i=0;i<4;++i){
		printf("%7.2f",*(*p+i));
	}
	putchar('\n');
}
           

继续阅读