0% found this document useful (0 votes)
10 views18 pages

W7-Presentation-Through The Loops

The document outlines a programming task to create a program that displays an equilateral triangle based on user input for height. It details the necessary C++ includes, the structure of the main function, and the logic for using loops to format the triangle correctly. The explanation includes how to manage spaces and asterisks to visually represent the triangle shape.

Uploaded by

igcasan.jc07
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)
10 views18 pages

W7-Presentation-Through The Loops

The document outlines a programming task to create a program that displays an equilateral triangle based on user input for height. It details the necessary C++ includes, the structure of the main function, and the logic for using loops to format the triangle correctly. The explanation includes how to manage spaces and asterisks to visually represent the triangle shape.

Uploaded by

igcasan.jc07
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/ 18

Week 007

Through the Loops


Week 007: Through the Loop

Problem Solving

Write a program that will display


an equilateral triangle with the
height depending on the users
input.
Week 007: Through the Loop

Problem Solving

In the 1st line the used of #include <cstdlib>


is for the system functions.
Week 007: Through the Loop

Problem Solving

Next in 2nd line we used the #include <cstring>


for String functions like “strcmt” and “strcpy”.
Week 007: Through the Loop

Problem Solving

Next in 3rd line we used the #include <iostream>


for String input and output of C++.
Week 007: Through the Loop

Problem Solving

Next in 4th line we used the using namespace std;


to ask permission in C++ that we will use std;
Week 007: Through the Loop

Problem Solving

Next in 5th line we used the int main( ) {


To start or what we call our entry point.
Week 007: Through the Loop

Problem Solving

Now in the next line we have int.


To declare integers. The objective of this is to input
numbers.
Week 007: Through the Loop

Problem Solving

In this line we used the cout <<


to display an output string in order to inform the user
that you need the number of rows to be entered.
Week 007: Through the Loop

Problem Solving

Next line we used the cin >>


to store the user inputs in the variable of the n.
Week 007: Through the Loop

Problem Solving

In this line we used the cout << endl;


to create a new line.
Week 007: Through the Loop

Problem Solving

Since we need to display a


triangle with a height depending
in users input. The condition for
the for loop in line 10 is i less
than the user input.
Week 007: Through the Loop

Problem Solving

The first inner for loop will display


the necessary places to form a
triangle.

The condition being that current


row should be more spaces in the
beginning of the triangle (at the
peck) and gradually decreasing
as we go to the bottom of the
triangle.
Week 007: Through the Loop

Problem Solving

Therefore, we came up with the


equation n being the user input
minus i being the current row
minus 1.
i n–I–1
0 5–0–0=4
1 5–1–0=3
2 5–2–0=2
3 5–3–0=1
4 5–4–0=0
Week 007: Through the Loop

Problem Solving

The second for loop will create


the foundation f our triangle.

Take note that for every


increment or iteration the number
of * doubles depending on
current raw with an extra of one *.
Week 007: Through the Loop

Problem Solving

That is why we come up with the


formula of i being the current raw
multiplied by 2, with their product
added by 1.
i (i * 2) + 1
0 (0 * 2) + 1 = 1
1 (1 * 2) + 1 = 3
2 (2 * 2) + 1 = 5
3 (3 * 2) + 1 = 7
4 (4 * 2) + 1 =9
Week 007: Through the Loop

Problem Solving

The last line of our outer for loop


ends with an end line to signify
that we have ended the current
row.
Week 007: Through the Loop

Problem Solving

The last 3 lines will simple display


a closing remarks and terminate
the int main ( ) function.

You might also like