4

Hi I read the guidelines about homework questions and it says to clearly state that it is homework. This is homework, I have spent the last 45 minutes trying over and over again. I've hit a wall and need help.

My assignment was to take this code that came from a double For loop and convert it into a while loop nested into a for loop. I have successfully completed that. However, the 3rd part is to take that code and make the outer for loop into a do while loop. The output needs to increment a "#" each line like so if the input was "4"

#
##
###
####

Below is my code that I wrote that I need to make the outer for loop into a do while loop:

int main()
{
    int side;

    cout << "Enter a number: ";
    cin >> side;

    for (int i = 0; i < side; i++)
    {
        int j = i;
        while(j >= 0)
        {
            cout << "#";
            j--;
        }
        cout << "\n";
    }
}

This is my attempt so far:

int main()
{
    int side;
    int i;

    cout << "Enter a number: ";
    cin >> side;
    int j=side;
    do
    {
        while(j >= 0)
        {
            cout << "#";
            j--;
        }
        cout << "\n";
        i++;
    }
    while(j >= side);
}

My teacher said as long as the code is explained and I understand how it works that it's okay. Any help would be much appreciated. Thanks.

3
  • 3
    What is the purpose of i++; in your loop? Commented Oct 13, 2016 at 9:45
  • 1
    I think youare the first person i see trying to solve his homework before posting... wow. Ho, and why isn't the do-while loop using i in its condition? Commented Oct 13, 2016 at 9:47
  • Don't vandalise your post. Commented Oct 13, 2016 at 10:28

4 Answers 4

1

The first mistake you made is this:

int i; //not initialized!
/*...*/
i++;

and you didn't even use it in your do-while condition.

So while(j >= side); > while (i >= side);

Actually, that's not true, either. Since side is the input, you want i to check if it's smaller not greater then the input. So it's while (i < side);

Another thing is int j=side;, when you decrement j it will never reset, so you must set this into your do-while loop and also initialize it with i rather than side....

Anyway, here's the full code:

#include <iostream>
using namespace std;

int main()
{
    int side;
    int i = 0;

    cout << "Enter a number: ";
    cin >> side;
    do
    {
        int j = i;
        while (j >= 0)
        {
            cout << "#";
            j--;
        }
        cout << "\n";
        i++;
    } while (i < side);

    return 0;
}

example output:

Enter a number: 10
#
##
###
####
#####
######
#######
########
#########
##########
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you soo much! I think I was just having serious brain fog as its 3am here :) But after tracing through the code it actually makes sense! Now I just have to error handle and make it take in negatives in the morning :) Is it okay if I message you tmrw if I run into problems making it opperate the same with negatives
Sure, why not. Good luck !
one more thing, do I need to use "return 0;" when programming. My code runs just fine with out it. And @treycos does not use it either
Since int main() is a function that returns an int, it's best practise to actually return 0 (no errors accoured). If you leave it out, the compiler adds it for you. But in my oppinion, it's hurts no-one to write a little extra.
Ohh, ok I was confused because my teacher has it in some problems and some he leaves it out...I had no idea that the compiler just auto adds it... Thanks again Danny for all the help
1

I suggest

int main()
{
    int side;
    int i = 0;
    cout << "Enter a number: ";
    cin >> side;

    do
    {
        int j = i;
        while(j >= 0)
        {
           cout << "#";
           j--;
        }
        cout << "\n";
        i++;
    }while(i < side)
}

A for loop usually consists in an initialization (i=0), a stop condition (i < side) and an increment (i++); why would you not use i anymore?

Comments

0
  • while(j >= side); should be while(i <= side);
  • j should be initialized in each iteration of the outer loop (j = i;)
  • int j=side; is unnecessary.

An friendly suggestion - name your variables and functions descriptively - row and column are much better than i and j.

Comments

0

They answer above solves your problem but let me show you a shorter way to do things:

int main()
{
    int side;

    std::cout << "Enter a number: ";
    std::cin >> side;

    int row = 1;
    do
    {
        int col = 0;
        while (col++ != row)
        {
            std::cout << "#";
        }
        std::cout << "\n";
    } while (row++ != side); //This way, the condition is checked (row != side), and then row is incremented
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.