Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
Hi All
 
I need to input number 3 and it should output in square form as following:
   ###
   # #
   ###

Here is my code
 
 lang"C#">
 string input = Console.ReadLine();
 
  int s = #;
 for (int i = 0; i<input.Count();i++)
 {
    int a = convert.int32(input[i]);
        s = s * #;
     //this where I am stuck 

   
 
 }
Posted 6-Jun-13 0:51am
5Sazi282
Edited 6-Jun-13 0:57am
CPallini325.7K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

That is pretty much the oddest code I've seen in a while! Laugh | :laugh:
 
I don't think you quite understand what you are trying to do - neither do I come to that, since your example of a "square form" doesn't seem to match anything I would recognise.
 
However, this may be just a mixup in language, or you assuming I can see your screen - which I can't.
 
So, you want to read a number from the user:
string input = Console.ReadLine();
Yes, that'l work - it won't just read numbers though, so you will have to do some checking later.
The rest of it doesn't make much sense though, you don't appear to do anything with the values you converted, and multiplying strings by anything doesn't work.
 
So, go back a stage, and try to tell me what you are trying to achieve - show me what input generates what output, and I'll see if I can help you get the code together.
 
[EDIT]
 
OK, this isn't difficult, but it needs a little thought. Since it's your homework, I'm going to let you write the actual code, pretty much, but I'l help you along.
 
First things first: split this into two parts, by writing two methods:
 
private int GetNumber()
    {
    int result = 0;
    // Your code goes here
    return result;
    }
 
private static void PrintSquare(int num)
    {
    Console.WriteLine(num);
    // Your code goes here
    }
You then call them from your Main method.
The first one is pretty trivial - you have that already (although I would recommend you do some error checking in case the user enters "Hello" instead of a number)
 
The second one is more complex (but not a lot).
First off, check that the number is positive and not zero - if it isn't then you don't need to do anything else.
Then, create two character arrays, big enough to hold all the characters you need in your line:
char[] outerLine = new char[num];
char[] centreLine = new char[num];
Then, use a loop to fill the outer line completely with '#' characters, and the center line with spaces.
Then set the first and last element of the centerline to '#' characters.
Then print the data:
1) Print the outer line once:
   Console.WriteLine(outerLine);
2) Check if the number is greater than one. If it isn't you don't need to do anything else.
3) Use a loop to print the centreline the (number - 2) times
   Console.WriteLine(centreLine);
4) Print the outer line once more.
 
Done!
 
I realize that some bits of this may not make a lot of sense, but they will if you give it a try. If you can't get it to work, show me the code you have so far, and I'll show you how to correct it.
 
[/EDIT]
  Permalink  
v2
Comments
5Sazi at 6-Jun-13 6:09am
   
this should be an output if i input number 3
###
# #
###
 
This is code I have type
namespace square
{
class Program
{
static void Main(string[] args)
{
int num =Convert.ToInt32( Console.ReadLine());
 

for (int i = 0; i < num; i++)
{



}
Console.WriteLine(num);
Console.ReadLine();
}
}
}
OriginalGriff at 6-Jun-13 6:34am
   
Answer updated
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You have to:
Correctly parse user input to obtain a meaningful integer, say N.
Then you have two special cases: first and last line, composed of N '#'; all the other lines have 1 '#', (N-2) blanks and 1 final '#'.
It doesn't look a daunting task to me.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 6 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100