Subscript notation to pointer notation

I got this assignment the other day in which I had to convert this project I had done from subscript notation to pointer notation. I dont really understand the syntax used for pointer notation. My program below (using subscript notation) works fine but I dont really know exactly how to go about converting it to pointer notation. Any information would help.

The program just creates two matrices using 2 dimensional dynamic arrays. It then goes on to perform addition, subtraction, and multiplication between the matrices.


#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

template<class X>
class Matrix
{
private:
X **ptr;
int row,col;
friend istream & operator >> (istream &,Matrix &);
friend ostream & operator << (ostream &,Matrix &);
public:
Matrix(int=0,int=0);
~Matrix();
void inputValues();
void outputValues();
void operator - (Matrix &); //for sets difference
void operator + (Matrix &); //for sets intersection
void operator * (Matrix &); //for sets multiplication
};

template<class X>
Matrix<X>::Matrix(int r,int c)
{
row=r;
col=c;
ptr=new X * [row];

for(int a=0; a<row; a++)
{ ptr[a]=new X[col]; }
}

template<class X>
Matrix<X>::~Matrix()
{
for(int a=0; a<row; a++)
{ delete[]ptr[a]; }
delete[]ptr;
}

template<class X>
void Matrix<X>::inputValues()
{
cin>>*this;
}

template<class X>
void Matrix<X>::outputValues()
{
cout<<*this;
}

template<class X>
void Matrix<X>::operator + (Matrix &t)
{ int a,b; cout<<endl;

if(row==t.row && col==t.col)
{
cout<<"Matrix A + Matrix B: ";

for(a=0; a<row; a++)
{ cout<<endl;
for(b=0; b<col; b++)
{
cout<<ptr[a][b]+t.ptr[a][b]<<" ";
} }
cout<<endl; }
else { cout<<"The addition of these two matrices is undefined."<<endl; }
}

template<class X>
void Matrix<X>::operator - (Matrix &t)
{ int a,b; cout<<endl;

if(row==t.row && col==t.col)
{
cout<<"Matrix A - Matrix B: ";

for(a=0; a<row; a++)
{ cout<<endl;
for(b=0; b<col; b++)
{
cout<<ptr[a][b]-t.ptr[a][b]<<" ";
} }
cout<<endl; }
else { cout<<"The subtraction of these two matrices is undefined."<<endl; }
}

template<class X>
void Matrix<X>::operator * (Matrix &t)
{ cout<<endl;

if(row==t.col)
{
cout<<"Matrix A * Matrix B: ";

for(int a=0; a<row; a++)
{ cout<<endl;
for(int b=0; b<col; b++)
{
cout<<ptr[a][b]*t.ptr[a][b]<<" ";
} }
cout<<endl; }
else { cout<<"The multiplication of these two matrices is undefined."<<endl; }
}

template<class X>
istream& operator >> (istream & input,Matrix<X> & t)
{

cout<<"Please enter the values in your matrix set."<<endl;
for(int a=0; a<t.row; a++)
{ cout<<endl;
for(int b=0; b<t.col; b++)
{
input>>t.ptr[a][b]; }} cout<<endl;

return input;
}

template<class X>
ostream& operator << (ostream & output,Matrix<X> & t)
{
cout<<"The values in your matrix set are: "<<endl;

for(int a=0; a<t.row; a++)
{ cout<<endl;
for(int b=0; b<t.col; b++)
{
output<<t.ptr[a][b]<<" "; }} cout<<endl;

return output;
}

int main()
{
int r1=0,c1=0,r2=0,c2=0,b;
cout<<"Which set type do you want: 1. Integer, 2. Float, or 3. Character."<<endl;
cin>>b;
cout<<"First enter the number of rows in first matrix and then the columns."<<endl;
cin>>r1>>c1;
cout<<"First enter the number of rows in second matrix and then the columns."<<endl;
cin>>r2>>c2;

Matrix<int> intmat1(r1,c1),intmat2(r2,c2);
Matrix<float> floatmat1(r1,c1),floatmat2(r2,c2);
Matrix<char> charmat1(r1,c1),charmat2(r2,c2);

if(b==1)
{
intmat1.inputValues();
intmat2.inputValues();
intmat1.outputValues();
intmat2.outputValues();

cout<<endl;

intmat1+intmat2; //shows the sets intersection
cout<<endl;
intmat1-intmat2; //shows the sets difference
cout<<endl;
intmat1*intmat2; //shows the sets multiplication
cout<<endl;
}

else if(b==2)
{
floatmat1.inputValues();
floatmat2.inputValues();
floatmat1.outputValues();
floatmat2.outputValues();

cout<<endl;

floatmat1+floatmat2; //shows the sets intersection
cout<<endl;
floatmat1-floatmat2; //shows the sets difference
cout<<endl;
floatmat1*floatmat2; //shows the sets multiplication
cout<<endl;
}

else
{
charmat1.inputValues();
charmat2.inputValues();
charmat1.outputValues();
charmat2.outputValues();

cout<<endl;

charmat1+charmat2; //shows the sets intersection
cout<<endl;
charmat1-charmat2; //shows the sets difference
cout<<endl;
charmat1*charmat2; //shows the sets multiplication
cout<<endl;
}

getch();

}
arr[x] = *(arr+x). An array is really just a block of memory in which data of the same kind is stored sequentially.

for example,

1
2
3
struct Example {
int a; int b; int c;
};


If we create a new Example array

 
Example* test = new Example[3];


Then test will be a pointer to the first element in the array. Lets assume that sizeof(int) == 4 and arr == 0x1000

*arr (or *(arr+0)) will get us the first array element at location 1000. *(arr+1) will get us the second element at location 100C (because our struct has a size of 12 bytes total, arr+1 == (Example*)((char*)arr+12)) which is equivalent to what arr[1] gets us.

Did I make myself clear?
Last edited on
@ ADEEL SHEIKH brther are u studying in Univ of SOUTH ASIA ? DATA STRUCTURES?
bhai aap ki multiplication ka function galt ha ,,is me 3 nested loop lgy gy...and condition ye hoti ha
columns of 1st matrix = rows of 2nd matrix...
Topic archived. No new replies allowed.