Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the error I am getting.

Error (active) E0167 argument of type "int" is incompatible with parameter of type "int (*)[3]"

What I have tried:

This is my code. The error line is highlighted in italics.
C++
#include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;

int main()
{
	int n;
	int m[3][3];
	int i, j;
	cout << " Enter 1 or 0 for respective places" << endl;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
		fg:
			cout << " Enter the Element for m[" << i << "][" << j << "]" << endl;
			cin >> m[i][j];
			if (m[i][j] != 1 && m[i][j] != 0)
			{
				cout << "This entry isnt allowed. ReEnter" << endl;
				goto fg;
			}
		}
	}
	cout << "the matrix is:" << endl;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			cout << "\t" << m[i][j];
		}
		cout << "\n";
	}

	X3(m[3][3]);    ###Error Line###
	return 0;
}

int X3(int m[3][3]) //generation of new matrices for 3X3 matrix
{
	int x[2][2];
	int i, j;
	int a, b;
	for (i = 0; i < 3 ; i++)
	{
		for (j = 0; j < 3; j++)
		{
			x[0][0] = m[i][i+j];
			x[0][1] = m[i][i + j + 1];
			x[1][0] = m[i + 1][i + j];
			x[1][1] = m[i + 1][i + j + 1];

			cout << " The newly generated matrices are:" << endl;

			for (a = 0; a < 2; a++)
			{
				for (b = 0; b < 2; b++)
				{
					cout << "\t" << x[a][b];
				}
				cout << "\n";
			}
			
		}
	}
}
Posted
Updated 21-Mar-22 23:55pm
v2

Try
C++
#include <iostream>
#include <cmath>

using namespace std;

int X3(int m[][3]); //generation of new matrices for 3X3 matrix

int main()
{
  int m[3][3];
  int i, j;
  cout << " Enter 1 or 0 for respective places" << endl;
  for (i = 0; i < 3; i++)
  {
    for (j = 0; j < 3; j++)
    {
      bool bad_entry;
      do
      {
        cout << " Enter the Element for m[" << i << "][" << j << "]" << endl;
        cin >> m[i][j];
        bad_entry = (m[i][j] != 1) && (m[i][j] != 0);
        if ( bad_entry )
        {
          cout << "This entry isnt allowed. ReEnter" << endl;
        }
      } while ( bad_entry );
    }
  }
  cout << "the matrix is:" << endl;
  for (i = 0; i < 3; i++)
  {
    for (j = 0; j < 3; j++)
    {
      cout << "\t" << m[i][j];
    }
    cout << "\n";
  }

  X3(m);
  return 0;
}

int X3(int m[][3]) //generation of new matrices for 3X3 matrix
{
  int x[2][2];
  int i, j;
  int a, b;
  for (i = 0; i < 3 ; i++)
  {
    for (j = 0; j < 3; j++)
    {
      if ( ((i + j + 1)  < 3) && (i+1) < 3)
      {
        x[0][0] = m[i][i+j];
        x[0][1] = m[i][i + j + 1];
        x[1][0] = m[i + 1][i + j];
        x[1][1] = m[i + 1][i + j + 1];

        cout << " The newly generated matrices are:" << endl;

        for (a = 0; a < 2; a++)
        {
          for (b = 0; b < 2; b++)
          {
            cout << "\t" << x[a][b];
          }
          cout << "\n";
        }
      }
    }
  }
  return 0;
}
 
Share this answer
 
Comments
Atharav Jadhav 22-Mar-22 6:07am    
Thank you so much sir for your thoughtful insight and your invested time. I really respect your advice and will certainly act upon them
CPallini 22-Mar-22 6:12am    
You are welcome.
Patrice T 22-Mar-22 9:20am    
+5
CPallini 22-Mar-22 17:24pm    
Thank you!
You are trying to pass a single integer (1. m[3][3]) to a function that is expecting an array (2. m[3][3]).

1. At this point the call to X3 says pass the single integer at row 3, column 3 of m to the function X3.
2. In your definition of the function X3 you declare parameter 1 to be an array of int types with 3 rows and 3 columns.

Your call to X3 should just pass the address of the array:
C++
X3(m);
 
Share this answer
 
Comments
Richard Deeming 22-Mar-22 6:06am    
Looks like you've managed to post the same solution twice. :)
Richard MacCutchan 22-Mar-22 6:34am    
No idea how that happened, although it did take a long time to come back.
When you declare an array as a function parameter:
C++
int X3(int m[3][3]
{
   ...
}
You are telling the compiler that X3 accepts a single parameter, which is a 3 x 4=3 array of integers.

When you call a function with a parameter like this:
C++
X3(m[3][3]);
You are dereferencing the array to a single element of that array - which actually lies outside the bound of the array itself:
C++
int m[3][3];
because in C++, array indexes start at zero.
The dereference is the same as assigning a variable:
C++
int x = m[0][[0];
Puts the value in the first element of the first row of the array m into x

There are numerous problems with that code that you haven' noticed yet, including (but not limited to):
* X3 does not retrun a value.
* if you returned the "new array" x from X3 you would have a dangling reference, and your code would start to behave very badly - you can't return stack based items and expect it to work properly!
* why are you looping and using constant values in X3?

That code doesn't look "Planned" - it looks like you quickly scanned teh take and leapt into code without thinking about what you are expected to do - and it shows as poor code that really isn't worth saving. Back that up somewhere, and start again from scratch - read the assignment carefully, and think about how to solve it for five or ten minutes before you even start l=thinking about code. It'll save you loads of time in the long run!
 
Share this answer
 
Comments
Atharav Jadhav 22-Mar-22 6:06am    
Thank you so much sir for your thoughtful insight and your invested time. I really respect your advice and will certainly act upon them.

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