Jump to content

Alright boys, need some C++ help


Gr4vitas

Recommended Posts

So essentially what I'm trying to do is output this pattern on the screen.

 

+

++

+++

++++

+++++

++++++

+++++++

++++++++

+++++++++

++++++++++

 

++++++++++

+++++++++

++++++++

+++++++

++++++

+++++

++++

+++

++

+

 

 

I'm having the hardest time understanding the logic behind getting this to output, all I've come up with is this.

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

using namespace std;


const char space = '_';
const char draw = 'X';

int main()
{

int col, row, print;

print = 0;

for(row = 0; row<10; row++)
{
	for(col=0; col<10; col++)
	{
		if(print == col)
		{	
			cout << draw;
		}
		else 
		{
			cout << space;
		}
	}
	print++;
	cout << endl;
}





getchar();
}

 

Obviously this doesn't accomplish what I need to, I just cant figure it out and this is all I really come up with.

 

anyway if anyone has a method for this that'd be really cool.

 

by the way the above code outputs:

 

X_________

_X________

__X_______

___X______

____X_____

_____X____

______X___

_______X__

________X_

_________X

Edited by Gr4vitas

Share this post


Link to post
Share on other sites

Try this out.

const char space = ' ';

int main()
{

int col, row, print;
char str[10];

print = 0;

for(row = 0; row<10; row++)
{
	strcpy( str, space );
	for(col=0; col<10; col++)
	{
		if(print <= col)
		{	
			strcat( str, draw );
		}
		else
		{
			strcat( str, space );
		}
	}
	print++;
	cout << str << endl;
}





getchar();
}

 

This should get you through the first half of the pattern, and I'll let you figure out the second half.

Share this post


Link to post
Share on other sites

Try this out.

const char space = ' ';

int main()
{

int col, row, print;
char str[10];

print = 0;

for(row = 0; row<10; row++)
{
	strcpy( str, space );
	for(col=0; col<10; col++)
	{
		if(print <= col)
		{	
			strcat( str, draw );
		}
		else
		{
			strcat( str, space );
		}
	}
	print++;
	cout << str << endl;
}





getchar();
}

 

This should get you through the first half of the pattern, and I'll let you figure out the second half.

 

Thanks man! Really helped a lot! Just read your sig thanks for the quote haha!

 

EDIT:

 

It would appear that it won't build.

 

strcat cannot convert parameter 2 from 'char' to 'const char *'

Edited by Gr4vitas

Share this post


Link to post
Share on other sites

Now how would I go about displaying that in reverse order?


cout << endl;

for(i=11; i>0; i--){
  for(j=11; j>=i; j--) cout << "+";
  cout << endl;
}

 

that way

 

that should work I think.

Share this post


Link to post
Share on other sites

public class test 
{
   public static void main(String[]args)
   {
   for(int i=1; i<11; i++)
   {
  		for(int j=1; j<=i; j++)
  		{
  			System.out.print("+");
  		}
  		System.out.println();
  	}
  	System.out.println();
  	for(int i=11; i>0; i--)
  	{
		for(int j=1; j<i; j++)
		{
			System.out.print("+");
  			}
   		System.out.println();
	}
}
}

in java

 

EDIT: the codebox cut off the last nested for statement

Edited by RimX

Share this post


Link to post
Share on other sites

here its not pretty but it works

 

#include <iostream>

#include <conio.h>

 

int main()

{

 

int print, temp;

 

for(print = 0 ; print<10; print++)

{

 

if(print <= 10)

{

temp = print;

while(temp > -1)

{

printf("+");

temp=temp-1;

}

printf("\n");

}

}

printf("\n");

print = 9;

while(print > -1)

{

 

if(print >= -1)

{

temp = print;

while(temp > -1)

{

printf("+");

temp=temp-1;

}

printf("\n");

print--;

}

}

 

 

system("PAUSE");

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...