Informatics - Deconstructing & Constructing loop functions (week 1)
Hello everyone! Welcome to my first blog post. I will talk about coding, some tricks and anything about informatics from my informatics class at my school. In this first blog post I will share about what I'm doing in my informatics class which is deconstructing and constructing loop function. At the beginning of the class I was given a code of loop functions. Then I assigned to make a triangle shape by using loop.
Here is the code that was given to me:
(1) #include <iostream>
using namespace std;
(2) int main (){
(3) for (int i = 1; i <= 5; i++){
(4) for (int j = 1; j <= i; j++){
(5) cout << "*";
(6) }
(7) cout << std :: endl;
(8) }
(9) return 0;
(10) }
The first step I took is I deconstruct the code first to know how it actually works.
Line 1: includes the input/output stream library, providing access to std::cout for output.
Explanation
When i = 1:
- Inner loop runs from j = 1 to j = 1 (1 iteration).
- Output: 1 asterisk (*) is printed, followed by a newline.
When i =2:
- Inner loop runs from j = 1 to j = 2 (2 iterations).
- Output: 2 asterisks (**) are printed, followed by a newline.
And so on until it prints 5 asteriks. This output forms a right-angled triangle where the number of asterisks increases by 1 in each subsequent row. The inner loop prints the asterisks, and the outer loop controls how many rows are printed.
As I stated in the beginning, I assigned to make a triangle shape by using loop function. My final output display should make a triangle by using a 'A' sign. in the first line there will be an 'A' in the middle and it will increase until shaped a triangle.
So, my output should be display as the picture above. Which is a shape of a triangle.
Stay tuned for the next blog when I start to construct the code and making the triangle shape!
Komentar
Posting Komentar