Home > Blockchain >  C : How to make program differentiate between multiplication and pointers, not accepting operations
C : How to make program differentiate between multiplication and pointers, not accepting operations

Time:09-17

So I'm writing a program for an assignment that multiplies two matrices using dynamic arrays only. I'm running into two problems. I can't figure out how to add specific values from two different arrays and storing that in a third array:

bag = F[add]   F[add 1]; //line 73

and I also can't figure out how to multiply specific values from two different arrays and storing that in a third array:

F[y] = (C[cnt1][cnt2]) * (D[cnt2][cnt1]); //line 68

It's reading the multiplication operation as if i'm trying to create another pointer, because I keep getting these errors:

 sh -c make -s
./main.cpp:68:36: error: incompatible integer to pointer conversion assigning to 'int *' from 'int'
            F[y] = (C[cnt1][cnt2]) * (D[cnt2][cnt1]);
                   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
./main.cpp:70:19: error: invalid operands to binary expression ('int *' and 'int')
          if (F[n]%2 != 0)
              ~~~~^~
./main.cpp:73:24: error: invalid operands to binary expression ('int *' and 'int *')
          bag = F[add]   F[add 1];
                ~~~~~~ ^ ~~~~~~~~
3 errors generated.
make: *** [Makefile:9: main] Error 1
exit status 2

This is my full code:

#include <iomanip> 
#include <iostream>

using namespace std;

int main()
{
  cout << endl;
  int **C, n, m; //pointer, rows, columns for matrix 1;
  int **D, p, q; //pointer, rows, columns for matrix 2;
  int **E, r, s; //pointer, rows, columns for result matrix;
  int **F, v, w; //pointer, rows, columns for array that stores values while computing the multiplication of the matrices;
  
  cout << "Enter the dimensions of your matrices: ";
  cin >> n >> m;
  p = n; q = m;
  r = n; s = m;
  v = n;
  cout << endl;
  C = new int *[n];
  D = new int *[p];
  E = new int *[r];
  for (int x=0 ; x < n; x  )
    {
      C[x] = new int [m];
    }

  for (int x=0 ; x < p; x  )
    {
      D[x] = new int [q];
    }

  for (int x=0 ; x < r; x  )
    {
      E[x] = new int [r];
    }
  for (int x=0 ; x < v; x  )
    {
      F[x] = new int [v];
    }

  cout << "Enter the values of your first matrix: ";
  for (int I=0 ; I < n; I  )
    {
    for (int K=0 ; K < m; K  ) 
      cin >> C[I][K];
    }

  cout << "Enter the values of your second matrix: ";
  for (int L=0 ; L < p; L  )
    {
    for (int Z=0 ; Z < q; Z  ) 
      cin >> D[L][Z];
    }

  cout <<  endl;
  
  for (int cnt1 = 0; cnt1 < n; cnt1  )
    {
      for (int cnt2 = 0; cnt2 < m; cnt2  )
      {
        int bag;
        int add = 0;
        while (add < v)
        {
          for (int y = 0; y < n; y  )
          {
            F[y] = (C[cnt1][cnt2]) * (D[cnt2][cnt1]);
          }
          if (F[n]%2 != 0)
            F[n] = F[n 1];
          bag = 0;
          bag = F[add]   F[add 1];
          add  = 2;
        }
        E[cnt1][cnt2] = bag;
      }
    }

  cout << "The product of:" << endl << endl;

for (int I=0 ; I < n; I   )
    {
      for (int K=0 ; K < m; K  )
        cout << setw(4)<< C[I][K];
      cout << endl;
    }

  cout <<  endl << "and:" << endl << endl;


  for (int L=0 ; L < p; L   )
    {
      for (int Z=0 ; Z < q; Z  )
        cout << setw(4)<< D[L][Z];
      cout << endl;
    }
  
  cout << endl << "is: " << endl << endl;

  for (int T=0 ; T < r; T  )
    {
    for (int U=0 ; U < s; U  ) 
      cout << setw(4) << E[T][U];
    }

  return 0;
  }

How do I fix this?

Note: I am aware that this style is outdated and there are memory leaks, and I am aware that using vectors would be better. Unfortunately I have to do it the way my professor wants me to :\

Any help/suggestions is appreciated.

CodePudding user response:

F[y] = (C[cnt1][cnt2]) * (D[cnt2][cnt1]); //line 68

C is int **C

D is int **D

F is int **F

So, the expression on the right side is an int.

F[y], though, is an int pointer. So, you either meant:

*(F[y]) = ... or F[y][something] = ...

There's no magic, you just need to carefully look at every type and every operation. What would also help is having meaningful variable names. This ways, it is much easier to understand what the code is attempting to do.

  • Related