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;
}