Hello. I am taking a "Data Structures" class. I have to create a code that will print the following "Output":
8 6 4 2 0
6 4 2 0
4 2 0
2 0
0

I tried doing this code three different ways. Here is the first way. I did a similar project in "Java" so I knew I needed a "Nested Loop. I just "re-wrote" the code in "C++" but it does not print the correct numbers. I have to print the numbers in a way that the numbers count by "two's" and not "one's":

for(int i = 8; i >= 1; --i){
    for(int j = 1; j <= i; ++j){
        cout << j << " ";
     }
     cout << endl;
    }
return 0;

Here is the second way. This is incorrect becasue the output prints too many numbers:

for(int count = 8; count >= 1; --count){
    for(int index = 1; index <= count; ++index){
        for (int num = 8; num >= 0; num = num - 2) {
            cout << num << " ";
        }
    }
    cout << endl;
}

Here is the third way. This is incorrect becasue the teacher said this is still considered "Hard Coding" which is not allowed:

 char str[] = "86420 6420 420 20 0";  

 char *token = strtok(str, " "); 

 while (token != NULL) { 
    printf("%s\n", token); 
    token = strtok(NULL, " "); 
  }

return 0; 

My main problem is this (this program seems so simple and I think I have a general idea on how to solve this so I apologize if this question has an easy answer): How do I manipuate the numbers to print "86420" on the first row, "6420" on the second row etc...? I was told that I would need to print these numbers in a "horizontal line" then use a "two level nested loop" to manipulate how the numbers are printed but this is the part I am having trouble with. I can manipulate how the numbers print itself but I can not get the numbers to print correctly.

I tried to find examples of this on "Dani Web" and online but none of them answerd my main question. Please help and thank you in advance to anyone who can help me solve this problem.

I'll avoid supplying the answer since you must code it yourself or I'd be doing your homework.

I take it you want to decrement by 2. SO... THINK about.

--i means i-=1; return i

So to decrement by 2, change the 1 to 2.

Thank you. That seemed like a simple solution (I didn't want it to look like I was trying to get free homework answers either).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.