i need to make a program that would display the figure as shown below. using a DO-WHILE structure and a method to implement the task.
a C++ program.

output:

0
0 2
0 2 4
0 2 4 6
0 2 4 6 8
0 2 4 6 8 10

this is my draft:

#include <iostream>


using namespace std;

int main ()
{
int x=0;
int y=10;

do{
   (x=0, y<0);
    cout <<x<<endl;


}while
    (y=x+2, y>=0);
    cout<<y<<endl;

return 0;
}

is not working :(

Recommended Answers

All 11 Replies

You should first (before writing code) think what would the algorithm look like.

I think it should look like this:

while i've not printed 6 lines{
      print next line
}

print next line() {
      required number of numbers to be printed = ???
      while I've not printed required number of numbers on this line {
           print the number (=index*2)
           print a space
      }
      print new line.
}

I'm using C++ do-while. sorry, I' a newbie :(
your sample is Turbo or sharp C.

It's not even that, it's a pseudocode algorithm. It's up to you to translate it to C++ code that behaves similarly and meets your objective. No one here is just going to hand you the solution on a silver platter. Also note that most C code is also perfectly valid as C++ code as well.

As long as I'm posting, I don't like the way you've written your condition in your loop control. It's generally not advisable to have an assignment statement as part of a conditional. You should be controlling your loop with some sort of index and modifying the index appropriately inside the loop.

Given that you have already tried and in the interest of abiding by the rules of this website of not giving out full solutions I have sample code that will guide you in the right direction, you just have to add condition to increment by two every time.

#include<iostream>
#include<cstdlib>

using namespace std;
int main(){
int num = 0;
int count = ;
cout<<"Enter your number: "<<endl;

cin>> num;

  do{

//Do your output here

  for(){//This is the part where you have to do the work, it should be easier from what I have given you. 
    cout<<count<<endl;
 }

  num--;
  }while(num != 0)

return 0;
}
#include <stdio.h>
#include <iostream>
int main()
{

int i, j;

do(i = 0; i < 10;)
{

cout<<i;

do(j = i+2; j>0; )
{
cout<<j;

while
(j = j -2);
}
cout<<"\n";
i =i +2;
}

system("pause");
return 0;
}

This is the closes draft i made. something missing. error in DO part.

ok.. tnx syd. I try.

#include <stdio.h>
#include <iostream>
int main()
{

int i, j;

do(i = 0; i < 10;)
{

cout<<i;

do(j = i+2; j>0; )
{
cout<<j;

while
(j = j -2);
}
cout<<"\n";
i =i +2;
}

system("pause");
return 0;
}

This is the closes draft i made. something missing. error in DO part.

Please use [code] ... code tags ... [/code] when posting code, they make your code easier to read by adding syntax highlighting and preserving formatting.

What's wrong with this construct?:

do (condition) {   //begin do
  while(condition) //begin while
    //some code... //end while
}                  //end do

You have done this in your code... You need to re-arrange the elements of your syntax

More info on C++ control structures.

how many DO i need to make?

You'll need a pair of nested loops. One will control the number of lines output, the other will control the actual content of the line, much like displaying the contents of a 2-D array.

#include <iostream>

const int ROWS = 3, COLS = 2;

int main() {
  int myArray[ROWS][COLS] = {{1,2},{3,4},{5,6}};

  for (int i = 0; i < ROWS; ++i) {       //select row

    for (int j = 0; j < COLS; ++j) {     //select column
      std::cout << myArray[i][j] << " "; //display value
    }                                    //end column loop

    std::cout << std::endl;              //add a linefeed

  }                                      //end row loop

  std::cin.get();
  return 0;
}

more complicated for a newbie like me.

Your posts are starting to look like your fishing for free code, that's not going to happen here. Get over your "newb" complex and buck up, you're not helpless, and you're obviously not stupid. We're here to help you, not do your work for you.

This is not a complicated concept. It's a simple nested loop construct. Imagine the required output as a simple rectangular array of values (a "matrix"). The outer loop controls the current line/row and the inner loop controls the position within the current line/column. In this particular case, the number of iterations the inner loop performs is based on the "index" of the current iteration of the outer loop.

commented: Some good posts in this thread. +13
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.