### Help with pointers and arrays, please!

Pages: 12
Hi all.

I'm learning c++ by myself, and right now I'm working in a basic problem of arrays. I want to create a code that multiplies matrices. I wrote the code already and it works, but I wanna do it using functions and pointers, but then, everything is messed up. This is the code that I have so far, here is only the part where I ask the user for the dimensions and entries of the matrices, no multiplication is done yet. I have a couple of errors and I've been reading the references that I am following and asked a couple of fellas but no help. If anyone can give me some help, I appreciate it. Thanks!

***********************************************************
#include<iostream>
#include<cmath>
void intro()
{
using namespace std;
cout << "*****************************************************" << endl;
cout << "Program to calculate the product of two matrices C=AB" << endl;
cout << "*****************************************************" << endl;
}
void get_A(double**& A, int& rowsA, int& colsA)
{
using namespace std;
//double** A;
cout << "********************************" << endl;
cout << "Enter the dimensions of matrix A" << endl;
cout << "********************************" << endl;
cout << "Number of rows: ";
cin >> rowsA;
cout << "Number of columns: ";
cin >> colsA;
cout << "********************************" << endl;
A = new double [rowsA][colsA];

for(int i=0; i<rowsA; i++)
{
for(int j=0; j<colsA; j++)
{
cout << "A[" << i << "," << j << "] = ";
cin >> A[i][j];
}
}
}
void get_B(double**& B, int& rowsB, int& colsB)
{
using namespace std;
// double** B;
cout << "********************************" << endl;
cout << "Enter the dimensions of matrix B" << endl;
cout << "********************************" << endl;
cout << "Number of rows: ";
cin >> rowsB;
cout << "Number of columns: ";
cin >> colsB;
cout << "********************************" << endl;
B = new double [rowsB][colsB];

for(int i=0; i<rowsB; i++)
{
for(int j=0; j<colsB; j++)
{
cout << "B[" << i << "," << j << "] = ";
cin >> B[i][j];
}
}
}
//void FreeMatrix_A(double** A, int rowsA, int colsA)
//{
//for(int i=0; i<rowsA; i++)
// {
// for(int j=0; j<colsA; j++)
// {
// delete[] A[i][j];
// }
// }
//delete[] A;
//}
//void FreeMatrix_B(double** B, int rowsB, int colsB)
//{
//for(int i=0; i<rowsB; i++)
// {
// for(int j=0; j<colsB; j++)
// {
// delete[] B[i][j];
// }
// }
//delete[] B;
//}
int main(int argc, char* argv[])
{
//using namespace std;

double** A;
double** B;
int rowsA, colsA, rowsB, colsB;

intro();
get_A(A, rowsA, colsA);
get_B(B, rowsB, colsB);
delete [] A;
delete [] B;
return 0;
}
Can you copy and paste the error messages you are getting?

Also, for code, you can past code [code]between code tags[/code] to get formatting like `this`. It makes it easier to read than just the plain text.
Last edited on
These are the erros that I am getting :

\$ matrix_product_2.cpp: In function ‘void get_A(double**&, int&, int&)’:
\$ matrix_product_2.cpp:22:26: error: ‘colsA’ cannot appear in a constant-expression
\$ matrix_product_2.cpp: In function ‘void get_B(double**&, int&, int&)’:
\$ matrix_product_2.cpp:45:26: error: ‘colsB’ cannot appear in a constant-expression

I'm sorry, I feel fool today, but I can not put my code into tags as you told me :( [/try]

`A = new double [rowsA][colsA];`

`B = new double [rowsB][colsB];`

C++ doesn't work this way (though Java does). You have to make it an array of pointers and then go through each and initialize it to an array (think of it an an 'array of arrays').
Last edited on
Thank you, I will try to work on it.
Hey, I was working on it, I did something that fixed errors but now when I run the code I can enter the dimensions of the matrix A and then my compiler shows me this:
 `Segmentation fault (core dumped) `
