day9 c语言学习

task1

二维数组作为参数传递,完成二维数组中元素的输入输出

#include <stdio.h>
const int line = 3, row = 3;
//implementation of the array input
void inputArray(int (*p)[row], int line, int row){
	for (int i = 0;i < line;i++){
		for(int j = 0; j < row; j++){
			printf("entering a number into arr[%d][%d]:",i,j);
			scanf("%d",*(p+i)+j);
		}
	}
}
//implementation of the array output
void outputArray(int (*p)[row], int line, int row){
	printf("******************************\n");
	printf("printing out the array:\n");
	for(int i = 0; i < line;i++){
		for(int j = 0; j < row; j++){
			printf("arr[%d][%d] = %d\n",i,j,*(*(p+i)+j));
		}
	}
}
int main(int argc, const char *argv[])
{
	int arr[line][row];
	inputArray(arr,line,row);
	outputArray(arr,line,row);
	return 0;
}

task2

封装自己的strlen函数,int my_strlen(char *s);

#include <stdio.h>
int my_strlen(char *s){
	int count = 0;
	while(*s){
		s++;
		count++;
	}
	return count;
}
int main(int argc, const char *argv[])
{	
	char *s = "China";
	printf("the length of the string is %d\n",my_strlen(s));
	return 0;
}

task 3

猴子吃桃问题,循环算法:

#include <stdio.h>
int peach(int day){
	int peachNum = 1;
	while(day > 1){
		day--;
		peachNum += 1;
		peachNum *= 2;
		printf("day %d, having %d peaches left\n",day,peachNum);
		
	}
	return peachNum;

}
int main(int argc, const char *argv[])
{
	int a = 10;
	int total = peach(a);
	printf("the total number = %d\n",total);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值