1 / 11

Object Oriented Programming Paradigm Lesson 02

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”.

mattox
Download Presentation

Object Oriented Programming Paradigm Lesson 02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming ParadigmLesson 02 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

  2. Multi-dimensional Arrays

  3. 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];

  4. Multi-dimensional Array • Two dimensional array are combination of two array, each array will count its own coordinate.

  5. 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];

  6. 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};

  7. 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.

  8. 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.

  9. 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.

  10. 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; } }

  11. Assignment • What is three dimensional array? Write a simple program for three dimensional array. • Int x[][][];

More Related