user-0 提问: 编写函数求二维数组中的所有奇数之和与所有偶数之和

回复:
user: user-1
content: 用余数算法%来每个判断下，奇数相加，偶数相加就可以了嘛

user: user-2
content: 学了别的发现指针都不会用了。。。。

user: user-3
content: 给我写，我会在if里面写“(a[i][j]&1)”。写驱动写习惯了

user: user-4
content: 给我写，我会在if里面写“(a[i][j]&1)”。写驱动写习惯了。

user: user-0
content: 引用 12 楼 自信男孩的回复:[quote=引用 10 楼 风吟xqy 的回复:] [quote=引用 8 楼 自信男孩的回复:]#include <stdio.h> #include <stdlib.h>  #define N 5 #define M 6 void fun(int (*a)[6],int n,int m,int *odd,int *even);//函数声明。其中odd返回奇数的和，even返回偶数。  int main() {     int a[N][M]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};     int (*p)[M] = a;     //int qishu,oushu;     int qishu = 0, oushu = 0;   //odd even     //fun(p,5,6,&qishu,&oushu);     fun(p, N, M, &qishu, &oushu);     printf("qishu_sum=:%d\noushu_sum=:%d",qishu,oushu); /*     int odd, even;      odd = even = 0;      fun(p, N, M, &odd, &even);     printf("odd = %d\t, even = %d\n", odd, even);  */     return 0; } void fun(int (*a)[6],int n,int m,int *odd, int *even) {     *odd=0;     *even=0;     for (int i = 0; i < n; i++)     {         for (int j = 0; j < m; j++)         {             //if (*(*(a + i) + j)%2==0)             if (a[i][j]%2==0)             {                 *odd = *odd + a[i][j];             }             else if (a[i][j] % 2 == 1)             {                 *even = *even + a[i][j];             }         }     }     //return *odd,*even;  } 供参考～  odd， even作为指针，那么就会把结果带出函数，不需要返回。 根据题意，函数参数里面要是a[][]喔[/quote] 别喔了，a[][]和int (*a)[M]是等价的。 基本的C语言建议多学多练～[/quote] 这里还是知道的

user: user-0
content: 引用 21 楼 w4505021990的回复:louzhu牛逼哟 xiongdi牛逼噢

user: user-0
content: 引用 20 楼 weixin_42878936的回复:厉害欧，有点东西 哪有，我是萌新

user: user-5
content: louzhu牛逼哟

user: user-6
content: 厉害欧，有点东西

user: user-7
content: 引用 10 楼 风吟xqy 的回复:[quote=引用 8 楼 自信男孩的回复:]#include <stdio.h> #include <stdlib.h>  #define N 5 #define M 6 void fun(int (*a)[6],int n,int m,int *odd,int *even);//函数声明。其中odd返回奇数的和，even返回偶数。  int main() {     int a[N][M]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};     int (*p)[M] = a;     //int qishu,oushu;     int qishu = 0, oushu = 0;   //odd even     //fun(p,5,6,&qishu,&oushu);     fun(p, N, M, &qishu, &oushu);     printf("qishu_sum=:%d\noushu_sum=:%d",qishu,oushu); /*     int odd, even;      odd = even = 0;      fun(p, N, M, &odd, &even);     printf("odd = %d\t, even = %d\n", odd, even);  */     return 0; } void fun(int (*a)[6],int n,int m,int *odd, int *even) {     *odd=0;     *even=0;     for (int i = 0; i < n; i++)     {         for (int j = 0; j < m; j++)         {             //if (*(*(a + i) + j)%2==0)             if (a[i][j]%2==0)             {                 *odd = *odd + a[i][j];             }             else if (a[i][j] % 2 == 1)             {                 *even = *even + a[i][j];             }         }     }     //return *odd,*even;  } 供参考～  odd， even作为指针，那么就会把结果带出函数，不需要返回。 根据题意，函数参数里面要是a[][]喔[/quote] 别喔了，a[][]和int (*a)[M]是等价的。 基本的C语言建议多学多练～

user: user-8

user: user-9
content: 17楼　一个字输入错误：循环次搂　更正为：循环次数  p 的初值（数组元素首址），可以把int a[][]用强制类型转换赋值给 p吧？

user: user-9
content: 难道不可以在void fun() 定义一个变量 int *p ， 　把“首个元素”的地址赋值给它； 然后for循环用 p遍历取出所有元素的值来处理？ （循环次搂 以m和n的值可计算出来）

user: user-10
content: 来混个十几分，感谢楼主

user: user-0
content: 引用 14 楼 自信男孩的回复:[quote=引用 13 楼 风吟xqy 的回复:] [quote=引用 12 楼 自信男孩的回复:][quote=引用 10 楼 风吟xqy 的回复:] [quote=引用 8 楼 自信男孩的回复:]#include <stdio.h> #include <stdlib.h>  #define N 5 #define M 6 void fun(int (*a)[6],int n,int m,int *odd,int *even);//函数声明。其中odd返回奇数的和，even返回偶数。  int main() {     int a[N][M]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};     int (*p)[M] = a;     //int qishu,oushu;     int qishu = 0, oushu = 0;   //odd even     //fun(p,5,6,&qishu,&oushu);     fun(p, N, M, &qishu, &oushu);     printf("qishu_sum=:%d\noushu_sum=:%d",qishu,oushu); /*     int odd, even;      odd = even = 0;      fun(p, N, M, &odd, &even);     printf("odd = %d\t, even = %d\n", odd, even);  */     return 0; } void fun(int (*a)[6],int n,int m,int *odd, int *even) {     *odd=0;     *even=0;     for (int i = 0; i < n; i++)     {         for (int j = 0; j < m; j++)         {             //if (*(*(a + i) + j)%2==0)             if (a[i][j]%2==0)             {                 *odd = *odd + a[i][j];             }             else if (a[i][j] % 2 == 1)             {                 *even = *even + a[i][j];             }         }     }     //return *odd,*even;  } 供参考～  odd， even作为指针，那么就会把结果带出函数，不需要返回。 根据题意，函数参数里面要是a[][]喔[/quote] 别喔了，a[][]和int (*a)[M]是等价的。 基本的C语言建议多学多练～[/quote] 这里还是知道的[/quote] 哦[/quote] 哈哈，谢谢大佬

