-2

I need to print this table

2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9

I have written this code for the following result

#include <iostream>
using namespace std;

int main(){
    const int N = 9; 
    for(int i = 0; i <= N; i += 3){   
        for (int j = 0; j <= i; j +=3) {
            cout << i << " ";  
        }       
        cout << endl; 
    }
    cout << "\n";
    for(int i = 2; i <= N; i += 2){    
        for (int j = i; j > 0; j -= 2) {
            cout << j  << " ";    
        }         
        cout << endl; 
    }
    return 0;
}

My Result:

0
3 3
6 6 6
9 9 9 9

2
4 2
6 4 2
8 6 4 2

Required Result:

2
0
4 2
3 3
6 4 2
6 6 6
8 6 4 2
9 9 9 9

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1) change your logic to create the numbers such that for both you have the outer loop index correspond to the line number 2) combine both into one – 463035818_is_not_an_ai Oct 09 '18 at 11:32
  • I asked how do I combine them and your answer is , combine them ! Wow – Trevor Philip Oct 09 '18 at 11:43
  • read the comment again more carefully.... – 463035818_is_not_an_ai Oct 09 '18 at 11:43
  • I suggest to create sub-functions: one to create multiple of 3, on for decreasing even numbers. then write both loop to have i = 0 -> 4. then merge loop. – Jarod42 Oct 09 '18 at 11:58
  • `#include using namespace std; int main(){ const int N = 9; for(int i = 2; i <= N; i += 2){ for (int j = i; j > 0; j -= 2) { cout << j << " "; } cout << endl; for(int j = 0; j <= i; j +=3) { cout << j << " "; } cout << endl; } return 0; }` – Trevor Philip Oct 09 '18 at 12:20

3 Answers3

1
#include <iostream>
using namespace std;

int main(){
    const auto n = 4;
    auto count = 0;
    for (auto i = 2; i <= n * 2; i += 2)
    {
        for (auto j = i; j > 0; j -= 2)
            std::cout << j << " ";
        std::cout << std::endl;
        for (auto j = 0; j < (i == 2 ? i : i + 2); j += 3)
            std::cout << count * 3 << " ";
        ++count;
        std::cout << std::endl;
    }


  return 0;
}

Edit: Corrected...

A little near to the answer
2
0 ==> true
4 2
0 3 ==> should be 3 3
6 4 2
0 3 6 ==> should be 6 6 6
8 6 4 2
0 3 6 ==> should be 9 9 9

Ruks
  • 3,886
  • 1
  • 10
  • 22
1

Here is another approach for case when you're providing number of repeats (like in @Ruks answer):

void printSequence(unsigned int repeats)
{
    int n = 2;
    for(int i = 1; i < repeats; i++)
    {
        n+=2*i;
    }
    //n - number of all numbers in sequence for given number of repeats

    int step = 0;
    int numsPerRow = 1;

    for(int i = 0; i < n; i+=step)
    {
        for(int j = numsPerRow; j > 0; j--)
        {
            std::cout << 2*j << " ";
        }
        std::cout << std::endl;
        for(int j = 0; j < numsPerRow; j++)
        {
            std::cout << step+numsPerRow-1 << " ";
        }
        std::cout << std::endl;

        step+=2;
        numsPerRow++;
    }
}
Rhathin
  • 1,176
  • 2
  • 14
  • 20
0

Use this:

void print_sequence(unsigned long long const repeat = 4, unsigned long long const i = 0)
{
    for (auto j = (i + 1ull) * 2ull; j > 2ull; j -= 2ull)
        std::cout << j << " ";
    std::cout << (repeat > 0ull && repeat < -1ull ? "2\n" : "");
    for (auto j = 0ull; j < i; j++)
        std::cout << i * 3ull << " ";
    std::cout << (repeat > 0ull && repeat < -1ull ? std::to_string(i * 3ull) + "\n" : "");
    if (repeat < -1ull && i + 1ull < repeat)
        print_sequence(repeat, i + 1ull);
}

Edit: The shortest and strongest method I can think of...

And call it like this:

print_sequence();

If you don't want 4 times:

print_sequence(10)

It will repeat it as many times you want...

Kind regards,

Ruks.

Ruks
  • 3,886
  • 1
  • 10
  • 22
  • I am not at that level of C++ . We are just going throught for loops . And this problems needs to be solved with simple for loops . – Trevor Philip Oct 09 '18 at 12:25
  • @TrevorPhilip Which part of it is **"that level of C++"**? If you want, you can remove the if statements inside the for-loop, that won't make a big difference (would add extra spaces at the end of each line)... – Ruks Oct 09 '18 at 12:31
  • You can see my answer . That's my level of understanding C++ – Trevor Philip Oct 09 '18 at 12:33
  • I don't think of it more than simple arithmetic and for-loops always look the same in syntax, nothing new there. Just try reading it carefully and tell me where you stop. – Ruks Oct 09 '18 at 12:36
  • So can you help my correcting my answer rather than understanding yours ? Because I lack of time , and I am near to the answer . – Trevor Philip Oct 09 '18 at 12:38