如何在数组中调用函数? [英] how to call a function in array?

查看:87
本文介绍了如何在数组中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用一个函数来获取数组中的偶数总数

这是代码



谢谢你你的帮助

I would like to call a function to get the total even number in the array
Here is the code

Thank you for your help

#include <iostream>
#define MAX_ROWS 3
#define MAX_COLUMNS 2


using namespace std;

int  Even(int A, int length, int width);

int main(){

	
	int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
	
	//Even(a,b,c);

	system("pause");
	return 0;

}


int Even( int a[][2], int length, int width){

	int sum = 0;
	int i; 
	int j; 

	for (i = 0; i < length, i++;){

		for (j = 0; j < width, j++;){


			if (a[i][j]  % 2 == 0){

				sum = sum + a[i][j];
			}
			cout << "The even numbers are " << sum << endl;
		}

	}

	return sum;

}

推荐答案





好吧......让我们从分析你的代码开始,这不错。



现在,我不明白 int a [] [2] :它应该是 int a [] [MAX_COLUMNS] ,否则循环将无法访问矩阵的所有内容,或者更糟糕的是,它还可以访问不属于矩阵的元素。这用 Pointer Arithmetics 来解释:矩阵只是指针(int ** a)指向元素[0] [0]的指针的别名。每当您尝试访问同一列中的下一个元素([1] [0])时,您只看到原始指针指向的元素增加1 * sizeof(int)= 2. [参考:将2D数组传递给C ++函数 - Stack Overflow [ ^ ] ]。可以为列绘制相同的结论,但是当您移动到下一列([0] [1])时,编译器会在内存中向前移动n_rows * 1 * sizeof(int)。你可以访问以这种方式声明的矩阵的元素[100] [45]:int mx [10] [10],但是元素不属于矩阵,但它在一个完全不同的内存部分。尽管读取它并不危险,但修改它可能是因为你正在修改一部分内存,这很可能不属于你的程序。程序或操作系统可能会崩溃,您将不得不重新启动系统。



然后,我没有看到这行代码的用处:

Hi,

Ok... Let's start by analysing your code, which is not bad.

Now, I do not understand the int a[][2]: it should be int a[][MAX_COLUMNS], otherwise the loop won't be able to access all the content of the matrix or, worse, it can also access elements that are not part of the matrix. This is explained with Pointer Arithmetics: a matrix is just an alias for a pointer of a pointer (int** a) which points to the element [0][0]. Whenever you try to access the next element in the same column ([1][0]), you just see the element pointed by the original pointer incremented by 1 * sizeof(int) = 2. [Reference: Passing a 2D array to a C++ function - Stack Overflow[^]]. The same conclusions can be drawn for the columns, but when you move to the next column ([0][1]), the compiler moves forward in memory by n_rows * 1 * sizeof(int). You can access the element [100][45] of a matrix that has been declared this way: int mx[10][10], but the element does not belong to the matrix, but it is in a completely different part of memory. Although it is not dangerous to read it, modifying it could be so because you are modifying a part of memory which, most likely, does not belong to your program. The program or the OS may crash and you'll be obliged to reboot your system.

Then, I do not see the usefulness of this line of code:
cout << "The even numbers are " << sum << endl;



如果你返回价值总和,向用户显示它的重点每个循环



之后还有另一件我不理解的事情:你的任务是计算偶数还是将矩阵中包含的所有偶数加在一起。因为您编写的程序会对矩阵中包含的所有偶数进行求和并返回此总和。如果您只想计算偶数,则应将


If you return the value sum, what's the point in showing it to the user each loop.

After that there is another thing I do not understand: does your task consist in counting even numbers or summing all the even numbers contained in the matrix together. Because the program you've written sums all the even numbers contained in the matrix and returns this sum. If you just want to count even numbers you should replace

sum = sum + a[i][j];

替换为

sum++;





最后,打印出甚至的结果是主程序的职责功能,所以你应该在主要的cout它。



总结一下,这是我为完成任务而编写的代码:





Finally, it's the main program's duty to print out the result of the Even function, so you should "cout" it in the main.

To sum up, here's the code I would write to accomplish the task:

#include <iostream>
#define MAX_ROWS 3
#define MAX_COLUMNS 2

 
using namespace std;
 
int Even( int a[][MAX_COLUMNS], int length, int width){
 
	int sum = 0;
	int i; 
	int j; 
 
	for (i = 0; i < length, i++;){
 
		for (j = 0; j < width, j++;){
 
			if (a[i][j]  % 2 == 0){
 
				sum++;
			}
		}
 
	}
 
	return sum;
 
}

int main(){
 
	
	int A[MAX_ROWS][MAX_COLUMNS] = { { 3, 2 }, { 4, 5 }, { 2, 2 } };
	
	cout << "There are " << Even(A,MAX_ROWS,MAX_COLUMNS) << " even numbers in the matrix." << endl;
        cout << "Goodbye :-)" << endl;

	system("PAUSE");
	return 0;
 
}


std :: accumulate 做你想做的事。使用它而不是滚动自己的循环。您可以在< algorithm> 标题中找到它。
std::accumulate does what you want. Use it instead of rolling your own loops. You'll find it in the <algorithm> header.


这篇关于如何在数组中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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