Jump to content

Newb Programmer needing help in c++

QuirkyPhoenix

I'm 15 and have no idea how a for loop works. Will someone please help me. How does the loop generate a pyramid? What is this sorcery???

Link to comment
Share on other sites

Link to post
Share on other sites

I will probably get a little bit of terminology wrong here, but this is the overview. A for loop is used to repeat a bit of code a specified number of times. Every for loop can be rewritten as a while loop (at least in most of the big name languages), and it works like this. Consider the below piece of code.

int number = 0; //Declare variable number of type integer and initialize it to 0
while (number < 6) {
    printf("The value of number is %i\n", number);
    number = number + 1; //number is now equal to it's original value + 1
}

What will that code output? Well, it will first check the condition (i < 6), and if it's true, it will execute the code below it. If it's false, it will skip the body of the loop and continue to whatever code is below it. So, it's hopefully obvious to see that it will run 6 times, and print:
 

The value of number is 0
The value of number is 1
The value of number is 2
The value of number is 3
The value of number is 4
The value of number is 5

Also note that the line number = number + 1 can be compressed to the expression number++ in most languages.

 

So later, you try to do this again, but forget to add the number = number + 1. What happens? The code enters into an infinite loop, beause number will never be greater than 5. That's annoying. Also, you do it a second time with a different variable, but you forget the first line (int myVar = 0). Whoops,  compiler error, because the variable is not defined. So what if we didn't have to include either of those lines? That's where a for loop comes in. It compresses the initialization, condition, and increment into one line:

for (int number = 0; number < 6; number++) {
    printf("The value of number is %i\n", number);
}

To answer your other question, you're going to need to provide more informations. What kind of pyramid?

Link to comment
Share on other sites

Link to post
Share on other sites

The thing about a for loop is that's a contract in the programming and it should NEVER BE MESSED WITH. Let it run. It's great for if it just needs to do one set of things over and and over until it's done. in the declaration there is three things wrapped into one thing. You have for("part 1"; "part 2"; "part 3") and those parts are 1. the counter initiation. 2. the counter sentry for when it's over, and 3. the counter increase. So these are the same thing...

int numberArray[5] = {1,2,3,4,5}

int index = 0;

while(index < 5)
{
  cout << numberArray[index] << endl;
  index++;
}

for (int index = 0; index < 5; index++)
{
  cout << numberArray[index] << endl;
}

So we declare an int array of 5 indexes and call it numberArray. then we declare the index counter and drop into the while loop where we print out the number array one number per line and increase the index after we print the line. In the for loop that all is on one line for the declaration and one line for the print. 

 

For the pyramid, you'd have to do some tricky stuff for setw() or a modulus with the index.

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a pyramid of numbers... 

#include <iostream>
  
  using namespace std;

int main()
{

    int k = 0;
    int count = 0;
    int count1 = 0;

    cout<<"Enter the number of rows: ";
    int rows = 0;
    cin>>rows;
   
  for(int i=1;i<=rows;++i)
    {
        for (int space = 1; space <= rows - i; ++space)
        {
            cout << "  ";
            ++count;
        }
        while (k != 2 * i - 1)
        {
            if (count <= rows - 1)
            {
                cout << i + k << " ";
                ++count;
            }
            else
            {
                ++count1;
                cout << i + k - 2 * count1 << " ";
            }
            ++k;
        }
        count1 = count = k = 0;
        cout << "\n";
    }

    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, QuirkyPhoenix said:

I'm 15 and have no idea how a for loop works. Will someone please help me. How does the loop generate a pyramid? What is this sorcery???

You should consider reading this: http://www.learncpp.com/ and then using it as a reference when you need to. I hate to be "that guy", but for loops are one of the most basic elements of any program and they are used in most languages.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, straight_stewie said:

You should consider reading this: http://www.learncpp.com/ and then using it as a reference when you need to. I hate to be "that guy", but for loops are one of the most basic elements of any program and they are used in most languages.

I know, I'm just starting out.

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

×