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.

Line 2: defines the entry point of the program. "Main" is where the program is executing
Line 3: outer for loop that controls the number of rows in the triangle. It runs from i = 1 to i = 5, iterating 5 times.
Line 4: nested inner loop that controls how many asterisks are printed in each row. The number of iterations for this loop depends on the value of i. For each value of i, it prints i asterisks. 
Line 5: prints an asterisk (*) to the console without moving to the next line. it will print i asterisks in a single row, where i corresponds to the current iteration of the outer loop.
Line 7: prints a newline character (std::endl), moving the cursor to the next line.
Line 9: returns 0 from the main function, indicating that the program has executed successfully.

To run this code, I will use the online compiler which is Online GDB. Here's the output of the code:

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

Postingan populer dari blog ini

Informatics - Making a Main C++ Program to Run all the Calculator Program (week 11)

Informatics - Triangles (week 18)

Informatics - Making Minimarket Discount Program in C++ version 1 (Week 12)