Is there anyway I can declare a 2d array in a structure and pass it into function to input numbers in it C++?

Viewed 54

I have been having a lot of trouble for this code I have to write for class. I basically have to use two 2d arrays and find their product, sum, ect. I am aware you cannot pass a 2d array into a function, but all the other ways I have tried have not worked. The problem is that I have to ask the user to input the variables in the 2d arrays but nothing I have done has worked. I posted original code I made but their may be small mistakes cause I changed it a lot.

struct Array
{
  string name1, name2;
  int scalar, sum, difference, scalar_matrix, product, matrix_inverse;
  int matrix1[2][2];
  int matrix2[2][2];
  
  

};




//Declare all the functions that will be used in the code
void get_matrixname(string name1,string name2, int matrix1, int matrix2); 
void get_scalar(int scalar);
int calc_sum(int matrix1, int matrix2, int sum);
int calc_diff(int matrix1, int matrix2, int difference);
int scalar_mult(int scalar, int matrix1, int matrix2, int scalar_matrix);
int calc_prod(int matrix1, int matrix2,int product);
int calc_inv(int matrix1, int matrix2, int matrix_inverse);

Array matrix;
fstream myfile;

int main() 
{
  
  const int max_row = 2;
  const int max_col = 2;
  
  int input;

  cout << "               Array program menu.\n"
         << "Choose one of the following numbers to perform a task:\n";
    cout << " 1. Input array names and numbers.\n";
    cout << " 2. Input scalar int.\n";
    cout << " 3. Calcute sum of both Matrices.\n";
  cout << " 4. Calcute difference of both Matrices.\n";
  cout << " 5. Calcute scalar multiplication.\n";
  cout << " 6. Calcute matrix multiplication.\n";
  cout << " 7. Calcute inverse of matrix.\n";
  cout << "Input: ";
    cin  >> input;

  

  switch (input)
    { 
    {
          case 1: 
            get_matrixname(matrix.name1,matrix.name2,matrix.matrix1,matrix.matrix2);
           
            
      case 2: 
          get_scalar(matrix.scalar);   
      case 3:
          int calc_sum(int matrix1, int matrix2, int sum);
      case 4:
         int calc_diff(int matrix1, int matrix2, int difference);
      case 5:
          int scalar_mult(int scalar, int matrix1, int matrix2, int scalar_matrix);;
      case 6:
          int calc_prod(int matrix1, int matrix2,int product);
      case 7:
          int calc_inv(int matrix1, int matrix2, int matrix_inverse);
    }
  }
}

void get_matrix_name(string name1,string name2)
{
  cout << "Enter the name for Matrix 1" << endl;
  cin >> matrix.name1;
 
  
  
  struct Array arr_matrix1[2][2];
  cout << "Enter 4 variables that for Matrix 1" << endl;
  for(int i=0;i<2;i++)
  {
    for(int j=0;j<2;j++)
   {
       cout << "Enter a number" << endl;
       cin>>matrix.matrix1[i][j];
   }
  }
  
  cout << "Enter the name for Matrix 2" << endl;
  cin >> matrix.name2;

  cout << "Enter 4 variables that for Matrix 2" << endl;
  for(int x=0;x<2;x++)
  {
    for(int y=0;y<2;y++)
   {
       cout << "Enter a number" << endl;
       cin>>matrix.matrix2[x][y];
   }
  }
  
}

void get_scalar(int scalar)
{
  cout << "Enter scalar int ";
  cin >> matrix.scalar;
}
0 Answers
Related