Page 1 of 1

Reputation:

# I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:00 AM

hey!
I'll start by mentioning a couple of things -
I know you guys don't help with homework and stuff, but I already know how to do the project, I'd like to know if I can maybe make it a bit shorter because I think it's pretty long.

If you went into this thread with an intention to help and saw the length, don't run away or anything I bet it's pretty simple for you guys.

I have to create a program that receives 3 numbers from the user - a number between 0-9, the 'x' length and the 'y' length.
with this information I have to 'draw' the number on the command prompt with '*'. for instance the number 2 -
x length is 5, y length is 3 :

```*****
*
*
*
*****
*
*
*
*****
```

we were advised to divide everything into methods which will have a 'part' of the number in each one. for example -
a method which is in-charge of the middle line, the left line and the right one.
with the methods I've mentioned above we create the numbers in different methods.

this is what I did, demonstrated with the number 2-

```class Program
{

static int InputX(int x)
{
Console.Write("Enter X value: ");
x = int.Parse(Console.ReadLine());

return x;

}

static int InputY(int y)
{
Console.Write("Enter Y value: ");
y = int.Parse(Console.ReadLine());

return y;

}

static int CoordX(int xValue)
{

xValue = InputX(xValue);

return xValue;
}

static int CoordY(int yValue)
{

yValue = InputY(yValue);

return yValue;
}

static int Left(int x, int y, char z)
{

int i;
for (i = 0; i < y; i++)
{
Console.Write("        ");
Console.WriteLine("*");

}
return z;
}

static int Right(int x, int y, char z)
{
int i, j;
for (i = 0; i < y; i++)
{
Console.Write("        ");

for (j = 1; j < x; j++)
{
Console.Write(" ");
}
Console.WriteLine("*");
}

return z;
}

static int Middle(int x, int y, char z)
{
Console.Write("        ");
int i;
for (i = 0; i < x; i++)
{

Console.Write("*");

}
Console.WriteLine();
return z;
}

static int NumberTwo(int num)
{
int cordX = 0;
cordX = CoordX(cordX);
int cordY = 0;
cordY = CoordY(cordY);

num = Middle(cordX, cordY, 'a');
num = Right(cordX, cordY, 'a');
num = Middle(cordX, cordY, 'a');
num = Left(cordX, cordY, 'a');
num = Middle(cordX, cordY, 'a');

return num;

}

static void Main(string[] args)
{

int Number2 = 0;
Number2 = NumberTwo(2);

}
}

```

what do you guys think?
I'll be honest - the thing that worries me is the 4 functions that are in-charge of receiving the coordination's and assigning them to variables.

I really hope I could get some help here, so thanks in advance!

This post has been edited by insertAlias: 07 February 2011 - 09:34 AM

Is This A Good Question/Topic? 0

## Replies To: I have a project to hand in, is there a way to make my code more effic

### #2 lordofduct

• I'm a cheeseburger

Reputation: 2539
• Posts: 4,641
• Joined: 24-September 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:08 AM

you do a lot of redundant method definitions... what is the point of the two members 'CoordX' and 'CoordY', if all they do is call 'InputX' and 'InputY'... why not just call 'InputX' and 'InputY' directly and remove the coord methods?

Also in the Middle, Left, and Right methods... what is the third param 'char z'??? You don't use it, all you do is take it in, and then return it. What was the point of that? Did you intend to use that char as the symbol to use in place of the "*"??? Anyways, why return it? Why not just make those methods void (unless this was originally C code that you ganked and converted, and there was no void).

I'm sorry but this stinks of, 'you didn't write this'. If this is true, stop now, restart, and write it yourself. Your concern right now shouldn't be 'making it more efficient', it should be 'learning what the fuck it is you're doing'.

This post has been edited by lordofduct: 07 February 2011 - 09:14 AM

Was This Post Helpful? 0

Reputation:

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:21 AM

I won't say anything bad because you're actually doing me a favour by trying to help me out, so I'll just explain why you're awfully wrong!

I did write it, and I can explain why every single thing was done like that....
I'm learning c# in high school and we were requested to do it with int. I can photocopy the pages from our book if you don't believe and you can see what the book is instructing.
every method has a return because that's what we were told to do! the teacher repeated several times that we aren't supposed to do it as void (like the main method) because we don't know how to instance objects (if that's what it's called, when you do Program asd = new Program (); asd.Method())

