0% found this document useful (0 votes)
29 views22 pages

PF 6

Uploaded by

mrsfahad550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views22 pages

PF 6

Uploaded by

mrsfahad550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Modular programing

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

 One of the most important concepts of programming is the ability to group


some lines of code into a unit that can be included in our program. The
original wording for this was a sub-program. Other names include: macro,
sub-routine, procedure, module and function. We are going to use the
term function for that is what they are called in most of the predominant
programming languages of today. Functions are important because they allow
us to take large complicated programs and to divide them into smaller
manageable pieces. Generally, functions fall into two categories:
Categories

 Program Control – Functions used to simply sub-divide and control the


program. These functions are unique to the program being written. Other
programs may use similar functions, maybe even functions with the same
name, but the content of the functions are almost always very different.
 Specific Task – Functions designed to be used with several programs. These
functions perform a specific task and thus are usable in many different
programs because the other programs also need to do the specific task.
Specific task functions are sometimes referred to as building blocks. Because
they are already coded and tested, we can use them with confidence to more
efficiently write a large program.
Continued…….

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

 Code is easier to read


 Code is easier to test
 Easily find things later
 Programming Errors are Easy to Detect
 Improves Manageability
 Allows Re-Use of Codes
Array in C++

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

int arr[5] = {1, 2, 3, 4, 5};


2. Initialize Array with Values and
without Size in C++
 We have initialized the array with values but we have not declared the length
of the array, therefore, the length of an array is equal to the number of
elements inside curly braces.

int arr[] = {1, 2, 3, 4, 5};


Initialize Array after Declaration (Using
Loops)
 We have initialized the array using a loop after declaring the array. This
method is generally used when we want to take input from the user or we
cant to assign elements one by one to each index of the array. We can modify
the loop conditions or change the initialization values according to
requirements.

for (int i = 0; i < N; i++) {


arr[i] = value;
}
4. Initialize an array partially in C++

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

int partial Array[5] = {1, 2};


5. Initialize the array with zero in C++

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

int zero_array[5] = {0};


Array Declaration in C++

 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];

 // Inserting elements in an array


 arr[0] = 10;
 arr[1] = 20;
 arr[2] = 30;
Continued

 // Accessing and printing elements of the array


 cout << "arr[0]: " << arr[0] << endl;
 cout << "arr[1]: " << arr[1] << endl;
 cout << "arr[2]: " << arr[2] << endl;

 return 0;
 }
//C++ Program to Illustrate How to
Traverse an Array
 #include <iostream>
 using namespace std;

 int main()
 {

 // Initialize the array


 int table_of_two[10]
 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
// Traverse the array using for loop

 for (int i = 0; i < 10; i++) {


 // Print the array elements using indexing
 cout << table_of_two[i] << " ";
 }

 return 0;
 }

You might also like