8 Replies - 12824 Views - Last Post: 04 January 2010 - 05:13 PM Rate Topic: -----

#1 XxXnucronXxX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 08

asterisk pattern using for-loop (console app)

Post icon  Posted 03 January 2010 - 04:40 AM

hi, C# gurus.. i really need help on this problem.. this pattern gives me a head ache.. pls help me on this..

btw.. Kindly disregard the function..[size=2][color=#FF0000]

Posted Image

here's what i got so far.. pls i really need help.. thanks in advance!



using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
	class Program
	{
		static void Main(string[] args)
		{
			int x1, y1, x2, len = 4, x, y; //cont1, strpos, cont2, cont3;
			string str;

			//System.Console.WriteLine("Please input lenght:");
			//len = Convert.ToInt16(System.Console.ReadLine());

			System.Console.WriteLine("Please input x1(point 1 horizontal coord.:");
			x1 = Convert.ToInt16(System.Console.ReadLine());

			System.Console.WriteLine("Please input y1(point 1 vertical coord.:");
			y1 = Convert.ToInt16(System.Console.ReadLine());

			System.Console.WriteLine("Please input your name:");
			str = System.Console.ReadLine();
			x2 = str.Length;

			System.Console.Clear();

			//y = 1;
			/*cont1 = x1 + x2; //my first pattern
			strpos = cont1 / 2;
			


			cont2 = x2 /2;
			y1 = strpos-cont2;*/
	 

			for (y = 1; y <= (x2/2) -1; y++)
			{
				System.Console.SetCursorPosition(x2 , y);// stuck in here..
				System.Console.WriteLine("*");
				

			}
			for (x = 1; x <=((x1 + x2) / 2) - 3; x++)
			{
				System.Console.SetCursorPosition(x, 0);
				System.Console.WriteLine(" " + str);
				//System.Console.SetCursorPosition(x + 12, len); // not sure if this is correct
				//System.Console.WriteLine("*");

			}


			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");

		}
	}
}


This post has been edited by XxXnucronXxX: 03 January 2010 - 04:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: asterisk pattern using for-loop (console app)

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: asterisk pattern using for-loop (console app)

Posted 03 January 2010 - 09:11 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 XxXnucronXxX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 08

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 06:53 AM

View PostPsychoCoder, on 3 Jan, 2010 - 08:11 AM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field


thanks for the reply.. i have revised my code (attached at the lower part)..

here's my detailed explanation on my problem..

Posted Image

this is the part of the code (basing from the above image):
 for (x = 1; x <= (x1 + x2) / 2; x++)
			{
				System.Console.SetCursorPosition(x, 0);
				System.Console.WriteLine(" " + str);

				for (cont1 = 1; cont1 + 1 <= x2 / 2; cont1++) // serves as y-axis
				{
					System.Console.SetCursorPosition( x2,cont1 );
					System.Console.WriteLine("*");
					//System.Console.SetCursorPosition(cont1 + ((len*(y1+x1))+len),len ); // suppose to be the next move
					//System.Console.WriteLine("*");

					
				}





revised code:
int x1, y1, x2, len, x, y ,cont1;
			string str;

			

			System.Console.WriteLine("Please input x1(point 1 horizontal coord.:");
			x1 = Convert.ToInt16(System.Console.ReadLine());

			System.Console.WriteLine("Please input y1(point 1 vertical coord.:");
			y1 = Convert.ToInt16(System.Console.ReadLine());

			System.Console.WriteLine("Please input length:");
			len = Convert.ToInt16(System.Console.ReadLine());

			System.Console.WriteLine("Please input your name:");
			str = System.Console.ReadLine();
			x2 = str.Length;

			


			System.Console.Clear();




			
			for (x = 1; x <= (x1 + x2) / 2; x++)
			{
				System.Console.SetCursorPosition(x, 0);
				System.Console.WriteLine(" " + str);

				for (cont1 = 1; cont1 + 1 <= x2 / 2; cont1++) // serves as y-axis
				{
					System.Console.SetCursorPosition( x2,cont1 );
					System.Console.WriteLine("*");
					//System.Console.SetCursorPosition(cont1 + ((len*(y1+x1))+len),len );
					//System.Console.WriteLine("*");

					
				}
			   

				

			}

			

			


			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
		}

This post has been edited by XxXnucronXxX: 04 January 2010 - 07:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 07:14 AM

You have to do that pattern horizontal line by horizontal line. You can't do it by doing the vertical line, then the horizontal, then one slanted line and another. The idea is for you to use your brain to come up with the algorithm -- using loops -- for printing the pattern.