what makes you so cocksure I copied it?

after we cleared things out -
my bad about the duplicated methods, I thought that if I call InputX and InputY every-time it will ask me to input numbers again and again, but I see it does the same thing with CoorX and CoorY so I took them off.
anything else?

This post has been edited by insertAlias: 07 February 2011 - 09:40 AM
Reason for edit:: removed quote

Was This Post Helpful? 1

Reputation:

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:34 AM

about the z - is there a way I can do 'return' on 3 parameters?
I looked at examples in my textbook and they did have an example of a method with 2 parameters, but they only returned one, supposedly just to return a value to the method like I did?

This post has been edited by insertAlias: 07 February 2011 - 09:40 AM
Reason for edit:: removed quote

Was This Post Helpful? 0

### #5 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4799
• Posts: 8,534
• Joined: 08-June 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:39 AM

Quote

every method has a return because that's what we were told to do! the teacher repeated several times that we aren't supposed to do it as void (like the main method) because we don't know how to instance objects (if that's what it's called, when you do Program asd = new Program (); asd.Method())

Either you misunderstood what he meant, or your teacher doesn't know what the hell he's talking about.

Actually, I'm going to guess you misunderstood him, because the reasoning you provided there was why you use static methods, not why you use a return type.

Static methods don't rely on an instance of an object to be called. Non-static does. Return type, on the other hand, has nothing to do with instances. You can call a void method the same way you call any other. The difference is, it returns no value.

I'd also say you used a lot of redundant methods. I think you could reduce your "InputX", "InputY", "CoordX" and "CoordY" down to one method: GetInput:

```static int GetInput(string s)
{
Console.Write("Enter {0} value: ", s);
int input = int.Parse(Console.ReadLine());
return input;
}
...
...
...
static void DrawNumberTwo()
{
int coordX = GetInput("X");
int coordY = GetInput("Y");
...
}
```

Another issue is the fact that you're passing completely unused parameters. In your Left method, you pass (x, y, z), but you only use y, not counting the pointless returning z. Same for Middle, except you only use X.

You could significantly simplify your program by making these changes. And Left, Middle, Righyt, and NumberTwo should all be void, not int.
Was This Post Helpful? 1

Reputation:

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:47 AM

hey!
thanks for your reply

you're right - the teacher isn't the brightest person in the world... he didn't even explain what I thought was the reason, it's something I understood from the internet.
the only thing he said is we're not dealing with void return types right now....
I actually asked him today and he told us that he demands we do them as 'static int'

about the returned values - so you suggest I take the unused parameters in the certain methods?

thanks again

This post has been edited by insertAlias: 07 February 2011 - 09:55 AM
Reason for edit:: again, removed unnecessary quote

Was This Post Helpful? 0

### #7 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4799
• Posts: 8,534
• Joined: 08-June 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 09:54 AM

If you have to return garbage values because a professor demands it, well, ok, but don't add extra parameters to do it. Just throw a return 0; at the end of the method. I say get rid of all the unused parameters. They serve no purpose.

I've always said to just do what professors tell you, even when it's incorrect. Correcting them isn't as important as just passing the class and forgetting the wrong information later. Remember that just because "static" and the return type are right next to each other, it doesn't mean that they have anything to do with each other. "Static" means that it's shared across all instances of the class, so you can call it without an object reference, and from other static methods. The return type is not connected to that.
Was This Post Helpful? 1

### #8 lordofduct

• I'm a cheeseburger

Reputation: 2539
• Posts: 4,641
• Joined: 24-September 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 10:34 AM

medu, on 07 February 2011 - 09:21 AM, said:

I won't say anything bad because you're actually doing me a favour by trying to help me out, so I'll just explain why you're awfully wrong!

...

what makes you so cocksure I copied it?

...

you missed a word in my last paragraph, it makes a very distinct difference between 'cocksure' and 'speculation'

Quote

...

I'm sorry but this stinks of, 'you didn't write this'. If this is true, stop now, restart, and write it yourself. ...

see the 'if'?

I wasn't cocksure, it just stank of it. We see it a lot here, someone coming in and saying "i gots homeworks, help me clean it up". When actually its just code they stole.

Your arbitrary return types of int, when void would have sufficed, is a 'C' thing. 'C' has no void return type. This led me to the 'IF' statement.

