SlideShare ist ein Scribd-Unternehmen logo
1 von 17
More About Arrays


Objectives
In 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
More About Arrays


Multidimensional 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
More About Arrays


Introducing 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
More About Arrays


Declaring 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
More About Arrays


Declaring 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
More About Arrays


Two-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
More About Arrays


Problem Statement 5.D.1
As a member of a team that is developing billing system
software for Diaz Telecommunication Inc., you have
been assigned the task of creating a software module
that accepts the mobile number and billing amount of five
customers and then display the value in a tabular format.




©NIIT                                OOPS/Lesson 5/Slide 7 of 17
More About Arrays

Just 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
More About Arrays


Just 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
More About Arrays


Introducing 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
More About Arrays


Problem Statement 5.P.1
Write a program to accept marks of six subjects for ten
students and then display their total marks in a tabular
format.
[Hint: Create a class and function to accept marks,
display marks.]




©NIIT                              OOPS/Lesson 5/Slide 11 of 17
More About Arrays


Passing 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
More About Arrays


Problem Statement 5.P.2
Make 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        9


Using 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
More About Arrays

Enumeration (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
More About Arrays


Enumeration (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
More About Arrays


Summary
In 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
More About Arrays


Summary (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

Weitere ähnliche Inhalte

Was ist angesagt?

C programming session 05
C programming session 05C programming session 05
C programming session 05Dushmanta Nath
 
2.6 support vector machines and associative classifiers revised
2.6 support vector machines and associative classifiers revised2.6 support vector machines and associative classifiers revised
2.6 support vector machines and associative classifiers revisedKrish_ver2
 
Tutorial - Support vector machines
Tutorial - Support vector machinesTutorial - Support vector machines
Tutorial - Support vector machinesbutest
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmeticfyjordan9
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]ecko_disasterz
 
Support Vector Machines- SVM
Support Vector Machines- SVMSupport Vector Machines- SVM
Support Vector Machines- SVMCarlo Carandang
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
Support Vector Machine (Classification) - Step by Step
Support Vector Machine (Classification) - Step by StepSupport Vector Machine (Classification) - Step by Step
Support Vector Machine (Classification) - Step by StepManish nath choudhary
 
Support vector machines (svm)
Support vector machines (svm)Support vector machines (svm)
Support vector machines (svm)Sharayu Patil
 
C programming session 05
C programming session 05C programming session 05
C programming session 05AjayBahoriya
 

Was ist angesagt? (18)

Computer
ComputerComputer
Computer
 
Svm vs ls svm
Svm vs ls svmSvm vs ls svm
Svm vs ls svm
 
Pointer
PointerPointer
Pointer
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 
Support Vector machine
Support Vector machineSupport Vector machine
Support Vector machine
 
Pointers
 Pointers Pointers
Pointers
 
2.6 support vector machines and associative classifiers revised
2.6 support vector machines and associative classifiers revised2.6 support vector machines and associative classifiers revised
2.6 support vector machines and associative classifiers revised
 
Tutorial - Support vector machines
Tutorial - Support vector machinesTutorial - Support vector machines
Tutorial - Support vector machines
 
Operators
OperatorsOperators
Operators
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
 
Support Vector Machines- SVM
Support Vector Machines- SVMSupport Vector Machines- SVM
Support Vector Machines- SVM
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Support Vector Machine (Classification) - Step by Step
Support Vector Machine (Classification) - Step by StepSupport Vector Machine (Classification) - Step by Step
Support Vector Machine (Classification) - Step by Step
 
Support vector machines (svm)
Support vector machines (svm)Support vector machines (svm)
Support vector machines (svm)
 
C programming session 05
C programming session 05C programming session 05
C programming session 05
 

Andere mochten auch

Andere mochten auch (14)

Lecture1 classes1
Lecture1 classes1Lecture1 classes1
Lecture1 classes1
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array in c++
Array in c++Array in c++
Array in c++
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Array in C
Array in CArray in C
Array in C
 
Arrays
ArraysArrays
Arrays
 
Array in c language
Array in c languageArray in c language
Array in c language
 

Ähnlich wie Aae oop xp_05

Ähnlich wie Aae oop xp_05 (20)

C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
Lecture_01.2.pptx
Lecture_01.2.pptxLecture_01.2.pptx
Lecture_01.2.pptx
 
Savitch Ch 07
Savitch Ch 07Savitch Ch 07
Savitch Ch 07
 
Savitch Ch 07
Savitch Ch 07Savitch Ch 07
Savitch Ch 07
 
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
 
Savitch ch 07
Savitch ch 07Savitch ch 07
Savitch ch 07
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)Array & Exception Handling in C# (CSharp)
Array & Exception Handling in C# (CSharp)
 
Ch8 Arrays
Ch8 ArraysCh8 Arrays
Ch8 Arrays
 
Object Oriented Programming - 5.1. Array
Object Oriented Programming - 5.1. ArrayObject Oriented Programming - 5.1. Array
Object Oriented Programming - 5.1. Array
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Unit 3
Unit 3 Unit 3
Unit 3
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
Copyright © 2012 Pearson Addison-Wesley. All rights reserve.docx
Copyright © 2012 Pearson Addison-Wesley.  All rights reserve.docxCopyright © 2012 Pearson Addison-Wesley.  All rights reserve.docx
Copyright © 2012 Pearson Addison-Wesley. All rights reserve.docx
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 

Mehr von Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Kürzlich hochgeladen

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 

Kürzlich hochgeladen (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 

Aae oop xp_05

  • 1. More About Arrays Objectives In 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. More About Arrays Multidimensional 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. More About Arrays Introducing 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. More About Arrays Declaring 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. More About Arrays Declaring 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. More About Arrays Two-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. More About Arrays Problem Statement 5.D.1 As a member of a team that is developing billing system software for Diaz Telecommunication Inc., you have been assigned the task of creating a software module that accepts the mobile number and billing amount of five customers and then display the value in a tabular format. ©NIIT OOPS/Lesson 5/Slide 7 of 17
  • 8. More About Arrays Just 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. More About Arrays Just 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. More About Arrays Introducing 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. More About Arrays Problem Statement 5.P.1 Write a program to accept marks of six subjects for ten students and then display their total marks in a tabular format. [Hint: Create a class and function to accept marks, display marks.] ©NIIT OOPS/Lesson 5/Slide 11 of 17
  • 12. More About Arrays Passing 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. More About Arrays Problem Statement 5.P.2 Make 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 9 Using 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. More About Arrays Enumeration (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. More About Arrays Enumeration (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. More About Arrays Summary In 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. More About Arrays Summary (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