Sie sind auf Seite 1von 6

Mapping Function for Array

1- One Dimensional Array

baseAddress + index * sizeofType


e.g.
char a[10];

a[4]='d';

BaseAddres=100

100+4*1

e.g.
float a[10];

a[4]=2.5f;

BaseAddres=100

100+4*4

e.g.
struct student{
int id; // 4
char name[30];
float cgpa;
}
student a[10];

a[4].cgpa=2.5f;

BaseAddres=100

100+4*38

2- two Dimensional Array


- a matrix (Abstraction)
- order row x col

a 2x3

10 20 30
40 50 60

int a[2][3];
a[0][0]=10;
a[1][2]=60;

- physically all memory is flattened


- Matrix is only user view
- Physical view

i) Row Major (c++,c,java,, python..)

10 20 30 40 50 60
ii) Col Major (fortran, cobol)
10 40 20 50 30 60

Mapping Function

1) Row Major
Ba+ i*n+j *sizeofType
i=row index
j=col index
n= size of total cols in matrix

2) Col Major
Ba+ j*n+i *sizeofType
i=row index
j=col index
n= size of total rows in matrix

example : (row Major)


a 2x3

10 20 30
40 50 60

int a[2][3];
a[0][0]=10;//100
a[1][2]=60;//120
a[1][1]=50;//116

10 20 30 40 50 60(row Major inc++)

100 104 108 112 116 120

Ba+ i*n+j *sizeofType


100+ 1*3+1 *4

example : (Col Major)


a 2x3

10 20 30
40 50 60

int a[2][3];
a[0][0]=10;//100
a[1][0]=40;//104
a[1][2]=60;// 120

10 40 20 50 30 60 (col Major in fotran)

100 104 108 112 116 120

Ba+ j*n+i *sizeofType


100+ 2*2+1 *4

..............................................
Example Print Array

void print(int b[][3],int row, int col){


for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
cout<<b[i][j]; //ba+i*n+j*size

void main(){
int a[2][3]={{10,20,30},
{40,50,60}};
print(a,2,3);
int b[2][4]={{10,20,30,40},
{40,50,60,70}};
print(b,2,4);// not workable

- Generic print of 2d Arrays can't be written using


2d array Syntax

...................................

Example Print Array

void print(int b[][3],int row, int col){


for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
cout<<b[i][j]<<endl; //ba+i*n+j*size

void main(){
int a[2][3]={{10,20,30},
{40,50,60}};
print(a,2,3);
int b[2][4]={{10,20,30,40},
{40,50,60,70}};
print(b,2,4);// not workable

}
......................................

Example Print Array (Pointer) Solution 1


Objective: generic print function

void print(int *b,int row, int col){


for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
cout<<*(b+i*col+j)<<endl;//ba+i*n+j*size

void main(){
int *a=new int [6];
a[0]=10;a[1]=20;a[2]=30;a[3]=40;a[4]=50;a[5]=60;
print(a,2,3);

int *b=new int [8];


b[0]=10;b[1]=20;b[2]=30;b[3]=40;b[4]=50;b[5]=60;
b[6]=70;b[7]=80;
print(b,2,4);// not workable

..........................................

Example Print Array (Pointer) Solution 2


Objective: generic print function
use b as one d Array and mapping Function

void print(int *b,int row, int col){


for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
cout<<b[i*col+j]<<endl;//ba+i*n+j*size

void main(){
int *a=new int [6];
a[0]=10;a[1]=20;a[2]=30;a[3]=40;a[4]=50;a[5]=60;
print(a,2,3);

int *b=new int [8];


b[0]=10;b[1]=20;b[2]=30;b[3]=40;b[4]=50;b[5]=60;
b[6]=70;b[7]=80;
print(b,2,4);// not workable

...............................................

Example Matrix and an astraction

#include <iostream>
#include <string>

using namespace std;


class Matrix{
public :
Matrix() { }
Matrix(int r, int c){
row=r;
col=c;
p=new int[row*col];
for (int i=0;i<row*col;i++)
p[i]=0;
}
void get(){
for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
cin>>*(p+i*col+j); // p[i*col+j]
}
void print(){
for (int i=0;i<row;i++){
for (int j=0;j<col;j++)
cout<<*(p+i*col+j)<<"\t";//// p[i*col+j]
cout<<endl;
}
}
Matrix multiply(Matrix b){
Matrix temp(row,b.col);
for (int i=0;i<row;i++)
for (int j=0;j<b.col;j++)
for (int k=0;k<col;k++)
temp.p[i*b.col+j]=temp.p[i*b.col+j]+p[i*col+k]* b.p[k*b.col+j];

return temp;

}
private :
int *p;
int row;
int col;
};

void main(){
Matrix a(2,3);
a.get();
Matrix b(3,2);
b.get();
Matrix c(2,2);
c=a.multiply(b);
c.print();
}

....................................................

#include <iostream.h>

class Matrix{
public :
Matrix() { }
Matrix (int r,int c){
row=r;
col=c;
p=new int[row*col];
for(int i=0;i<row*col;i++)
p[i]=0;
}
void get() {
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
cin>>p[i*col+j];

}
void print() {
for(int i=0;i<row;i++){
for(int j=0;j<col;j++)
cout<<p[i*col+j]<<"\t";
cout<<endl;
}

}
Matrix mul(Matrix b){
Matrix temp(row,b.col);
for(int i=0;i<row;i++)
for(int j=0;j<b.col;j++)
for(int k=0;k<col;k++)
temp.p[i*b.col+j]=
temp.p[i*b.col+j]+p[i*col+k]*
b.p[k*b.col+j];

return temp;
}

private:
int *p;
int row;
int col;
};

void main(){
Matrix a(2,3);
Matrix b(3,2);
Matrix c(2,2);
a.get();
a.print();
b.get();
b.print();
c=a.mul(b);
c.print();

}
..................................................

Assignment 1

Q- Implement Matrix class with following functions

1- Mutiply
2- Add
3- Transpose
4- Determinent
5- print
6- get
7- subtraction

Das könnte Ihnen auch gefallen