本篇內容主要是是對二維數組操作,掌握二維數組作為參數傳遞時,如何用指針表示。
第10題
double? sum_2diagonal(double* array_2d,? unsigned int m, unsigned int n);
功? 能:求出二維數組中兩條對角線上元素之和
參? 數:array_2d為二維數組;m為二維數組的行數;n為二維數組的列數.
返回值:二維數組中兩條對角線上元素之和
分? 值:
//題:求出二維數組中兩條對角線上元素之和。?應該是求行數和列數相同的數組
double?sum_2diagonal(double?*array_2d,unsigned?int?m,unsigned?int?n){double?sum=0;for(unsigned?int?i=0;i<m;i++){for(unsigned?int?j=0;j<n;j++){if(i==j)sum=sum+*(array_2d+i*n+j);if((n-1-i)==j)sum=sum+*(array_2d+i*n+j);}}return?sum;
}
第11題
double? sum_diagonal(double* array_2d,? unsigned int m, unsigned int n);
功? 能:求出二維數組中主對角線上元素之和
參? 數:array_2d為二維數組;m為二維數組的行數;n為二維數組的列數.
返回值:二維數組中主對角線上元素之和
分? 值:
double?sum_diagonal(double?*array_2d,unsigned?int?m,unsigned?int?n){double?sum=0;for(unsigned?int?i=0;i<m;i++){for(unsigned?int?j=0;j<n;j++){if(i==j)sum=sum+*(array_2d+i*n+j);}}return?sum;
}
第12題
double? sum_circum(double* array_2d,? unsigned int m, unsigned int n);
功? 能:求出二維數組中周邊元素之和
參? 數:array_2d為二維數組;m為二維數組的行數;n為二維數組的列數.
返回值:二維數組中周邊元素之和
分? 值:
double?sum_circum(double?*array_2d,unsigned?int?m,unsigned?int?n){double?sum=0;for(unsigned?int?i=0;i<m;i++){????sum=sum+*(array_2d+i*n+0);sum=sum+*(array_2d+i*n+(n-1));}for(unsigned?int?j=1;j<n-1;j++){sum=sum+*(array_2d+j);sum=sum+*(array_2d+(m-1)*n+j);}return?sum;
}
第13題
double? sum_max(double* array_2d,? unsigned int m, unsigned int n);
功? 能:求出二維數組中的最大值
參? 數:array_2d為二維數組;m為二維數組的行數;n為二維數組的列數.
返回值:二維數組中的最大值
分? 值:
double?sum_max(double?*array_2d,unsigned?int?m,unsigned?int?n){double?largest=*array_2d;for(unsigned?int?i=0;i<m;i++){for(unsigned?int?j=0;j<n;j++){if(largest<*(array_2d+i*n+j))largest=*(array_2d+i*n+j);}}return?largest;
}
第14題
double? sum_min(double* array_2d,? unsigned int m, unsigned int n);
功? 能:求出二維數組中的最小值
參? 數:array_2d為二維數組;m為二維數組的行數;n為二維數組的列數.
返回值:二維數組中的最小值
分? 值:
double?sum_min(double?*array_2d,unsigned?int?m,unsigned?int?n){double?min=*array_2d;for(unsigned?int?i=0;i<m;i++){for(unsigned?int?j=0;j<n;j++){if(min>*(array_2d+i*n+j))min=*(array_2d+i*n+j);}}return?min;
}//測試程序
#include<stdio.h>//將sum_2diagonal()、sum_diagonal()、sum_circum()、sum_max()、sum_min()代碼考到此處void?main()
{
//調用sum_2diagonal()、sum_diagonal()、sum_circum()、sum_max()、sum_min()函數double?arr_2d[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};double?sum_2d?=?sum_2diagonal((double?*)arr_2d,4,4);double?sum_d?=?sum_diagonal((double?*)arr_2d,4,4);double?sum_cir?=?sum_circum((double?*)arr_2d,4,4);double?max=sum_max((double?*)arr_2d,4,4);double?min=sum_min((double?*)arr_2d,4,4);printf("the?sum_2diagonal??is?%.2f\n",sum_2d);printf("the?sum_diagonal??is?%.2f\n",sum_d);printf("the?sum_circum??is?%.2f\n",sum_cir);printf("the?largest?number?is?%.2f\n",max);printf("the?least?number?is?%.2f\n",min);double?arr_2dd[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};sum_2d?=?sum_2diagonal((double?*)arr_2dd,3,4);sum_d?=?sum_diagonal((double?*)arr_2dd,3,4);sum_cir?=?sum_circum((double?*)arr_2dd,3,4);max=sum_max((double?*)arr_2dd,3,4);min=sum_min((double?*)arr_2dd,3,4);printf("the?sum_2diagonal??is?%.2f\n",sum_2d);printf("the?sum_diagonal??is?%.2f\n",sum_d);printf("the?sum_circum??is?%.2f\n",sum_cir);printf("the?largest?number?is?%.2f\n",max);printf("the?least?number?is?%.2f\n",min);
}
第15題
int sum_odd(unsigned int n);
功? 能:求出小于給定自然數的所有奇數之和
參? 數:n為給定的自然數.
返回值:>=0,小于給定自然數的所有奇數之和; <0,不成功.
分? 值:
int?sum_odd(unsigned?int?n){if(n<0)return?-1;int?sum=0;unsigned?int?i;for(?i=1;i<=n;i+=2){sum=sum+i;}return?sum;
}
第16題
int sum_even(unsigned int n);
功? 能:求出小于給定自然數的所有偶數之和
參? 數:n為給定的自然數.
返回值:>=0,小于給定自然數的所有偶數之和; <0,不成功.
分? 值:
int?sum_even(unsigned?int?n){if(n<0)return?-1;int?sum=0;unsigned?int?i;for(?i=0;i<=n;i+=2){sum=sum+i;}return?sum;
}//測試程序
#include<stdio.h>//將sum_odd()、sum_even()代碼考到此處void?main()
{//調用sum_odd()、sum_even()函數int?a=sum_odd(1);int?b=sum_odd(10);printf("the?sum_odd?is?%d\n",a);printf("the?sum_odd?is?%d\n",b);a=sum_even(1);b=sum_even(10);printf("the?sum_odd?is?%d\n",a);printf("the?sum_odd?is?%d\n",b);
}
????? 大家應該都知道,程序的算法不是唯一的,歡迎路過的朋友將你們認為更高效的算法指出,歡迎評論,讓我們一起進步吧......
轉載于:https://blog.51cto.com/junzhichen/1669931
總結
以上是生活随笔為你收集整理的C语言 基础60题(2)——二维数组操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。