Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C++
#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;

}
Posted
Comments
OriginalGriff 30-Jan-16 12:13pm    
And?
What is the problem?
What have you tried?
Where are you stuck?
What help do you need?
[no name] 30-Jan-16 12:22pm    
I think he showed what tried?
OriginalGriff 30-Jan-16 12:30pm    
The commented out line? I wondered, but since he doesn't declare a, b, or c I wanted him to explain better before we all go off on a tangent! :laugh:
Member 12297158 30-Jan-16 13:17pm    
I would like to call the function int Even( int a[][2], int length, int width) inside the main to print the output to the user
[no name] 30-Jan-16 12:23pm    
a.) Why "int a[][2]" for "int Even( int a[][2], int length, int width)"?
b.) Where is the problem? On a first glance everything (execpt [][2]) seems to be fine.

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:
C++
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
C#
sum = sum + a[i][j];
with
C#
sum++;


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:

C#
#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;
 
}
 
Share this answer
 
v3
Comments
Member 12297158 30-Jan-16 13:16pm    
@Jymm097 Thank you for taking the time to explain the way to solve the problem,

Can you tell me how to call the function int Even( int a[][2], int length, int width) inside the main to print the output to the user
LLLLGGGG 30-Jan-16 13:23pm    
The code is attached in the answer. I cannot figure out, though, why it didn't show up... But now everything's corrected.
Member 12297158 30-Jan-16 13:45pm    
i tried to run your code and gave me in error said int Even(int,int,int) connot convert argument 1 from int [3][2] to int
LLLLGGGG 31-Jan-16 6:27am    
The error you get is due to the fact that the prototype of the function is (int, int, int), whereas it should be (int[][], int, int). you can either correct the prototype or write your function above main: this way the prototype is not compulsory and your code will run just fine.
std::accumulate does what you want. Use it instead of rolling your own loops. You'll find it in the <algorithm> header.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900