C++Lec3 - Multi - DimensionalArray
C++Lec3 - Multi - DimensionalArray
Multi- Dimensional
Array
Lecture – 3
1
C++ Programming language
C++ is a statically typed, compiled, general-purpose,
case-sensitive programming language that supports
procedural, object-oriented, and generic programming.
NOTE:
using namespace std
means that we can use
names for objects and
variables from the
standard library.
1D-Array –Example2
1D-Array –Example3
Display Elements in Array
1D-Array –Example4
1D-Array –Example5
Find the sum of Elements in Array
Two-Dimensional Arrays
The simplest form of the multi-dimensional array is the
two-dimensional array.
Declare a two-dimensional integer array of size x,y, as
follows
The nested braces, which indicate the intended row, are optional.
The following initialization is equivalent to previous example
Accessing Two-Dimensional Array Elements
An element in 2D-dimensional array is accessed by using the
subscripts, row index and column index of the array.
The above statement will take the 4th element from the 3rd row
of the array.
2-D Array–Example1
Array Important Concepts
There are following few important concepts, which
should be clear to a C++ programmer
Concept Description
Multi-dimensional arrays C++ supports multidimensional arrays.
The simplest form of the multidimensional
array is the two-dimensional array.
Pointer to an array You can generate a pointer to the first element
of an array by simply specifying the array
name, without any index.
Passing arrays to functions You can pass to the function a pointer to an
array by specifying the array's name without
an index.
Return array from functions C++ allows a function to return an array.
Passing Arrays to Functions
C++ does not allow to pass an entire array as an argument to a
function.
However, You can pass a pointer to an array by specifying the
array's name without an index.
If you want to pass a single-dimension array as an argument in a
function, you would have to declare function formal parameter in
one of following three ways
all three declaration methods produce similar results because
each tells the compiler that an integer pointer is going to be
received.
Passing Arrays to Functions
Way-1
Formal parameters as a pointer
Passing Arrays to Functions
Way-2
Formal parameters as a sized array
Passing Arrays to Functions
Way-3
Formal parameters as an unsized array
Average of the Numbers Passed
through an Array –Example2
Average of the Numbers Passed
through an Array –Example2
NOTE:
As you can see, the length of the array doesn't matter as far as the function is concerned
because C++ performs no bounds checking for the formal parameters
Return Array from Functions
C++ does not allow to return an entire array as an argument to a
function.
However, you can return a pointer to an array by specifying the
array's name without an index.
If you want to return a single-dimension array from a function, you
would have to declare a function returning a pointer.
Second point to remember is that C++ does not supporter to
return the address of a local variable to outside of the function so
you would have to define the local variable as static variable.
Display Array using Functions
Example3
Search for Array Element
Example4
Search for Array Element
Example4
Assignment No2.