#Can someone help me on how to write code for this problem

107 messages · Page 1 of 1 (latest)

barren pondBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sly trout
#

what have you tried so far?

wind karma
#
#include <iostream>

using namespace std;

int main()
{
    int num, i, j;
    
    cout << "Enter a number:";
    cin >> num ;
    
    for(i = 0; i < num; i++)
    {
        for(j = 0; j < num; j++)
        {
            cout << "*";
        }
    }
    return 0;
}```
#

i only have this so far

#

ive been thinking of how to make it go down but havent got a clue how to

vapid yewBOT
#
Program Output
Enter a number:*******************************************************************************************************************************************************************************************************************************************
sly trout
#

🤔 don't remember how to input things anymore

wind karma
#

there is input

#

idk why the comiler does that

#

compiler*

vapid yewBOT
#

Hello! I can compile code for you. To compile code, first post a code block containing code, right click the message, go to the Apps dropdown, and select the Compile option!

If you are unfamiliar with Markdown, codeblocks can be created by formatting your message as the following.
```
<code>
```

sly trout
#

anyhow nvm for me trying to get the bot to print stuff

#

where on your code you getting stuck?

wind karma
#

one sec

#

idk how to make it go down and then print out the correct number of asteriks

#

actually wait i think i know how to make it go down correctly but the number of asteriks still i dont know how to get it correctly

sly trout
#

well how do you think you could do it?

wind karma
#

to make it go down i can just put the code outside of the for loop for the asterisks

#

and for the asterisks i think i can make a formula so it prints out correctly

#

but that part im stuck

#

the asterisks part

sly trout
#

the easiest way imho is to just split it in 2 parts, the ascending and descending part

wind karma
#

okay

#

but how do i get the ascending par

#

part

sly trout
#

I mean you do have that already:

    for(i = 0; i < num; i++)
    {
        for(j = 0; j < num; j++)
        {
            cout << "*";
        }
    }
wind karma
#

i get this output

sly trout
#

well your code is close

#

a minor modification can get you this:

*
**
***
****
#
for(j = 0; j < num; j++)
#

that loop condition isn't very logical

wind karma
#

what do i change

#

its the middle bit right

#

the condition

sly trout
#

yeah

wind karma
#

but what do I change about it

sly trout
#

well spoiling it would be spoonfeeding :D
what do you want that loop logically to do?

wind karma
#

to

#

print out 1 and then 2 and then 3 and so on until num

sly trout
#

yea

#

where does it give you a number that has that property

#

that starts out as 0, then 1 then 2 etc

wind karma
#

j?

sly trout
#

well yes but no :)

#

i ;)

wind karma
#

ohh

#

is the J loop useless?

sly trout
#

no

#

very important actually

#
    for (i = 1; i <= num; i++)
    {
        for (j = 0; j < i; j++)
        {
            cout << "*";
        }
        std::cout << "\n";
    }

take a look at this slightly edited code

wind karma
#

mhmm

#

okay so if num is 3

#

it goes in

#

first for loop i = 1

#

second for loop j = 0, i = 1

#

so it only prints once

#

then

#

i = 2

#

again second for loop j = 0 i = 2 so it prints twice

#

ohhh

sly trout
#

yup :)

wind karma
#

okay thank you

#

you're a good teacher

#

okay so for the descending part

#

imma try

#

wait

sly trout
#

you can do something very similar to the ascending part

#

but then in reverse

wind karma
#
for(i = 1; i <= num; i++)
    {
        for(j = 0; j < i; j++)
        {
            cout << "*";
        }
        cout << "\n";
        for(z = 0; z < num - i; z++)
        {
            cout << "*";
        }
        cout << "\n";
    }```
#

i tried this but got a very confusing output

sly trout
#

ah yeah, that descending loop would need to be further out

#
    for (i = 1; i <= num; i++)
    {
        for (j = 0; j < i; j++)
        {
            cout << "*";
        }
        std::cout << "\n";
    }
    <-- same code but now descending
wind karma
#

Hm

#

I dont get it

#

Oh can i make another for loop using i?

sly trout
#

yeah after the first loop is done

#
    for (i = 1; i <= num; i++)
    {
        for (j = 0; j < i; j++)
        {
            cout << "*";
        }
        std::cout << "\n";
    }

    for (i = 1; i <= num; i++) <-- now this loop needs to change
    {
        for (j = 0; j < i; j++)
        {
            cout << "*";
        }
        std::cout << "\n";
    }
#

kinda like this

wind karma
#

Oh so theres another seperate for loop?

#
    for (i = 1; i <= num; i++)
    {
        for (j = 0; j < i; j++)
        {
            cout << "*";
        }
        std::cout << "\n";
    }

    for (i = 1; i <= num - i; i++) 
    {
        for (j = 0; j < i; j++)
        {
            cout << "*";
        }
        std::cout << "\n";
    }
#

Like that?

#

Im on mobile cause im on the move

sly trout
#

yeah this be the easiest way

wind karma
#

Oh

sly trout
#

you have some duplicate code now ands stuff but this is the simplest way

#

that 2nd loop is most likely wrong still

wind karma
#

Oh okay

#

I will have to change it later but thank you

sly trout
#

for (i = 1; i <= num - i; i++)
most likely want to loop from num - 1 to 0

wind karma
#

Oh okay

#

Thanks for the teaching!!

barren pondBOT
#

@wind karma Has your question been resolved? If so, run !solved :)

raven terrace
#

!solved

barren pondBOT
raven terrace
#

Bruh nvm

wind karma
#

!solved

barren pondBOT
#

Thank you and let us know if you have any more questions!

wind karma
#

wait can i unsolve

#

!unsolve

#

!solved

barren pondBOT
#

Thank you and let us know if you have any more questions!

wind karma
#

!unsolve

#

wuu

#

okay

#

!solved