Aae oop xp_05


Published on

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Aae oop xp_05

  1. 1. More About ArraysObjectivesIn this lesson, you will learn to: Use two-dimensional and multi-dimensional array in C++ program Pass arrays to function Use enumeration©NIIT OOPS/Lesson 5/Slide 1 of 17
  2. 2. More About ArraysMultidimensional Arrays One-dimensional array can be represented as a single row or column For example: C A T H Y 0 C A T H Y 0©NIIT OOPS/Lesson 5/Slide 2 of 17
  3. 3. More About ArraysIntroducing Two-Dimensional Arrays If you need to store the standard height versus weight chart as given below: Height Weight 4 40 5 50 5.5 60 Two-dimensional array should be used.©NIIT OOPS/Lesson 5/Slide 3 of 17
  4. 4. More About ArraysDeclaring Two-Dimensional Arrays The syntax to declare the two-dimensional array is as follows: data_type variable_name[dimension1_size] [dimension2_size]; where, dimension1_size represents the number of rows and dimension2_size represents the maximum number of elements in one row of the array.©NIIT OOPS/Lesson 5/Slide 4 of 17
  5. 5. More About ArraysDeclaring Two-Dimensional Arrays (Contd..) The syntax to initialize an array is as follows: Name of an array[row number][column number]= value; If initialization has some missing values then they are automatically initialized with the default value. An array can also be initialized at the time of its declaration. All the values of the two-dimensional array should be of the same datatype.©NIIT OOPS/Lesson 5/Slide 5 of 17
  6. 6. More About ArraysTwo-Dimensional Character Arrays Consider the following array definition of a two- dimensional character array: char arr[2][5]; The size of the first dimension of the array is 2 The size of the second dimension of the array is 5 The total number of elements in the array is 2 * 5 = 10©NIIT OOPS/Lesson 5/Slide 6 of 17
  7. 7. More About ArraysProblem Statement 5.D.1As a member of a team that is developing billing systemsoftware for Diaz Telecommunication Inc., you havebeen assigned the task of creating a software modulethat accepts the mobile number and billing amount of fivecustomers and then display the value in a tabular format.©NIIT OOPS/Lesson 5/Slide 7 of 17
  8. 8. More About ArraysJust a Minute…2. Two-Dimensional array can store multiple datatype values(T/F).3. If no value is passed to a character datatype two- dimensional array, then it is initialized with space(T/F)4. What will be the output of following program: a. #includeiostream int main() {float salary[4][5]; salary[1][1]=1.0; salary[2][1]=4.0; coutsalary[1][1] ” “salary[2][1]endl;}©NIIT OOPS/Lesson 5/Slide 8 of 17
  9. 9. More About ArraysJust a Minute…(Contd..) a. What will be the output of following program: #includeiostream int main() { float salary[4][5]; salary[1][1]=1.0; salary[2][1]=4.0; coutsalary[3][2];}2. Find out an error in following statement: int wrong [2][1]={{1,”a”},{2,”b”},{3,”c”}} ©NIIT OOPS/Lesson 5/Slide 9 of 17
  10. 10. More About ArraysIntroducing Multi-Dimensional Arrays An array with more than two dimensions is known as multi-dimensional array. The syntax to declare a multi dimensional array is as follows: datatype array_name[n1][n2][n3]....[nm]; where n1, n2…are the dimensions., which specify the size of the array dimension©NIIT OOPS/Lesson 5/Slide 10 of 17
  11. 11. More About ArraysProblem Statement 5.P.1Write a program to accept marks of six subjects for tenstudents and then display their total marks in a tabularformat.[Hint: Create a class and function to accept marks,display marks.]©NIIT OOPS/Lesson 5/Slide 11 of 17
  12. 12. More About ArraysPassing Array as Parameter When there is a need to pass more than one variable to a function,then it is preferable to pass all the values through an array. Arrays are inherently passed to functions by the call by reference method. For Example: void Calculate_TotMarks(int mks[]) { // code }©NIIT OOPS/Lesson 5/Slide 12 of 17
  13. 13. More About ArraysProblem Statement 5.P.2Make a program to accept following values in two arrays 1 2 3 1 2 3 4 5 6 4 5 6 7 8 9 7 8 9Using above two matrix, generate the following output 2 4 6 8 10 12 14 16 18[Hint: Pass the array as parameter to the function]©NIIT OOPS/Lesson 5/Slide 13 of 17
  14. 14. More About ArraysEnumeration (ENUM) An enumeration is a user-defined type consisting of a set of named constants called enumerators It serves to create data types, that is not limited to either numerical or character constants or Boolean values The syntax to declare an enum is as follows: enum model_name { value1, value2, value3, . .};©NIIT OOPS/Lesson 5/Slide 14 of 17
  15. 15. More About ArraysEnumeration (ENUM) (Contd..) By default, the first enumerator has a value of 0 Each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator An enumerator can be promoted to an integer value. Converting an integer to an enumerator requires an explicit cast The following statement will initialize the enum object mycolor to blue: mycolor = blue;©NIIT OOPS/Lesson 5/Slide 15 of 17
  16. 16. More About ArraysSummaryIn this lesson, you learned that: Two Dimensional arrays are used to store the values in a tabular format. Two Dimensional arrays consist of rows and columns. Multidimensional array consist of three or higher dimensions. Total number of elements in any dimension of arrays is the product of all sizes included in the declaration. Array can be passed as a parameter to the function, to simplify the manipulation on multiple variables.©NIIT OOPS/Lesson 5/Slide 16 of 17
  17. 17. More About ArraysSummary (Contd..)In this lesson, you learned that: An enumeration is a user-defined type consisting of a set of named constants called enumerators.©NIIT OOPS/Lesson 5/Slide 17 of 17