user: user-7
content: 引用 13 楼 风吟xqy 的回复:[quote=引用 12 楼 自信男孩的回复:][quote=引用 10 楼 风吟xqy 的回复:] [quote=引用 8 楼 自信男孩的回复:]#include <stdio.h> #include <stdlib.h>  #define N 5 #define M 6 void fun(int (*a)[6],int n,int m,int *odd,int *even);//函数声明。其中odd返回奇数的和，even返回偶数。  int main() {     int a[N][M]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};     int (*p)[M] = a;     //int qishu,oushu;     int qishu = 0, oushu = 0;   //odd even     //fun(p,5,6,&qishu,&oushu);     fun(p, N, M, &qishu, &oushu);     printf("qishu_sum=:%d\noushu_sum=:%d",qishu,oushu); /*     int odd, even;      odd = even = 0;      fun(p, N, M, &odd, &even);     printf("odd = %d\t, even = %d\n", odd, even);  */     return 0; } void fun(int (*a)[6],int n,int m,int *odd, int *even) {     *odd=0;     *even=0;     for (int i = 0; i < n; i++)     {         for (int j = 0; j < m; j++)         {             //if (*(*(a + i) + j)%2==0)             if (a[i][j]%2==0)             {                 *odd = *odd + a[i][j];             }             else if (a[i][j] % 2 == 1)             {                 *even = *even + a[i][j];             }         }     }     //return *odd,*even;  } 供参考～  odd， even作为指针，那么就会把结果带出函数，不需要返回。 根据题意，函数参数里面要是a[][]喔[/quote] 别喔了，a[][]和int (*a)[M]是等价的。 基本的C语言建议多学多练～[/quote] 这里还是知道的[/quote] 哦

user: user-11
content: int main() { 	int a[N][M] = { { 1,2,3,4,5,6 },{ 7,8,9,10,11,12 },{ 13,14,15,16,17,18 },{ 19,20,21,22,23,24 },{ 25,26,27,28,29,30 } }; 	int qishu, oushu; 	fun(a, N,M,&qishu,&oushu); 	return 0; } void fun(int a[][M], int n, int m, int *odd, int *even) { 	 	int result[2] = { 0 }; 	for (int i = 0; i < n; i++) 	{ 		for (int j = 0; j < m; j++) 		{ 			result[a[i][j] & 0x1] += a[i][j];  		}  	} 	*odd = result[1]; 	*even = result[0];  }

user: user-0
content: 引用 8 楼 自信男孩的回复:#include <stdio.h> #include <stdlib.h>  #define N 5 #define M 6 void fun(int (*a)[6],int n,int m,int *odd,int *even);//函数声明。其中odd返回奇数的和，even返回偶数。  int main() {     int a[N][M]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};     int (*p)[M] = a;     //int qishu,oushu;     int qishu = 0, oushu = 0;   //odd even     //fun(p,5,6,&qishu,&oushu);     fun(p, N, M, &qishu, &oushu);     printf("qishu_sum=:%d\noushu_sum=:%d",qishu,oushu); /*     int odd, even;      odd = even = 0;      fun(p, N, M, &odd, &even);     printf("odd = %d\t, even = %d\n", odd, even);  */     return 0; } void fun(int (*a)[6],int n,int m,int *odd, int *even) {     *odd=0;     *even=0;     for (int i = 0; i < n; i++)     {         for (int j = 0; j < m; j++)         {             //if (*(*(a + i) + j)%2==0)             if (a[i][j]%2==0)             {                 *odd = *odd + a[i][j];             }             else if (a[i][j] % 2 == 1)             {                 *even = *even + a[i][j];             }         }     }     //return *odd,*even;  } 供参考～  odd， even作为指针，那么就会把结果带出函数，不需要返回。 根据题意，函数参数里面要是a[][]喔

user: user-0
content: 根据题意，函数参数里面要是a[][]喔

user: user-7
content: #include <stdio.h> #include <stdlib.h>  #define N 5 #define M 6 void fun(int (*a)[6],int n,int m,int *odd,int *even);//函数声明。其中odd返回奇数的和，even返回偶数。  int main() {     int a[N][M]={{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30}};     int (*p)[M] = a;     //int qishu,oushu;     int qishu = 0, oushu = 0;   //odd even     //fun(p,5,6,&qishu,&oushu);     fun(p, N, M, &qishu, &oushu);     printf("qishu_sum=:%d\noushu_sum=:%d",qishu,oushu); /*     int odd, even;      odd = even = 0;      fun(p, N, M, &odd, &even);     printf("odd = %d\t, even = %d\n", odd, even);  */     return 0; } void fun(int (*a)[6],int n,int m,int *odd, int *even) {     *odd=0;     *even=0;     for (int i = 0; i < n; i++)     {         for (int j = 0; j < m; j++)         {             //if (*(*(a + i) + j)%2==0)             if (a[i][j]%2==0)             {                 *odd = *odd + a[i][j];             }             else if (a[i][j] % 2 == 1)             {                 *even = *even + a[i][j];             }         }     }     //return *odd,*even;  } 供参考～  odd， even作为指针，那么就会把结果带出函数，不需要返回。