If I had been 'cocksure', you'll be certain that I would not have offered a helping hand whast-so-ever.

This post has been edited by lordofduct: 07 February 2011 - 10:36 AM

Was This Post Helpful? -1

Reputation:

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 10:49 AM

honestly mate let it go
you've done a mistake, happens to everybody
you can just admit it and keep trying to help me if you wish, or maybe if you were offended by me saying you were cocksure then I'm sorry, wasn't my intention at all.... I just didn't understand your aggressive reaction that's all...

a new problem that I found out -
my stupid methods make the 'y axis count' be twice as long, because in the number 2 for instance when the user inserts 5 for Y, it'll be more than 10 actually because it inserts Left & Right twice + the Middle, making it be more a lot than 5...
I've divided the 'i' in the for loops but I still have a problem with the 'middle' bar... I don't want to take off a '* from either Left or Right because it won't be symmetrical...
how should I deal with it?

I hope you guys understood my question by the way...
if you didn't, sorry for my bad english, not my native language... I'll try to rephrase myself!

This post has been edited by insertAlias: 07 February 2011 - 11:42 AM
Reason for edit:: removed quote

Was This Post Helpful? 1

### #10 lordofduct

• I'm a cheeseburger

Reputation: 2539
• Posts: 4,641
• Joined: 24-September 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 07 February 2011 - 11:22 AM

wow, you completely missed what I said. I wasn't offended, I was answering your question of: "what makes you so cocksure I copied it?"
Was This Post Helpful? 0

Reputation:

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 08 February 2011 - 09:32 AM

hi again!

okay, I've improved a couple of things, added all the numbers, deleted the unused parameters, but I now have a new problem -
what do I do when people insert 'x' and 'y' values that can't display the number?
for instance x=1 y=1 and asking for the number 6..
do I send back an error? or do I allow the console to print the result even though it doesn't even look like 6?
Was This Post Helpful? 0

### #12 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4799
• Posts: 8,534
• Joined: 08-June 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 08 February 2011 - 09:46 AM

That depends on what your professor wants you to do. That's what project specifications are for. Any answer we give would be a guess, so my advice is to ask your processor what to do with bad input.
Was This Post Helpful? 0

### #13 Sergio Tapia

• D.I.C Lover

Reputation: 1256
• Posts: 4,168
• Joined: 27-January 10

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 08 February 2011 - 09:54 AM

Ask him clear as day what the input should be and what the output should be with exception handling information.
Was This Post Helpful? 0

Reputation:

## Re: I have a project to hand in, is there a way to make my code more effic

Posted 08 February 2011 - 10:01 AM

okay, emailed him.

another question -
can I ask you guys a bit more in-depth questions? I have something that's really annoying me and I have absolutely no clue how to deal with it! (this is such a disgrace that our teacher gave us such an assignment.. we literally don't know a thing... how does he expect us to deal with such a thing?! hardest thing I've ever wrote. I have no clue what the other students will do)

as you might remember from my posts yesterday, I said that he requested we divide it into 'lines' which will build the number. Left,Right,Middle, Double Lines.
now, most numbers require more than 1 vertical line, making the y axis twice the length the user asked for. I thought about dividing the y value when doing the 'for' loops which helped me cut down the length of it, but I still have problems cause by the horizontal line.... for example -

the left vertical line-
```int i;
for (i = 1; i < y/2; i++)
{
Console.Write("        ");
Console.WriteLine("*");

}

```

right vertical line-
```int i, j;
for (i = 1; i < y/2; i++)
{
Console.Write("        ");

for (j = 1; j < x; j++)
{
Console.Write(" ");
}
Console.WriteLine("*");
}

```

middle horizontal line-

```Console.Write("        ");
int i;
for (i = 0; i < x; i++)
{

Console.Write("*");

}
Console.WriteLine();

```

and the number 2 -
```int cordX = 0;
cordX = InputX(cordX);
int cordY = 0;
cordY = InputY(cordY);

char asd = 'a';
asd = TextColour('a');

num = Middle(cordX, cordY, 'a');
num = Right(cordX, cordY, 'a');
num = Middle(cordX, cordY, 'a');
num = Left(cordX, cordY, 'a');
num = Middle(cordX, cordY, 'a');

```

now some numbers will cause a longer vertical 'y' value because of the middle ones adding up to the count...

what should I do?
thanks!
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }