0% found this document useful (0 votes)
21 views

C++Lec3 - Multi - DimensionalArray

The document discusses C++ arrays and multi-dimensional arrays. It covers topics like declaring, initializing, accessing array elements, passing arrays to functions, returning arrays from functions. It also provides examples to demonstrate various array operations and concepts.

Uploaded by

haponabeel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C++Lec3 - Multi - DimensionalArray

The document discusses C++ arrays and multi-dimensional arrays. It covers topics like declaring, initializing, accessing array elements, passing arrays to functions, returning arrays from functions. It also provides examples to demonstrate various array operations and concepts.

Uploaded by

haponabeel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

C++

Multi- Dimensional
Array
Lecture – 3

Dr. Aghabi Abosaif


October 2023

1
C++ Programming language
 C++ is a statically typed, compiled, general-purpose,
case-sensitive programming language that supports
procedural, object-oriented, and generic programming.

 C++ is regarded as a middle-level language, as it


comprises a combination of both high-level and low-
level language features.

 C++ was developed by Bjarne Stroustrup starting in 1979


at Bell Labs in Murray Hill, New Jersey.
C++ Programming language
 It`s enhancement to the C language and originally named
C with Classes but later it was renamed C++ in 1983.

 A programming language is said to use static typing


when type checking is performed during compile-time as
opposed to run-time.
Object-Oriented Programming

 C++ fully supports object-oriented programming,


including the four pillars of object-oriented development:
 Encapsulation
 Data hiding
 Inheritance
 Polymorphism
Standard Libraries
 Standard C++ consists of three important parts:
 The core language giving all the building blocks
including variables, data types and literals, etc.

 The C++ Standard Library giving a rich set of functions


manipulating files, strings, etc.

 The Standard Template Library (STL) giving a rich set


of methods manipulating data structures, etc.
The ANSI Standard

 The ANSI standard is an attempt to ensure that C++ is


portable; that code you write for Microsoft's compiler
will compile without errors, using a compiler on a Mac,
UNIX, a Windows box.
C++ Compiler
 This is an actual C++ compiler, which will be used to
compile your source code into final executable program.

 Source code extension is .cpp by default.

 Most frequently used and free available compiler is GNU


C/C++ compiler.
Basic Syntax
 C++ program, it can be defined as a collection of objects
that communicate via invoking each other's methods.

 Object - Objects have states and behaviors. Example: A


student has states – id, name, Address as well as
behaviors – GPA. An object is an instance of a class.

 Class - A class can be defined as a template/blueprint


that describes the behaviors/states that object of its type
support.
Basic Syntax
 Methods - A method is basically a behavior. A class can
contain many methods. It is in methods where the logics
are written, data is manipulated and all the actions are
executed.

 Instant Variables - Each object has its unique set of


instant variables. An object's state is created by the
values assigned to these instant variables.
C++ Arrays
 C++ provides an array data structure, which stores a fixed-size
sequential collection of elements of the same type.
 It is often more useful to think of an array as a collection of
variables of the same type.

 Instead of declaring individual variables, such as number0,..., and


number99, you declare one array variable such as numbers and
use numbers[0], ..., numbers[99] to represent individual variables.

 Element in an array is accessed by an index.

 All arrays consist of contiguous memory locations. The lowest


address corresponds to the first element and the highest address
to the last element.
Declaring Arrays
one-Dimensional Array
 To declare an array in C++, the programmer specifies the
type of the elements array name , arrayNmae and the
number of elements required by an array.

 The array-Size must be an integer constant greater than


zero and type can be any valid C++ data type.
Initializing Arrays
 You can initialize C++ array elements either one by one or using a
single statement.
 The number of values between braces { } cannot be larger than
the number of elements that we declare for the array between
square brackets [ ].
 If you omit the size of the array, an array just big enough to hold
the initialization is created.
Accessing Array Elements
 An element is accessed by indexing the array name. This
is done by placing the index of the element within
square brackets after the name of the array.

Default array Values ?


1D-Array –Example1

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

 Where type can be any valid C++ data type and


arrayName will be a valid C++ identifier.
Two-Dimensional Arrays
 A 2D-array can be think as a table, which will have x number of
rows and y number of columns.
 A 2-dimensional array a, which contains three rows and four
columns can be shown as below

 Every element in array a is identified by an element name of the


form a[ i ][ j ]
 a is the name of the array.
 i , j are the subscripts that uniquely identify each element in a.
Initializing Two-Dimensional Arrays
 Multi-dimensioned arrays may be initialized by specifying
bracketed values for each row.
 Following is an array with 3 rows and each row have 4 columns.

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

 Write a program to find frequency of given number


in a sorted array it`s length is ten elements?

 Write a program to sort array elements from


smallest to largest?

You might also like