110 likes | 273 Views
Object Oriented Programming Paradigm Lesson 02. Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh. Multi-dimensional Arrays. Multi-dimensional Arrays. Multi-dimensional arrays are also called as “arrays of arrays”.
E N D
Object Oriented Programming ParadigmLesson 02 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh
Multi-dimensional Arrays • Multi-dimensional arrays are also called as “arrays of arrays”. • It is also using contiguous memory places but in rows and columns • It is useful for storing multiple sets of data. • Syntax: • type name[rows][columns];
Multi-dimensional Array • Two dimensional array are combination of two array, each array will count its own coordinate.
Multi-dimensional Array(Defining Multidimensional Arrays) Defining Multi-dimensional arrays • The multi-dimensional array is defined with size specifiers, each enclosed in brackets, some examples of two-dimensional arrays: • myarray[5][6]; • Newarray[3][4];
Multi-dimensional Array(Initializing Multidimensional Arrays) Initializing Multi-dimensional arrays • The multi-dimensional array is initialized with commas and separated by middle brackets: • int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ }; OR • int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Multi-dimensional Array (Square & Cube program) #include <iostream.h> int main() { int wd=2; int ht=2; int area[2][2]={{5,10},{20,30,}}; for(int a=0;a<wd;a++) for(int b=0;b<ht;b++) { cout<<area[a][b]<<"\t"; cout<<area[a][b]*area[a][b]<<endl; cout<<area[a][b]*area[a][b] *area[a][b]<<endl; } } • Write a program using 2 dimensional arrays which calculate the square root and cube of the elements of array.
Multi-dimensional Array (Square & Cube program) #include <iostream.h> int main () { int rows=5; int cols=2; //an array with 5 rows and 2 columns int c[5][2] = { {0,1}, {1,2}, {2,3}, {3,4}, {4,5},}; //output each array element's value for(inti = 0; i < 5; i++) { for(int j = 0; j < 2; j++) { cout << c[i][j]; } cout<<endl; } } • Write an two-dimensional array program, which shows the output of five rows and two columns.
Multi-dimensional Array (Square & Cube program) #include <iostream.h> int main () { int rows=5; int cols=2; //an array with 5 rows and 2 columns int c[2][3] = { {25,30,35}, {40,45,50},}; //output each array element's value for(inti = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { cout << c[i][j]; } cout<<endl; } } • Write a two-dimensional array program, which shows the output of two rows and three columns.
Multi-dimensional Array (Square & Cube program) • Write an two-dimensional array program, which stores marks of three subjects of three students of the class and sum of result of each student. #include <iostream.h> int main () { int sum=0; //an array with 3 subjects and 3 students int c[3][3] = { {60,70,80,}, {50,50,50,}, {70,80,90,},}; //output each array element's value for(inti = 0; i < 3; i++) //Loop counts each column { for(int j = 0; j < 3; j++) //Loop count each rows { cout << c[i][j]<<"\t"; sum+=c[i][j]; } cout<<"Sum: "<<sum; //sum of each row cout<<endl; sum=0; } }
Assignment • What is three dimensional array? Write a simple program for three dimensional array. • Int x[][][];