2

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;
}
Amity
  • 21
  • 2
  • 1
    `int matrix1[2][2];` => `std::array, 2> matrix;` – Ted Lyngmo Sep 23 '21 at 20:24
  • Passing around 2D arrays is a nuisance, but you can pass a 2D array that's inside a structure, so passing around references to `Array`s is probably the way to go here. – user4581301 Sep 23 '21 at 20:25
  • Come to think of it, since this is tagged C++, why not make `calc_sum` etc... `Array` class member functions? Then you won't have to pass anything around. The magic of `this` does all the work for you. – user4581301 Sep 23 '21 at 20:26
  • @user4581301 Unfortunately I don't think my professor wants us to use classes :( – Amity Sep 23 '21 at 21:02
  • @TedLyngmo Sorry for asking this but how would I exactly do that? – Amity Sep 23 '21 at 21:11
  • 1
    So be it. Keep the functions as [Free Functions](https://stackoverflow.com/questions/4861914/what-is-the-meaning-of-the-term-free-function-in-c) and pass the `Array` to them as a reference. Eg: `int calc_sum(Array & arr);` Note this also probably isn't what the instructor has in mind, packing everything into one [god object](https://en.wikipedia.org/wiki/God_object) that knows everything like `Array` is usually a bad idea, so Use what Ted suggested up in the first comment if you can. If you can't use `std::array` yet, make a poor-man's version: `struct matrix { int data[2][2]; };` – user4581301 Sep 23 '21 at 21:11
  • 1
    `int calc_sum(int matrix1, int matrix2, int sum);` would look more like `matrix calc_sum(const matrix & a, const matrix & b);`. `a` and `b` are declared `const` as a promise that you won't change them in the function. This allows you to pass in temporary objects and because the compiler knows they won't be changed, it can often make some really zippy optimizations. – user4581301 Sep 23 '21 at 21:14

0 Answers0