C Primer Plus 第十章 编程练习 1-11题

    xiaoxiao2021-03-25  85

    第一题 #include<stdio.h> #define Months 12 #define Years 5 int main(void) { const float rain[Years][Months] = { {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.5,2.1,3.6,1.1,0.9}, {1.1,2.2,2.2,2.2,1.1,2.2,3.3,1.2,2.1,2.2,3.3,2.1}, {1.2,3.1,0.2,0.4,1.5,1.4,1.8,2.4,2.6,2.5,3.1,1.7}, {1.1,4.2,3.3,2.2,1.5,2.9,4.2,1.1,2.4,3.9,0.9,0.7}, {3.1,2.8,1.9,1.8,1.3,2.9,3.1,1.1,0.8,0.8,0.5,1.5} }; printf("YEAR RAINFALL\n"); float SumRain = 0; for(int Y = 0 ; Y < Years ; ++Y) { float RainOfYear = 0; for(int M = 0 ; M < Months ; ++M) RainOfYear += *(*(rain + Y) + M); printf("M .1f\n",2000+Y,RainOfYear); SumRain += RainOfYear; } printf("THe Yearly Average Is %.2F Inches.\n",SumRain/Years); printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n"); for(int M = 0 ; M < Months ; ++M) { float RainOfMonth = 0; for(int Y = 0 ; Y < Years ; ++Y) RainOfMonth += *(*(rain + Y) + M); printf("%-4.1f",RainOfMonth/Months); } return 0; } 第二题 #include<stdio.h> #define LEN 5 void copy_ptr(const double* sourceArr , double* tarArr , int Len); void copy_arr(const double sourceArr[] , double tarArr[] , int Len); int main(void) { double source[LEN] = {1.1,2.2,3.3,4.4,5.6}; double target_1[LEN]; double target_2[LEN]; copy_ptr(source,target_1,LEN); copy_ptr(source,target_2,LEN); printf("Array_ptr"); for(int i = 0 ; i < LEN ; ++i) printf("%-4.2lf ",*(target_1+i)); printf("\n"); printf("Array_arr"); for(int i = 0 ; i < LEN ; ++i) printf("%-4.2lf ",target_2[i]); printf("\n"); return 0; } void copy_ptr(const double* sourceArr , double* tarArr , int Len) { for(int i = 0 ; i < Len ; ++i) *(tarArr+i) = *(sourceArr+i); } void copy_arr(const double sourceArr[] , double tarArr[] , int Len) { for(int i = 0 ; i < Len ; ++i) tarArr[i] = sourceArr[i]; } 第三题 #include<stdio.h> #define LEN 10 int MaxInt(const int* arr , int Len); int main(void) { int TestArr[LEN] = {1,2,3,4,5,6,7,8,9,50}; printf("Max Integer Is %d\n",MaxInt(TestArr,LEN)); return 0; } int MaxInt(const int* arr , int Len) { int Max = *arr; for(int i = 0 ; i < Len ; ++i) { if(Max < *(arr+i)) Max = *(arr+i); } return Max; } 第四题 #include<stdio.h> #define LEN 10 int MaxIndex(const double* arr , int Len); int main(void) { double TestArr[LEN] = {1.1,2.2,3.3,4.1,5.2,6.3,7.1,8.3,9.8,50.1}; printf("Max Integer Is %d\n",MaxIndex(TestArr,LEN)); return 0; } int MaxIndex(const double* arr , int Len) { int maxIndex = 0; double Max = *arr; for(int i = 0 ; i < Len ; ++i) { if(Max < *(arr+i)) maxIndex = i; } return maxIndex; } 第五题 #include<stdio.h> #define LEN 10 double Lenght(const double* arr , int Len); int main(void) { double TestArr[LEN] = {1.1,2.2,3.3,4.1,5.2,6.3,7.1,8.3,9.8,50.1}; printf("Result Is %.2lf\n",Lenght(TestArr,LEN)); return 0; } double Lenght(const double* arr , int Len) { double Max = *arr; double Min = *arr; for(int i = 0 ; i < Len ; ++i) { if(Max < *(arr+i)) Max = *(arr+i); if(Min > *(arr+i)) Min = *(arr+i); } return Max-Min; } 第六题 #include<stdio.h> #define COLUMN 5 #define ROW 4 void copy_ptr(const double (*sourceArr)[COLUMN] , double (*tarArr)[COLUMN] , int Len); int main(void) { const double sourceArr[ROW][COLUMN] = { {1.1,2.2,3.3,4.4,5.5}, {1.1,1.1,1.1,1.1,1.1}, {3.3,3.1,3.2,6.7,8.6}, {4.4,4.4,4.4,4.4,4.4} }; double targetArr[ROW][COLUMN]; copy_ptr(sourceArr,targetArr,ROW); for(int i = 0 ; i < ROW ; ++i) { for(int j = 0 ; j < COLUMN ; ++j) printf("%-5.2lf",*(*(targetArr+i)+j)); printf("\n"); } return 0; } void copy_ptr(const double (*sourceArr)[COLUMN] , double (*tarArr)[COLUMN] , int Len) { for(int i = 0 ; i < Len ; ++i) { for(int j = 0 ; j < COLUMN ; ++j) *(*(tarArr+i)+j) = *(*(sourceArr+i)+j); } } 第七题 #include<stdio.h> #define LEN 3 #define Index 2 void copyPtr(const int* sourceArr , int* targetArr , int Len); int main(void) { int sourceArray[7] = {1,2,3,4,5,6,7}; int targetArray[LEN]; copyPtr(sourceArray+Index,targetArray,LEN); for(int i = 0 ; i < LEN ; ++i) printf("%-3d",*(targetArray+i)); return 0; } void copyPtr(const int* sourceArr , int* targetArr , int Len) { for(int i = 0 ; i < Len ; ++i) *(targetArr+i) = *(sourceArr+i); } 第八题 #include<stdio.h> #define ROW 3 #define COLUMN 5 void copyPtr(int column , int row , const int sourceArr[row][column] , int targetArr[row][column]); void showArray(int column , int row , int Arr[ROW][column]); int main(void) { const int sourceArray[ROW][COLUMN] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} }; int targetArray[ROW][COLUMN]; copyPtr(COLUMN,ROW,sourceArray,targetArray); showArray(COLUMN,ROW,targetArray); return 0; } void copyPtr(int column , int row , const int sourceArr[row][column] , int targetArr[row][column]) { for(int i = 0 ; i < row ; ++i) { for(int j = 0 ; j <column ; ++j) targetArr[i][j] = sourceArr[i][j]; } } void showArray(int column , int row , int Arr[ROW][column]) { for(int i = 0 ; i < row ; ++i) { for(int j = 0 ; j < column ; ++j) printf("%-5d",Arr[i][j]); printf("\n"); } } 第九题 #include<stdio.h> #define LEN 5 void sumArray(const int* sourceArr_1 , const int* sourceArr_2 , int* targetArr , int Len); int main(void) { int Arr_1[LEN] = {1,2,3,4,5}; int Arr_2[LEN] = {6,7,8,9,10}; int sumArr[LEN]; sumArray(Arr_1,Arr_2,sumArr,LEN); for(int i = 0 ; i < LEN ; ++i) printf("%-5d",*(sumArr+i)); return 0; } void sumArray(const int* sourceArr_1 , const int* sourceArr_2 , int* targetArr , int Len) { for(int i = 0 ; i < Len ; ++i) *(targetArr+i) = *(sourceArr_1+i) + *(sourceArr_2+i); } 第十题 #include<stdio.h> #define ROW 3 #define COLUMN 5 void doubleArray(int (*arr)[COLUMN] , int Len); void showArray(int (*arr)[COLUMN] , int Len); int main(void) { int TestArray[ROW][COLUMN] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} }; printf("SourceArray\n"); showArray(TestArray,ROW); doubleArray(TestArray,ROW); printf("DoubleArray\n"); showArray(TestArray,ROW); return 0; } void doubleArray(int (*arr)[COLUMN] , int Len) { for(int i = 0 ; i < Len ; ++i) { for(int j = 0 ; j < COLUMN ; ++j) *(*(arr+i)+j) *= 2; } } void showArray(int (*arr)[COLUMN] , int Len) { for(int i = 0 ; i < Len ; ++i) { for(int j = 0 ; j < COLUMN ; ++j) printf("%-4d",*(*(arr+i)+j)); printf("\n"); } } 第十一题 #include<stdio.h> #define Months 12 #define Years 5 float showSumRain(const float (*arr)[Months] , int Len); void showAvgRain(const float (*arr)[Months] , int Len); int main(void) { const float rain[Years][Months] = { {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.5,2.1,3.6,1.1,0.9}, {1.1,2.2,2.2,2.2,1.1,2.2,3.3,1.2,2.1,2.2,3.3,2.1}, {1.2,3.1,0.2,0.4,1.5,1.4,1.8,2.4,2.6,2.5,3.1,1.7}, {1.1,4.2,3.3,2.2,1.5,2.9,4.2,1.1,2.4,3.9,0.9,0.7}, {3.1,2.8,1.9,1.8,1.3,2.9,3.1,1.1,0.8,0.8,0.5,1.5} }; printf("YEAR RAINFALL\n"); float SumRain = showSumRain(rain,Years); printf("THe Yearly Average Is %.2F Inches.\n",SumRain/Years); printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n"); showAvgRain(rain,Years); return 0; } float showSumRain(const float (*arr)[Months] , int Len) { float SumRain = 0; for(int Y = 0 ; Y < Len ; ++Y) { float RainOfYear = 0; for(int M = 0 ; M < Months ; ++M) RainOfYear += *(*(arr + Y) + M); printf("M .1f\n",2000+Y,RainOfYear); SumRain += RainOfYear; } return SumRain; } void showAvgRain(const float (*arr)[Months] , int Len) { for(int M = 0 ; M < Months ; ++M) { float RainOfMonth = 0; for(int Y = 0 ; Y < Len ; ++Y) RainOfMonth += *(*(arr + Y) + M); printf("%-4.1f",RainOfMonth/Months); } }
    转载请注明原文地址: https://ju.6miu.com/read-15259.html

    最新回复(0)