Jump to content

While, for, do Loop

Patrick Banzon

hi im finding trouble understanding while, for, do, loops in c++ my teacher asked us to create a pyramid out of stars i can just search the codes and paste it but i want to get the concept of it i searched internet for hours but i still can't understand it can someone make me a 2x2 box made out of * and how do you make spaces like these    image.png.9d17fb06afe68e9fe3ca8f2d7e926584.png  i understand the concept of matrix but i really cant apply it hope someone can explain it

                                                                                                                                                                     

Link to comment
Share on other sites

Link to post
Share on other sites

i am sure you can just use a print function to just place the appropriate spaces and stars

also try not to look these things up, as often, if youre truly stuck then for sure, 

but coding is using what you have to do neat stuff, 

you can easily do the pyramid with a simple loop and some interesting display functions 

also you might want to look into the New Line function it might be \n (i am more known about c, not as much c++)

PC: Alienware 15 R3  Cpu: 7700hq  GPu : 1070 OC   Display: 1080p IPS Gsync panel 60hz  Storage: 970 evo 250 gb / 970 evo plus 500gb

Audio: Sennheiser HD 6xx  DAC: Schiit Modi 3E Amp: Schiit Magni Heresy

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Patrick Banzon said:

Hi, I'm having trouble understanding 'while, for, do, loops'. My c++ teacher asked us to create a pyramid out of stars. I can just search for the code, but I want to understand the concept. I've searched the internet for hours, but I still find it difficult to understand.                                                                                                                                                                    

Hello, programing isn't actually about programming, its about solving problems programmatically.

 

1. Draw a picture to help you figure out how many 'stars' you need for your pyramid.

Spoiler

image.png.09dcfad25280266556c6c468f46d9e7e.png

2. Break it down.

Spoiler

line 0: ---*---

line 1: --***--

line 2: -*****-

line 3: *******

------------------------

line 0: 3 -, 1 *, 3 -

line 1: 2 -, 3 *, 2-

line 2: 1 -, 5 *, 1 -

line 4: 0 -, 7 *, 0 -

 

Do you see a pattern yet?

 

As the line count increments by 1, your spaces decrement by 1, and your stars increment by 2.

 

3. Draft some sudo code.

Spoiler

while (line < 5)

{

   print(spaces) print(stars) print(spaces)

   space - 1

   star + 3

   line++

}

4. open your IDE and type some stuff.

Spoiler

#include <iostream>
using namespace std;

void print_star(int number); //this functions prints 'n' stars
void print_space(int number); //this functions prints 'n' spaces
void new_line(void); //this functions jumps to a new line

int main()
{
    int pyramid_height = 4;              //the height you want your pyramid to be
    int stars = 1;                       //initial condition
    int spaces = pyramid_height - stars; //math's

    //line = 0. if line < pyramid_height. line = line + 1
    for(int line = 0; line < pyramid_height; line = line + 1)
    {
        print_space(spaces); //print 'n' spaces
        print_star(stars);   //print 'n' stars
        print_space(spaces); //print 'n' spaces

        /* our rule */
        spaces = spaces - 1;
        stars = stars + 2;
        new_line();         //new line
    }

    return 0;
}

/* this functions jumps to a new line */
void new_line(void)
{
    cout << "\n"; //CR
}

/* this functions prints 'n' stars */
void print_star(int number)
{
    int print_counter = 0;
    while(print_counter < number)
    {
        cout << '*';
        print_counter = print_counter + 1;
    }
}

/* this functions prints 'n' spaces */
void print_space(int number)
{
    int print_counter = 0;
    while(print_counter < number)
    {
        cout << ' ';
        print_counter = print_counter + 1;
    }
}

 

5. Debug and goto step 4

Spoiler

image.png.d8871da4c4da1b76a388f4c991f10cb2.png

Hope this has helped, good luck.

Link to comment
Share on other sites

Link to post
Share on other sites

Better? code.

Spoiler

#include <iostream>
using namespace std;

int main()
{
    int pyramid_height = 15; //change me
    /* vars */
    int j = 0, i = 0, stars = 1, spaces = pyramid_height - stars;
    /* main 4 loop */
    for(i = 0; i < pyramid_height; i++)
    {
        for(j = 0; j < spaces; j++)
            cout << " ";
        for(j = 0; j < stars; j++)
            cout << "*";
        /* rule */
        spaces--;
        stars+=2;
        cout << "\n";
    }
    return 0;
}

 

It dose the same thing.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, KeyboardCowboy said:

<snip>

Simpler implementation of that logic:

 

#include <iostream>
#include <string>

using namespace std;

int 
main()
{
	const auto numLines = 10;
	for (int i = 0; i < numLines; ++i)
	{
		cout << string(numLines	- i - 1, ' ') << string(i * 2 + 1 , '*') << '\n';	
	}

	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, Patrick Banzon said:

hi im finding trouble understanding while, for, do, loops in c++

I'm going to answer this portion.

 

What a loop does in general:

Loops iterate over a block of code until some condition is met to either keep doing the loop or exit the loop. In C++, the blocks of code are enclosed with curly braces { }. For while and for loops you can omit the curly braces and next line of code is what's repeated, though I discourage this practice.

 

While-loop:

A while loop takes a condition to continue doing the loop. As long as this condition is met, the loop will continue. This is useful if you cannot determine the number of times to loop through the code. For example, if you have a program that asks the user to input something and you're looking for a specific input to exit, this is where you can use a while loop.

 

For-loop:

A for loop takes three arguments separated by semi-colons

  • Initialization of parameters, separated by commas
  • Continue condition
  • One or more operations to do at the end of the loop, separated by commas

Similar to a while-loop, as long as the continue condition is met, the loop will continue. This is useful if you can determine the number of times to loop through the code. An example is you need to iterate through an array, you can know the size of the array so you can figure out how many times to run the loop.

 

Technically for-loops aren't really necessary since a while-loop can achieve the same functionality, but for-loops make it easier to build said functionality.

 

Do-While-Loop

Basically the same as a while loop, but the condition is evaluated at the end of the loop. Which means the loop is done at least once.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×