Arrays Dynamic Array Multidimensional Array Matric Operation with

  • Slides: 21
Download presentation
Arrays Dynamic Array Multidimensional Array Matric Operation with Array

Arrays Dynamic Array Multidimensional Array Matric Operation with Array

DYNAMIC ARRAY

DYNAMIC ARRAY

How do you create Dynamic Array A dynamic array is an array whose size

How do you create Dynamic Array A dynamic array is an array whose size is determined when the program is running, not when you write the program

Example int main () { int i, n; int * p; cout << "How

Example int main () { int i, n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == 0) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p[n]; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p[n] << ", "; delete[] p; } return 0; }

MULTIDIMENSIONAL ARRAY

MULTIDIMENSIONAL ARRAY

How do you create it? Declare it using:

How do you create it? Declare it using:

How to read it/display it

How to read it/display it

Actually it is…

Actually it is…

Example

Example

Example: Output

Example: Output

MATRIC OPERATION WITH ARRAY

MATRIC OPERATION WITH ARRAY

How to do multiplication

How to do multiplication

Rules of Multiplication • Number of COLUMN for First ARRAY must be the same

Rules of Multiplication • Number of COLUMN for First ARRAY must be the same with Number of ROW for Second ARRAY • You must properly define who is the First Array and who is the Second Array • Example: – Matrix 3 x 5 with Matrix 5 x 1 = RIGHT – Matrix 2 x 6 with Matrix 2 x 4 = WRONG

Example • Let say you have From the example, we first take “ 1”

Example • Let say you have From the example, we first take “ 1” from (First Array) the first row first column multiply with “ 2” from (Second Array) the first row first column. Then PLUS (+) with “ 2” from (First Array) the first row second column multiply with “ 0” from (Second Array) the second row first column. We will have 1 x 2 + 2 x 1 = 4

But how to do that in C++?

But how to do that in C++?

But how to do that in C++? We iterate first Array but according to

But how to do that in C++? We iterate first Array but according to “m” value (MAX ROW)

But how to do that in C++? We iterate second Array according to the

But how to do that in C++? We iterate second Array according to the “q” value (MAX COLUMN)

But how to do that in C++? We let all the content = 0

But how to do that in C++? We let all the content = 0 for the first time

But how to do that in C++? K is for storing the result of

But how to do that in C++? K is for storing the result of multiplication. P is MAX ROW for SECOND ARRAY

But how to do that in C++? Remember to add c[i][j] for next “k”

But how to do that in C++? Remember to add c[i][j] for next “k” iteration after multiplication

Matrix Operation with Array • Refer source code explanation

Matrix Operation with Array • Refer source code explanation