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.

Dani AI

Generated

You are on the right track with a two-level nested loop, . The key is: both loops must step by 2 and the inner loop must count down from the current row start to 0. already hinted at the -2 step; apply it to both loops and you will avoid hard-coding strings or printing extra numbers.

#include <iostream>
using namespace std;

int main() {
    int n = 8;                 // or read from input
    if (n % 2 != 0) --n;       // ensure n is even

    for (int start = n; start >= 0; start -= 2) {      // rows
        for (int v = start; v >= 0; v -= 2) {          // values in a row
            cout << v;
            if (v > 0) cout << ' ';                    // no trailing space
        }
        cout << '\n';
    }
}

How it maps to your goal:

  • Outer loop picks the first number of each row: 8, 6, 4, 2, 0.
  • Inner loop prints that row, counting down by 2 until it reaches 0.
  • Using v -= 2 ensures the sequence is 8 6 4 2 0 (not by ones).
  • The small if avoids an extra space at line end.

Troubleshooting tips:

  • If you see ascending numbers, you likely started j at 1 and did ++j. Start at the row’s current max and do -= 2.
  • If you see repeated full lines (your attempt #2), you are looping an extra time around the sequence generator. Keep only one inner sequence loop per row.
  • To generalize, read any n and normalize it to the nearest even with if (n % 2) --n;. This avoids hard coding while keeping the same pattern.

Recommended Answers

All 2 Replies

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.