check my code in do-while loop.

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 7416 Views - Last Post: 26 March 2011 - 10:56 AM Rate Topic: **--- 2 Votes

#1 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

check my code in do-while loop.

Posted 24 March 2011 - 07:56 AM

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 <stdio.h>
#include <iostream>
int main()
{

int i=0;
int x=7;

do
{
     i=i+1;
cout<<i;

while(i<=7 )
{
cout<<i;


system("pause");
return 0;
}


< do i need to make a 2 DO's? and 2 WHILE? how can i start this?

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 24 March 2011 - 08:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: check my code in do-while loop.

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 08:04 AM

A DO {} WHILE() loop is actually comprised of both do and while. You will need an outer loop and an inner loop. The inner loop will produce the columns and the outer loop the rows.
Was This Post Helpful? 1
  • +
  • -

#3 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 08:20 AM

so.. how can i start this one? should i need to declare two variables 1st? let say, int x=0,int y=7. ??
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 08:41 AM

Yes you would need 2 variables to hold the counters. Your code should follow this algorithm. Follow it and try to implement it.

Written straight from the head so bare with me.
initialize integers inner and outer both to zero and maximum to 10
while outer is less than or equal to ( maximum / 2 )
	set inner to 0
	while inner is less than maximum
		print inner
		add 2 to inner
	end loop
	increment outer by 1
end loop



This post has been edited by codeprada: 24 March 2011 - 08:42 AM

Was This Post Helpful? 1
  • +
  • -

#5 EarthShaker  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 186
  • Joined: 16-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 08:52 AM

This should work. I haven't run it though, so try it and let me know.

int main()
{

int i=10;
int x=2;
int y=0;

while (x <= i)
{
    y = 0;
    while ( y <= x)
    {
        cout << y << " ";
        y += 2;
    }
    cout <<endl;
    x += 2;
}



return 0;


Was This Post Helpful? 0
  • +
  • -

#6 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:21 AM

oh tnx. by d way, i don't really know how to input inner and outer loop. LOL. do i need to make two DO and two WHILE?
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:28 AM

View Postbenchz23, on 24 March 2011 - 12:21 PM, said:

oh tnx. by d way, i don't really know how to input inner and outer loop. LOL. do i need to make two DO and two WHILE?


Oh so you don't have the basic syntax down as yet...Have a look at these sites to learn C++
http://www.learncpp.com/
http://www.cplusplus.com/doc/tutorial/
http://www.howtoforg...us-step-by-step

This is the shell of what your code should look like.

int inner, outer = 0, maximum = 10;
do
{
   inner = 0;

   do {

      //code here

   }while( inner <= maximum );

   outer = outer + 1; //or outer++;

} while( outer <= (maximum / 2) );


Was This Post Helpful? 0
  • +
  • -

#8 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:39 AM

View PostEarthShaker, on 24 March 2011 - 08:52 AM, said:

This should work. I haven't run it though, so try it and let me know.

int main()
{

int i=10;
int x=2;
int y=0;

while (x <= i)
{
    y = 0;
    while ( y <= x)
    {
        cout << y << " ";
        y += 2;
    }
    cout <<endl;
    x += 2;
}



return 0;




"its not working" theres a problem of cout<<y<<" "; and also to cout<<endl; and return 0;
Was This Post Helpful? 0
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:51 AM

did you include the proper header files at the beginning of the file? Like:
#include <iostream>
using namespace std;


You atleast need to have that, so you can do input/output to the console.

This post has been edited by vividexstance: 24 March 2011 - 09:51 AM

Was This Post Helpful? 1
  • +
  • -

#10 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:07 AM

PERFECT!!! thanks. my problem solved! woohoo! thank to you earthshaker and codeprada! i owe u one :)

oh.. wait a minute.. my case is need to solve using DO-WHILE structure but the one running is WHILE WHILE? I did not apply DO in my program. where i can insert my DO here?
Was This Post Helpful? 0
  • +
  • -

#11 EarthShaker  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 186
  • Joined: 16-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 10:43 AM

Can't you use your intelligence to add one or two do{} statements to the code i provided? if so, you better start reading your books more often.
Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 11:25 AM

Lovely explanation of a do {} while() statement here
http://mathbits.com/...ing/dowhile.htm

That's as simple as it gets
Was This Post Helpful? 0
  • +
  • -

#13 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 08:18 PM

Still Its not running. I'm spending 10hours in my computer today reading all about DO-WHILE. still i'm stuck up of this DO code here.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,435
  • Joined: 25-December 09

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:05 PM

Please post your current code along with any error/warning messages you receive when you compile the code if any.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 benchz23  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 22-March 11

Re: check my code in do-while loop.

Posted 24 March 2011 - 09:19 PM

this is my recent output same with my expected output.:
use this for DO-while structure:

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

heres my draft:

#include <<iostream.h>>
#include<<conio.h>>
#include<<stdio.h>>

using namespace std;
int main()

{

int i=10;
int x=2;
int y=0;
 
do
 {
    cout<<x <===what should i write here? DO code.. the statement? i tried many things but it doesn't work.
 }
while (x <= i)
{

    y = 0;
    while ( y <= x)

    {
        cout << y << " ";

        y += 2;
    }
    cout <<endl;
    x += 2;
}

return 0;


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2