hi can someone help me through this nested for loop

Mar 14, 2012 at 2:26am
i need to get this output

OUTPUT:
------*
-----**
----***
---****
--*****



NOTE: the ( - ) is just an space. guys can someone help me using nested for loops c++. i really nid this for my final exam T.T
Last edited on Mar 14, 2012 at 2:30am
Mar 14, 2012 at 3:59am
Write some code, post it to show us whether you are thinking.
Then we can start to help.
Mar 14, 2012 at 4:31am
I've only just started learning C++ about a week ago, so I'm not sure if this is what you're looking for. :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int spaceCount = 6;

int main()
{
    string myArray[5][1]= {{"*"},{"**"},{"***"},{"****"},{"*****"}};

    for(int row=0; row<5; row++){
        for(int column=0; column<1; column++){
            switch(spaceCount){
                case 6: cout << "      "; break;
                case 5: cout << "     "; break;
                case 4: cout << "    "; break;
                case 3: cout << "   "; break;
                case 2: cout << "  "; break;
                default: cout << "Error";
            }
            cout << myArray[row][column];
        }
        spaceCount--;
        cout << endl;
    }
}
Last edited on Mar 14, 2012 at 4:41am
Mar 14, 2012 at 8:41am
@Polaz: you have a for loop fixed to do one iteration. Why?
Mar 14, 2012 at 10:25am
maybe you can try something like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
 
int main(int argc, char *argv[])
{
        int numberOfChars = 7;
        for (int i = 0; i < numberOfChars; i++)
        {
                for (int ii = 0; ii < numberOfChars; ii++)
                {
                        if (i < ii)
                                std::cout << "*";
                        else
                                std::cout << " ";
                }
                std::cout << std::endl;
        }
}
Last edited on Mar 14, 2012 at 10:46am
Mar 14, 2012 at 11:19am
Please don't just give him the answers to his homework. Asides from it being cheating, he doesn't learn anything from it and then he'll have to post here again with a new problem because he never learned the lesson from the first problem (programming is cumulative, after all). And if you give him the answer to that problem too, well then he won't learn something for the second time, and will have to post here again. Instead of feeding this cycle, try to just give suggestions and hints without giving away the whole answer.

Teach a man to fish.
Mar 14, 2012 at 11:28am
if he's as interested as i was he'll still learn something from this :)
and if not, well let's hope his prof knows how to test the capabilities of his students :p
anyhow this wasn't much of a deal i guess ö.ö
but thanks you for your advice, i'll consider it next time
Mar 14, 2012 at 10:30pm
@Gaminic: Well, it's a nested for loop that way and it runs five times.. it's just to print out the asterisks on each line and then continue to the next. Like I said, my excuse is that I've only started learning about a week ago. Sorry.
Mar 14, 2012 at 11:21pm
he meant this

1
2
3
4
5
6
7
8
9
        for(int column=0; column<1; column++){
            switch(spaceCount){
                case 6: cout << "      "; break;
                case 5: cout << "     "; break;
                case 4: cout << "    "; break;
                case 3: cout << "   "; break;
                case 2: cout << "  "; break;
                default: cout << "Error";
            }


that loop will only run once, besides, spaceCount is outside of the larger for loop, so it would output 6 spaces 5 times, and then the program would end.

there are other problems as well, that would make it not work, if you are beginning to program in c++ I suggest you first compile everything.
Last edited on Mar 14, 2012 at 11:23pm
Mar 14, 2012 at 11:43pm
@Zephilinox: I know what he meant. That loop is called five different times from within the other for loop. The "spaceCount--" is within the first for loop and is initialized outside of main. (Meaning all classes/functions should have access to it)

I don't understand..? It worked perfectly fine on my compiler.
I use Code::Blocks.

Try running it yourself?
Last edited on Mar 14, 2012 at 11:44pm
Mar 14, 2012 at 11:57pm
You're right, I'm sorry, I mixed up the brackets, I'm not used to seeing them split up the way you've done it, I thought the first } was ending the column loop, so space count would be outside the row loop, and outputting the array wouldn't work because it was outside the column loop (where int column is defined)
Mar 14, 2012 at 11:59pm
@Zephilinox: No problem, it was my fault. I guess I need to change my syntax to make it easier to read if I do continue my pursuit in learning to program. That's just the way I've learned how.
Last edited on Mar 15, 2012 at 12:00am
Mar 15, 2012 at 12:01am
No it isn't, there are many ways of indenting and formatting code, yours is a valid one, I'm simply not used to reading it.

I believe most Java programmers do it the way you've done it, might be wrong though.

PitDaAnimA has also used a different style, by using 8 spaces instead of 4, 4 being the standard spacing for [TAB], some people use 2.

some people also like placing the beginning { at the start, then some code, then the rest of the code on other lines, and ending with } on its own line.
Last edited on Mar 15, 2012 at 12:07am
Mar 15, 2012 at 12:05am
@Zephilinox: Ah, okay. Thanks for the info. :)
Mar 15, 2012 at 8:59am
stupid me :p i usually have also 4 spaces for <tab> but i did it on notepad editor, hence i've no ide here @ work :D

as for the placing for the {...} adapt one style (java, c++, or whatever) you're comfortable with and use it the whole time :)
Topic archived. No new replies allowed.