Más contenido relacionado
La actualidad más candente (18)
Similar a Aae oop xp_05 (20)
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