. Here goes the code (Thanks for the help!):

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include void intro() { using namespace std; cout << "*****************************************************" << endl; cout << "Program to calculate the product of two matrices C=AB" << endl; cout << "*****************************************************" << endl; } void get_A(double**& A, int& rowsA, int& colsA) { using namespace std; //double** A; cout << "********************************" << endl; cout << "Enter the dimensions of matrix A" << endl; cout << "********************************" << endl; cout << "Number of rows: "; cin >> rowsA; cout << "Number of columns: ";[ cin >> colsA; cout << "********************************" << endl; for(int i=0; i> rowsB; cout << "Number of columns: "; cin >> colsB; cout << "********************************" << endl; for(int i=0; i

 ``1011121314151617181920212223242526`` ``````void get_A(double**& A, int& rowsA, int& colsA) { using namespace std; //double** A; cout << "********************************" << endl; cout << "Enter the dimensions of matrix A" << endl; cout << "********************************" << endl; cout << "Number of rows: "; cin >> rowsA; cout << "Number of columns: ";[ cin >> colsA; cout << "********************************" << endl; for(int i=0; i
You have to create A before you can create each A[i], and same for B. It should be a simple fix.
 ``9293`` `````` delete [] A; delete [] B;``````
You have to go through and delete each sub-array before you can delete these outer arrays, otherwise you have a memory leak.
I fixed functions get_A and get_B, the problem now is with the Multiply function to get C. When I finish typing entries of B, I get the same error
 `Segmentation fault (core dumped)`

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````void Multiply(double**A, double** B, int& rowsA, int& colsA) { using namespace std; double** C; //rowsA; //colsC = colsB; C = new double* [rowsA]; //for(int i=0; i
Of course you get a segfault when you comment lines 8-11. ;)
I made the comments because I thought that was the problem, even without that, the program is not running. Thanks for your help.
What do you mean by "the program is not running"? What happens?
I can enter the coefficients of A and B with no problem, and when I'm done it says again: segmentation fault (core dumped). I think the problem is with the function multiply, when it makes the product to get the elements of C, but I have no idea what else to do, and I don't have professors here to ask :(.
Is that when lines 8-11 are commented out? What happens when they are not commented out? (They should not be commented out)
They are not commented now, and it says the same :(
So, you know that the problem is with the function for multiplying C. Look at the function.
 ``4142434445464748`` ``````void FreeMatrix_C(int rowsA, double** C) { for(int i=0; i
I think you copy and pasted because you forgot to change "rowsA" to "rowsC", though it doesn't matter because it is being passed as a parameter. So, look at what you are passing as the parameter:
 ``616263`` `````` FreeMatrix_A(rowsA, A); FreeMatrix_B(rowsB, B); FreeMatrix_C(rowsA, C);``````
Should that not be "rowsC"?
Well, but I just realized that the number of rows of matrix C will be the same as matrix A, and even in the loop I put that condition, so for that reason I didn't see the reason to keep the variable rowsC and I just commented it, I don't know if that's right in C++. Now, if I write it as argument in FreeMatrix_C I will get the error of non defined variable.
LOL, I'm stupid. Sorry about that.

 ``525354555657585960616263`` `````` double** A; double** B; double** C; int rowsA, colsA, rowsB, colsB; intro(); get_A(rowsA, colsA); get_B(rowsB, colsB); Multiply(A, B, rowsA, colsA); FreeMatrix_A(rowsA, A); FreeMatrix_B(rowsB, B); FreeMatrix_C(rowsA, C);``````
Do you see? C never gets set to anything.
 ``1234`` ``````void Multiply(double**A, double** B, int& rowsA, int& colsA) { using namespace std; double** C;``````
This is a completely different C, not the same one as in main.
Hey, no problem. I don't know, I can't see why the C that you say is different from the one in the main. I made some modifications but still I don't see the problem, here goes the code that I have :

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128`` `````` #include #include void intro() { using namespace std; cout << "*****************************************************" << endl; cout << "Program to compute the product of two matrices C=AB" << endl; cout << "*****************************************************" << endl; } void get_A(int& rowsA, int& colsA) { using namespace std; cout << "********************************" << endl; cout << "Enter the dimensions of matrix A" << endl; cout << "********************************" << endl; cout << "Number of rows: "; cin >> rowsA; cout << "Number of columns: "; cin >> colsA; cout << "********************************" << endl; double** A; A = new double* [rowsA]; for(int i=0; i> rowsB; cout << "Number of columns: "; cin >> colsB; cout << "********************************" << endl; double** B; B = new double* [rowsB]; for(int i=0; i
 ``65666768`` ``````void get_C(double** A, double** B, int& rowsA, int& colsA, int& colsB) { using namespace std; double** C; //this makes a brand new variable ``````
 ``111112113114115116`` ``````int main(int argc, char* argv[]) { //using namespace std; double** A; double** B; double** C; //this is an entirely different variable ``````
How can I fix this?, I'm confused.
Pages: 12