本文介绍了如何找到奇数行和偶数行中的元素总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的任务:
给定一个大小为N的整数矩阵。找到奇数行的最小元素集和偶数行的大元素。
我真的不知道我做错了什么。
我尝试了什么:
This is my task:
Given an integer matrix of size N. Find the set of the smallest elements of its odd rows and large elements of its even rows.
I don't really know what am i doing wrong.
What I have tried:
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int size;
int min(int *array[][50])
{
int i,j,min_e=0;
for (i = 1; i < size; i++)
{
for (j = 1; j < size; j++)
{
if (array[i][j] < array[min_e])//operand types are incompatible ("int *" and "int **")
{
min_e = i;
}
return min_e;
}
}
}
int x(int array[][50])
{
int i, j,sum_e=0;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (i % 2 == 0)
{
sum_e += min(array);//argument of type "int (*)[50]" is incompatible with parameter of type "int *(*)[50]
}
return sum_e;
}
}
return 0;
}
int max(int array[][50])
{
int i, j, max_e = 0;
for (i = 1; i < size; i++)
{
for (j = 1; j < size; j++)
{
if (array[i][j] > array[max_e])//operand types are incompatible ("int *" and "int **")
max_e = i;
}
return max_e;
}
}
int y( int array[50][50])
{
int i, j, sum_o = 0, o = 0;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (array[i][j] % 2 == 1)
{
sum_o += max(array);//argument of type "int (*)[50]" is incompatible with parameter of type "int *(*)[50]
}
}
return sum_o;
}
return 0;
}
int main()
{
int array[50][50], i, j;
printf("\n Size of array: ");
scanf_s("\n %d", &size);
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("\n array[%d][%d]: ",i,j);
scanf_s("\n %d", array[i][j]);
}
}
printf("\n Sum of even-numbered elements \n: ", x(array));
printf("\n Sum of odd-numbered elements \n: ", y(array));
_getch();
return 0;
}
推荐答案
if (array[i][j] > max_e ) {
printf("new max found: %d", array[i][j] );
max_e = array[i][j];//adding one
}
应该是正确的。
你也应该保护这个大小不能大于50。
should be correct.
You should also guard that size cant be greater than 50.
这篇关于如何找到奇数行和偶数行中的元素总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!