0% found this document useful (0 votes)
9 views12 pages

Comp Prog Q4 Lesson 1 2

The document covers nested loops in programming, explaining their types (while, do-while, and for) and providing syntax and flowcharts for each. It also introduces derived data types in C++, specifically arrays, detailing their declaration, initialization, and usage. Sample programs demonstrate how to implement nested loops and arrays in C++.

Uploaded by

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

Comp Prog Q4 Lesson 1 2

The document covers nested loops in programming, explaining their types (while, do-while, and for) and providing syntax and flowcharts for each. It also introduces derived data types in C++, specifically arrays, detailing their declaration, initialization, and usage. Sample programs demonstrate how to implement nested loops and arrays in C++.

Uploaded by

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

NESTED LOOPS

(PATTERNS, PYRAMIDS, AND SHAPES)

Objectives:

Identify what are the different types of Nested Loops.


Differentiate and explain how Nested Loops woks in a program.
Create a C++ program using Nested Loops.

❖ NESTED LOOPS
• It is a loop inside another loop.
• A loop can be nested inside another loop.
• Nested loops are also called as “loop inside loop.”
• When you "nest" two loops, the outer loop takes control of the number of complete
repetitions of the inner loop.
• When working with nested loops, the outer loop changes only after the inner loop is
completely finished (or is interrupted.).
❖ TYPES OF NESTED LOOPS
• Nested While Loop
• Nested Do-while Loop
• Nested For Loop
Note: There can also be very variation of nested loops where a while loop can be inside a for
loop, a for loop can be inside a do-while loop and many more.

❖ NESTED WHILE LOOPS


Syntax:
while(condition1) {
while(condition2) {
// statement of inside loop
}
// statement of outer loop
}

❖ Flowchart of Nested While Loop

1
Sample program using Nested While Loop

1. Display or print Pyramid using while loop

❖ NESTED DO-WHILE LOOPS


Syntax
do{
// statements of outer loop
do{
// statement of inside loop
}while(condition);
// statement of outer loop
}while(condition);

❖ Flowchart of Nested Do-while Loop

2
Sample program using Nested Do..While Loop

1. Display or print Pyramid using do..while loop

❖ NESTED FOR LOOPS


Syntax:

for ( init; condition; increment ) {


for ( init; condition; increment ) {
statement(s);
}
statement(s);// you can put more statements.
}

❖ Flowchart of Nested While Loop

3
Sample program using Nested For Loop

1. Display or print Pyramid using for loop

All the activities in LAS 3 will be submitted through email. ([email protected])


LAS 3 - Activity 1
Directions: Read the problem carefully.

Problem: What is the output of the following (when embedded in a complete program)? Explain
how do you come up with that output.

1. 3.

2.

4
Problem: Write the output of each program. Explain the process how do you come up with that
output.

4.

5.

RECURSION is the process in which s function calls itself


directly or indirectly is called recursion and the
corresponding function is called as recursive function.

5
Problem: Write a C++ program that prints the following output using nested while loop, do-while
loop and for loop. Your program must be an interactive program.

6-8) Inverted right triangle

REFLECTION
Complete this statement:
What I have learned from this lesson
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________

REFERENCES

• Problem Solving with C++ 9th Edition by Walter Savitch


• Programiz.com
• beginnersbook.com
• https://www.w3schools.com/cpp/cpp_break.asp
• An Introduction to C++ by Michael Oliver

6
LEARNING ACTIVITY SHEET 4
Computer Programming
Grade 10 - STE

MELC:
• Design a program using the different derived data types.

Objectives:
Identify what are Derived Data Types
Differentiate and explain how Derived data types woks in a program.
Create a C++ program using the different types of derived data types.

❖ DERIVED DATA TYPES


- The data-types that are derived from the primitive or built-in datatypes are referred to
as Derived Data Types. There are four types of derived data types:

ARRAYS

DERIVED DATA
FUNCTIONS
TYPES

POINTERS

❖ ARRAYS
- In C++, an array is a variable that can store multiple values of the same type. For
example, suppose a class has 27 students, and we need to store the grades of all of
them. Instead of creating 27 separate variables, we can simply create an array:

double grade[27];

- Here, grade is an array that can hold a maximum of 27 elements of double type.
- In C++, the size and type of arrays cannot be changed after its declaration.
- Each piece of data stored in arrays is known as an element and has its own unique
identifier.
- Regardless on the length or type of array that is used in the first element is always a “0”.
Take a look below at the example.

- In the example, we have 5 blocks which can store 5 sets of data of the same type. When
we declare an array, we declare it like a variable as we have to give a data type and a
name, but we also have to declare how many elements we expect our array to hold.
7
We do this by using square brackets [] with a number inside. Based on the example, our
arrays have 5 elements [5].

Example:

❖ ARRAY DECLARATION

dataType arrayName[arraySize];

For example,
int x[6];
Here,
▪ int - type of element to be stored
▪ x - name of the array
▪ 6 - size of the array
❖ Access Elements in C++ Array
- In C++, each element in an array is associated with a number. The number is known as
an array index. We can access elements of an array by using those indices.
// syntax to access array elements
array[index];

- Consider the array x we have seen above.

Few Things to Remember:

• The array indices start with 0. Meaning x[0] is the first element stored at index 0.
• If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the
last element.

