Alternating Pattern Using For, While, and Do..While

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3430 Views - Last Post: 12 March 2010 - 09:24 AM Rate Topic: -----

#1 wiineked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-September 09

Alternating Pattern Using For, While, and Do..While

Posted 11 March 2010 - 04:07 PM

I am trying to write a program that creates this pattern using a for loop, while loop, and do...while loop. Each loop creates its own pattern so the output should look like:
      For Loop:
      
      *#*
      *#*#
      *#*#*

      While Loop:

      *#*
      *#*#
      *#*#*

      Do...While Loop:

      *#*
      *#*#
      *#*#*



This is what I have on the for loop. If I get it I think I can probably get the other loops.
#include<iostream>

using namespace std;

int main()
{
	int i, j;

	cout<<"For loop:"<<endl<<endl;

	for(i=1; i<=3; i++){
		for(j=i+2; j%2!=0; j++){
			cout<<"*";
		}
		for(j=i+1; j%2==0; j++){
			cout<<"#";
		}
		cout<<endl;
	}
		cout<<endl;
}


Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Alternating Pattern Using For, While, and Do..While

#2 wiineked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-September 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 11 March 2010 - 06:22 PM

No help? I'm still messing with it, but I can't get the logic right. It's driving me crazy. I'll take hints or anything.
Was This Post Helpful? 0
  • +
  • -

#3 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 01:00 AM

Hi

Sorry if no one has replied to you.

Your output is an asterisk (*) and a hash (#) for each line then another * and so on.

First up, your using a nested loop and are probably getting:
*##*##*##

instead of *#*#

have you considered this:
for(i=0; i<3; i++)
{
    // using modulus I can alternate each character
    switch (i % 2) // produces 0 1 0 1 for each value of i
        case 0:
            cout<<"*";
        case 1:
            cout<<"#";
}


Was This Post Helpful? 0
  • +
  • -

#4 wiineked  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-September 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:22 AM

Thanks, but I have to use nested loops. I think it's impossible.
Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:32 AM

Far too complicated. Use the ternary operator (?:).

This post has been edited by PlasticineGuy: 12 March 2010 - 06:33 AM

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:35 AM

It is very possible. :P

The outer loop is exactly the same as the one you posted originally, i.e.
for( i = 0; i < 3; i++ ) {


The inner loop can work various ways -- for instance the way gregoryH posted (of course, using j instead of i since i controls the outer loop), except for his control expression. Take this
for( j = 0; j < ??; j++ )

and replace the ?? with an expression dependent on i that will give you a row length that varies as the row number changes.

You can figure out the rest.

This post has been edited by r.stiltskin: 12 March 2010 - 06:39 AM

Was This Post Helpful? 1
  • +
  • -

#7 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:36 AM

Here is one way of doing it.

#include <iostream>
using namespace std;
//
int main()
{
    for(int r = 0; r < 3; r++)
    {
            for(int c = 0; c < 3+r ; c++)
            {
                    if(c != 1 && c != 3)
                        cout<<"*";
                    else 
                        cout<<"#";
            }
            cout<<std::endl;
    } 
std::cin.get();
return 0;
}



When using loops and nested loops to do patterns think of the outer loop as its row and the inner loop as its columns. You should be able to figure out the rest.

This post has been edited by seeP+: 12 March 2010 - 06:55 AM

Was This Post Helpful? 1
  • +
  • -

#8 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:38 AM

............................................________
....................................,.-...................``~.,
.............................,.-...................................-.,
.........................,/...............................................:,
.....................,?......................................................\,
.................../...........................................................,}
................./......................................................,:`^`..}
.............../...................................................,:........./
..............?.....__.........................................:`.........../
............./__.(.....~-,_..............................,:`........../
.........../(_....~,_........~,_....................,:`........_/
..........{.._$;_......=,_.......-,_.......,.-~-,},.~;/....}
...........((.....*~_.......=-._......;,,./`..../............../
...,,,___.\`~,......~.,....................`.....}............../
............(....`=-,,.......`........................(......;_,,-
............/.`~,......`-...............................\....../\
.............\`~.*-,.....................................|,./.....\,__
,,_..........}.>-._\...................................|..............`=~-,
.....`=~-,_\_......`\,.................................\
...................`=~-,,.\,...............................\
................................`:,,...........................`\..............__
.....................................`=-,...................,%`>--==``
........................................_\..........._,-%.......`\
...................................,<`.._|_,-&``................`\

Don't write code for him.
Was This Post Helpful? 0
  • +
  • -

#9 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:41 AM

This post was up yesterday at 3pm. He has shown good effort. And please, if your not going to complain about others don't complain at all.

This post has been edited by seeP+: 12 March 2010 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:42 AM

View PostseeP+, on 12 March 2010 - 08:36 AM, said:

Here is one way of doing it.

You should be able to figure out the rest.


What is there to figure out after you already wrote it for him?
Was This Post Helpful? 0
  • +
  • -

#11 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:43 AM

His other loops.
Was This Post Helpful? 0
  • +
  • -

#12 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:44 AM

It's exactly the same code in a different syntax.
Was This Post Helpful? 0
  • +
  • -

#13 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:48 AM

That he will have to figure out.
Was This Post Helpful? 0
  • +
  • -

#14 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 06:49 AM

Posted Image
Was This Post Helpful? 0
  • +
  • -

#15 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Alternating Pattern Using For, While, and Do..While

Posted 12 March 2010 - 07:38 AM

I see you that you love pictures.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2