And your assignment talks about x2, but I don't see its definition anywhere.
Was This Post Helpful? 1
  • +
  • -

#5 XxXnucronXxX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 08

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 07:45 AM

View PostJackOfAllTrades, on 4 Jan, 2010 - 06:14 AM, said:

And your assignment talks about x2, but I don't see its definition anywhere.


hmmm.. that is quite intriguing.. so that means i cant get it right b'coz of x2 not defined.. that really sparked something.. and thanks for the advise to start my next move in horizontal line by horizontal line. im a bit frustrated by my calculus and with that, it affected my Data Structures subject (programming).. *sigh*

This post has been edited by XxXnucronXxX: 04 January 2010 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 09:59 AM

Why don't you use Console.Write("something");
instead of Console.WriteLine("something");
?

Then you won't have to output a whole line each time, so if you have something else to write on that line (like spaces or another *) you can do so.

Also, this is bad:
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");



Did you want to print nothing but a space on each of the next eight lines? If that is what you want to do (why I don't know) then you can use a for loop to accomplish it, instead of repeating yourself 8 times. :)

Are you not allowed to put
using System;
in your program? Then you won't have to write System. on every line.

Here's an example from this site.:
using System;
public class HelloWorld
{
   public static void Main()
   {
	   Console.WriteLine("Hello World!");			 // relies on "using System;"
	   Console.Write("This is");
	   Console.Write("... my first program!\n");
	   System.Console.WriteLine("Goodbye World!");	// no "using" statement required
   }
}


Was This Post Helpful? 1
  • +
  • -

#7 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 10:28 AM

View PostOliveOyl3471, on 4 Jan, 2010 - 10:59 AM, said:

Also, this is bad:
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");



Did you want to print nothing but a space on each of the next eight lines? If that is what you want to do (why I don't know) then you can use a for loop to accomplish it, instead of repeating yourself 8 times. :)


Even faster:

Console.WriteLine("\n\n\n\n\n\n\n\n");


Was This Post Helpful? 1
  • +
  • -

#8 OliveOyl3471   User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 135
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 10:43 AM

Correct, but I was thinking that perhaps he'd want to write something on each line (such as spaces or an asterisk, then spaces & another *, more spaces and another *) and would need different amounts of spaces for each line, so that the asterisks will be in the correct places to make the pattern he's wanting to make. So if he used a for loop, there would be an iterator, and maybe the iterator could be used to get the amount of spaces for each line.
Was This Post Helpful? 0
  • +
  • -

#9 XxXnucronXxX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 08

Re: asterisk pattern using for-loop (console app)

Posted 04 January 2010 - 05:13 PM

View PostOliveOyl3471, on 4 Jan, 2010 - 08:59 AM, said:

Why don't you use Console.Write("something");
instead of Console.WriteLine("something");
?

Then you won't have to output a whole line each time, so if you have something else to write on that line (like spaces or another *) you can do so.

Also, this is bad:
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");



Did you want to print nothing but a space on each of the next eight lines? If that is what you want to do (why I don't know) then you can use a for loop to accomplish it, instead of repeating yourself 8 times. :)

Are you not allowed to put
using System;
in your program? Then you won't have to write System. on every line.

Here's an example from this site.:
using System;
public class HelloWorld
{
   public static void Main()
   {
	   Console.WriteLine("Hello World!");			 // relies on "using System;"
	   Console.Write("This is");
	   Console.Write("... my first program!\n");
	   System.Console.WriteLine("Goodbye World!");	// no "using" statement required
   }
}



we are allowed to use using system.. and thanks for the tips Olive.. our teacher just taught us System.Console.WriteLine.. also thanks for the example, i'll try it out.. :^: and btw.. i put those spaces so the pattern will have a lot of space, b'coz the press any key to continue.. interferes with the output.. XD


View PostSwiftStriker00, on 4 Jan, 2010 - 09:28 AM, said:

View PostOliveOyl3471, on 4 Jan, 2010 - 10:59 AM, said:

Also, this is bad:
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");
			System.Console.WriteLine(" ");



Did you want to print nothing but a space on each of the next eight lines? If that is what you want to do (why I don't know) then you can use a for loop to accomplish it, instead of repeating yourself 8 times. :)


Even faster:

Console.WriteLine("\n\n\n\n\n\n\n\n");



SwiftStriker00 thanks also for the tip.. this will help a lot.. :)

This post has been edited by XxXnucronXxX: 04 January 2010 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1