8
• Elements of an array have consecutive addresses. For example, suppose the starting
address of x[0] is 2120d. Then, the address of the next element x[1] will be 2124d, the
address of x[2] will be 2128d and so on.
• Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.
❖ ARRAY INITIALIZATION
- In C++, it's possible to initialize an array during declaration. For example,
// declare and initialize and array
int x[6] = {19, 10, 8, 17, 9, 15};

- Another method to initialize array during declaration:


// declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};

- Here, we have not mentioned the size of the array. In such cases, the compiler
automatically computes the size.

❖ ARRAY WITH EMPTY MEMBERS


- In C++, if an array has a size n, we can store up to n number of elements in the array.
However, what will happen if we store less than n number of elements.

For example,
// store only 3 elements in the array
int x[6] = {19, 10, 8};

- Here, the array x has a size of 6. However, we have initialized it with only 3 elements. In such
cases, the compiler assigns random values to the remaining places. Oftentimes, this random
value is simply 0.

❖ How to insert and print array elements?


int mark[5] = {19, 10, 8, 17, 9}

// change 4th element to 9


mark[3] = 9;

// take input from the user


// store the value at third position
cin >> mark[2];

// take input from the user


// insert at ith position

9
cin >> mark[i-1];

// print first element of the array


cout << mark[0];

// print ith element of the array


cout >> mark[i-1];

Sample program using Array

1. Program displaying Array elements

#include <iostream>
using namespace std;

int main() {

int numbers[5] = {7, 5, 6, 12, 35};

cout << "The numbers are: ";


// Printing array element
OUTPUT
The numbers are: 7 5 6 12 35
// using range based for loop
for (const int &n : numbers) { The numbers are: 7 5 6 12 35
cout << n << " ";
}
cout << "\nThe numbers are: ";
// Printing array elements

// using traditional for loop


for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}

- Here, we have used a for loop to iterate from i = 0 to i = 4 . In each iteration, we have
printed numbers[i] .

Note: In our program we used range based loop, we have used the code const int &n instead of int
n as the range declaration. However, the const int &n is more preferred because:

▪ Using int n simply copies the array elements to the variable n during each iteration. This is not
memory-efficient.
▪ &n, however, uses the memory address of the array elements to access their data without
copying them to a new variable. This is memory-efficient.
▪ We are simply printing the array elements, not modifying them. Therefore, we use const so as
not to accidentally change the values of the array.

2. Program that take Inputs from User and Store Them in an Array
#include <iostream>
using namespace std;
OUTPUT
int main() {
Enter 5 numbers:
int numbers[5];
11
cout << "Enter 5 numbers: " << endl; 12
13
// store input from user to array 14
for (int i = 0; i < 5; ++i) {
15
The numbers are: 11 12 13 14 15 10
cin >> numbers[i];
}
cout << "The numbers are: ";

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;
}
- Once again, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we took
an input from the user and stored it in numbers[i].
- Then, we used another for loop to print all the array elements.

3. Display Sum and Average of Array Elements Using for Loop


#include <iostream>
using namespace std;

int main() {

// initialize an array without specifying size


double numbers[] = {7, 5, 6, 12, 35, 27};
double sum = 0;
double count = 0;
double average;
OUTPUT
cout << "The numbers are: ";
// print array elements The numbers are: 7 5 6 12 35 27
Their Sum = 92
// use of range-based for loop
Their Average = 15.3333
for (const double &n : numbers) {
cout << n << " ";
// calculate the sum
sum += n;
// count the no. of array elements
++count;
}
// print the sum
cout << "\nTheir Sum = " << sum << endl;

// find the average


average = sum / count;

cout << "Their Average = " << average << endl;


return 0;
}

In this program:

▪ We have initialized a double array named numbers but without specifying its size. We also
declared three double variables sum, count, and average.

Here, sum =0 and count = 0.

▪ Then we used a range based for loop to print the array elements. In each iteration of the
loop, we add the current array element to sum.
▪ We also increase the value of count by 1 in each iteration, so that we can get the size of the
array by the end of the for loop.
▪ After printing all the elements, we print the sum and the average of all the numbers. The
average of the numbers is given by average = sum / count;

Note: We used a ranged for loop instead of a normal for loop. A normal for loop requires us to specify the
number of iterations, which is given by the size of the array. But a ranged for loop does not require such
specifications.
11
Direction: All your programs for this activity will be submitted through email.
(Email Add: [email protected])

LAS 4 - Activity 1
Directions: Read the questions carefully and write your answer on the space provided. Strictly no
ERASURE.

1. In the array declaration:


double score[5];

state the following:

a. The array name:

b. The base type:

c. The declared size of the array:

d. The range of values that an index for this array can have

e. One of the indexed variables (or elements) of this array

2. Identify any errors in the following array declarations.

a. int x[4] = { 8, 7, 6, 4, 3 };
b. int x[ ] = { 8, 7, 6, 4 };
c. const int SIZE = 4;
d. int x[SIZE];

3. Write some C++ code that will fill an array a with 20 values of type int read in from the
keyboard. You need not write a full program, just the code to do this, but do give the
declarations for the array and for all variables.

REFLECTION
Complete this statement:
What I have learned from this lesson
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________
_______________________________________________________________________________________________

REFERENCES

• Problem Solving with C++ 9th Edition by Walter Savitch


• Programiz.com
• https://www.w3schools.com/cpp/cpp_break.asp
• An Introduction to C++ by Michael Oliver

12

You might also like