PF 6
PF 6
Lec #6
What is modular programming?
Modular programming is a software design technique that emphasizes
separating the functionality of a program into independent, interchangeable
modules, such that each contains everything necessary to execute only one
aspect of the desired functionality.
Modular programming is a general programming concept where developers
separate program functions into independent pieces. These pieces then act
like building blocks, with each block containing all the necessary parts to
execute one aspect of functionality.
Concept of Modularization
The main program must establish the existence of functions used in that
program. Depending on the programming language, there is a formal way to:
define a function (its definition or the code it will execute)
call a function
declare a function (a prototype is a declaration to a compiler)
Advantages of modular programming
In C++, an array is a data structure that is used to store multiple values of similar data
types in a contiguous memory location.
Example:
if we have to store the marks of 4 or 5 students then we can easily store them by
creating 5 different variables but what if we want to store marks of 100 students or say
500 students then it becomes very challenging to create that numbers of variable and
manage them. Now, arrays come into the picture that can do it easily by just creating an
array of the required size.
Properties of Arrays in C++
An Array is a collection of data of the same data type, stored at a contiguous memory
location.
Indexing of an array starts from 0. It means the first element is stored at the 0th index,
the second at 1st, and so on.
Elements of an array can be accessed using their indices.
Once an array is declared its size remains constant throughout the program.
An array can have multiple dimensions.
The number of elements in an array can be determined using the sizeof operator.
We can find the size of the type of elements stored in an array by subtracting adjacent
addresses.
Initialization of Array in C++
In C++, we can initialize an array in many ways but we will discuss some most
common ways to initialize an array. We can initialize an array at the time of
declaration or after declaration.
1. Initialize Array with Values in C++
We have initialized the array with values. The values enclosed in curly braces
‘{}’ are assigned to the array. Here, 1 is stored in arr[0], 2 in arr[1], and so
on. Here the size of the array is 5.
Here, we have declared an array ‘partialArray’ with size ‘5’ and with values
‘1’ and ‘2’ only. So, these values are stored at the first two indices, and at
the rest of the indices ‘0’ is stored.
We can initialize the array with all elements as ‘0’ by specifying ‘0’ inside the
curly braces. This will happen in case of zero only if we try to initialize the
array with a different value say ‘2’ using this method then ‘2’ is stored at the
0th index only.
In C++, we can declare an array by simply specifying the data type first and
then the name of an array with its size.
data_type array_name[Size_of_array];
Example
Here,
int: It is the type of data to be stored in the array. We can also use other data
types such as char, float, and double.
arr: It is the name of the array.
5: It is the size of the array which means only 5 elements can be stored in the
array.
The C++ Program to Illustrate How to
Access Array Elements
// C++ Program to Illustrate How to Access Array Elements
#include <iostream>
using namespace std;
int main()
{
int arr[3];
return 0;
}
//C++ Program to Illustrate How to
Traverse an Array
#include <iostream>
using namespace std;
int main()
{
return 0;
}