用指向数组的指针作为函数参数

练习03-17-02

输出样例:

The matrix of score:
  22.00  75.00  74.00  53.00
   9.00  95.00  48.00  60.00
  85.00  18.00  32.00  30.00
The average score of whole student =  50.08
The scores of first student:
The score of class 1 =   22.00
The score of class 2 =   75.00
The score of class 3 =   74.00
The score of class 4 =   53.00

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


代码如下:

//有一个班,三个学生,各学四门课,计算总平均分数和第n个学生的成绩
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
	float average(float *p,int n);
	void  search(float (*p)[4],int n);
	float score[3][4];
	srand((unsigned)time(NULL));
	int i,j;
	printf("The matrix of score:\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 ave=average(*score,12);
	printf("The average score of whole student =%7.2f\n",ave);
	printf("The scores of first student:\n");
	search(score,0);
	return 0;
 } 
 float average(float *p,int n)
 {
 	float sum=0,ave;
 	float *i=p;
 	for(;i<p+n;++i){
 		sum+=*i;
	 }
	 ave=sum/n;
	 return ave;
 }
 void search(float (*p)[4],int n)
 {
 	int i;
 	for(i=0;i<4;++i){
 		printf("The score of class %d = %7.2f\n",i+1,*(*(p+n)+i));
	 }
 }

练习hanoi塔问题

//The problem of hanoi
#include<stdio.h>
int main()
{
	void hanoi(int n,char one, char two, char three);
	int n;
	printf("Input the number of discs:\n");
	scanf("%d",&n);
	printf("The steps to move:\n");
	hanoi(n,'A','B','C');
	return 0;
 } 
 void hanoi(int n,char one, char two, char three)
 {
 	void move(char x, char y);
 	if(n==1){
 		move(one,three);
	 }else{
	 	hanoi(n-1,one,three,two);
	 	move(one,three);
	 	hanoi(n-1,two,one, three);
	 }
  } 
  void move(char x,char y)
  {
  	printf("%c->%c\n",x,y);
  }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值