如何找到奇数行和偶数行中的元素总和 [英] How do I find the sum of elements in odd and even rows

查看:90
本文介绍了如何找到奇数行和偶数行中的元素总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的任务:

给定一个大小为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;
}

推荐答案

这是一个非常简单的任务,所以我只给你一些提示:



使用更清晰的var和函数名称, x()

在函数调用等关键代码中使用调试输出(printf)如果声明

(当不再需要时删除oe注释)

写一些测试数据,使用简单的数组,这样你就可以调试你的代码



一个错误就是你的max_e错了。您为索引分配的不是值。

That is a relly simple task, so I give you only some tips:

use clearer var and function names, not x()
use debug output (printf) in critical code pathes like function call and if statement
(remove oe comment out when not more needed)
write some test data, with simple array so you can debug your code

one bug is that your max_e is wrong. You assign the index not the value.
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.


这篇关于如何找到奇数行和偶数行中的元素总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