How to do this?

  • Thread starter Thread starter Amanda
  • Start date Start date
Ben said:
Using an array might work, but the reason the instructor wants you to
use Console.Write("*") is that he wants you to think about loops and
loops within loops.

Yes, I realized that. He should have stated CLEARLy that instruction
before displaying all triangles examples. I got those done last night
anyway.
The code you posted shows that you have got the
general idea (but bear in mind the comments by chanmm). To get your
triangles side by side, you will have to think a bit harder about the
loops within loops involved, ie not just Triangle1 and Triangle2 as
you have written them.

Yes, I had to think harder for that and got it. Thanks.

In your opinion, using arrays would defeat the purpose of this program,
i.e using for loops tin a way the instructor intended, wouldn't it?
 
Jon said:
Where did you say "Suggest me with logic"? I didn't see anything saying
that.

Okay, okay. I said "Any guide?" Gimme a break, will you. I had flu,
you know?

Shall we take a look at the first sentence of your first post?

If you wish but haven't i told you that I didn't see the instructor's
specific instructors later (in second post?
<quote>
Write a program that will allow user to choose to display traingles
and diamonds composed of asterisks ina lopping condition?
</quote>

How is that *not* asking people to write code for you?

See, I told you so. You didn't read things in context and hence you
didn't read properly.

As I said, if you could say how far you've got, and which particular
bit is giving you difficulty, you'll get a lot more positive replies
than you have done so far.

You are right but I was just getting started and was going to post my
code soon but you were too quick in using the lines "you want people to
do homework" or somwhtign like that as if I were a little kid.
If you're not willing to do that, and only
want to post insults, then you're unlikely to get help.

I wasn't insulting. I was merely pointing out how people judge each
other *SO* quickly and assume things. Beside, with the flu (nto too bad
yet but am worried that I will lose more time with this damn flu; I
ca't afford it), I wasn't really patient to explain the way I am doing
now. Anyway, have a good day.
 
chanmm said:
You loop is messy. My advise is your style is either < or <= and don't mess
them around.

Don't bother what reply you got. See, what you want is answer. So from here
what you can pick up is learning is never easy. If you really so interesting
in C# then go here:
http://msdn2.microsoft.com/en-us/vcsharp/default.aspx

Click on Learn C# and see what you have. Good luck.

Thanks but I hate that kind of big pile of information where I have to
go through the maze where it will only show basic for loop exapmples
anyway.

Yes, learning is nott hat difficult at low level like now which I am
trying to become very efficient so that i can move onto advanced topics
quickly but time is a problem.
 
Kevin said:
Hi Amanda,


<snip>

Since there is apparently a lot of bad blood going on here,

Bad blood? It was more like fun to me. Just kidding:) Well, I forget
easily especially fights, and so Jon is forgiven for treating me like a
college kid:)
please allow me
to try and smooth things out, since I have no dog in this fight.

First, if you will re-read your initial post, you did *not* say "Suggest me
with logic".

I did say "Any guide" and my intention was not to get the whole code
for the program. Anyone who wants that would be comunicatinfg witht
heir classmates, not here, don't you think?
Below this reply I have posted the exact message, so that you
may review it. You simply gave a problem and requested "Write a program that
will ... ?" I will presume that English is not your native language, as the
syntax of your query is an imperative (command language) followed by a
question mark, which I will interpret as being more along the lines of "How
do I write a program that will...?".

Hey you are a genius. I didn't grow with English beyond knowing the
basic. And I hate typing.

Hey don't worry too much. I know that Jon assumed that I was a college
kid and trying to guide but I have a MS in Chemistry and am taking a
class so that I would be forced to do the work - I get sidtracked
otherwise - and learn C# syntax. Frankly, I don't like online class but
for C#, that's the only option at this school.


I read the rest of your posts and will respond that I didn't explain my
situation only because I had flu and didn't want to waste my energy
last night. I made the accusation about judging because of personal
reasons (beign treated like that by oldest sister) and I am definitely
sorry about that. And I was a bit rude in using the phrase "judging
with one's standard, ego, etc." That was my initial reaction because I
just hated being treated like a college kid or a kid period. I get that
from much older sisters all the time and all my misery in my whole
life comes from dealing with them.


I am trying to get better programming skills so that I can get a
programming job in medicinal chemistry environement. I need to stay
away form direct exposure to chemicals. Long story how I didn't know
about my nasal condition and got Chemistry degrees.

Good to know that Jon is good at "turning the other cheek."
 
Jon

Will you accept my apology? If I didn't have flu, I would have typed
a bit more and explained.
 
Ben said:
Is this homework, by any chance?

It's an optional homework. Beside, I am not in need of a grade. If I
need a grade, I will be more agressive then this and get the code
somehow.

People, I am trying to learn, okay?
 
Yes, learning is nott hat difficult at low level like now which I am
trying to become very efficient so that i can move onto advanced topics
quickly but time is a problem.

Yes, but... you do need to put the time in to thinking about logical
problems such as the assignment you quote. It is, after all, a general
programming problem, not specifically about C# at all. Knowing how to
solve problems like that one[1] is a transferrable skill. There is no
real substitute for putting in the time and thinking about it.

[1] Ummm... not knowing how to solve it, exactly, more knowing how to
go about solving similar problems in a programming context.
 
Amanda said:
Will you accept my apology? If I didn't have flu, I would have typed
a bit more and explained.

Yup, no problem. Now, did the answer from Ben solve everything for you?
If not, could you give more details about what exact part you're having
trouble with? If you could post the code as far as you've done it (even
if it has a lot of comments like "// Go round the loop here") that
would help a lot.

Just to make it clear: everyone here does want to help you learn. We
just have experience of a lot of people wanting quick answers instead
of taking the time to understand how things work. You'd be amazed at
how much time people will put into helping you once we're all pulling
in the same direction :)
 
Jon said:
Yup, no problem.
Thanks.

Now, did the answer from Ben solve everything for you?

I saw his post this morning. I got the trangles doen last night and
it's basically like what he said.

If not, could you give more details about what exact part you're having
trouble with? If you could post the code as far as you've done it (even
if it has a lot of comments like "// Go round the loop here") that
would help a lot.

Rihgt now I am working on the diamond. I am just doing w/o the option
of giving the user to enter an odd integer, like this:

public static void Diamond()
{


for (int i = 1, j = 0; i <9; i++)
{
for (int p = 8; p >=j; p--)
Console.Write(" ");
for (int p = 0; p <= j; p++)
Console.Write("*");
for (int p = 1; p <= j; p++)
Console.Write("*");
j++;
Console.WriteLine();
}

for (int i = 0, j = 0; i < 9; i++)
{
for (int c = 8; c >= j; c--)
Console.Write("*");
Console.WriteLine();
for (int p = 0; p <= j; p++)
Console.Write(" ");
for (int p = 9; p > j; p--)
Console.Write("*");
j++;
}

I *need* to get rid of the ninth line in display (see below). May be I
should use a different logic and will definitely have to when I allow
the user to enter an odd number but until then, I want to solve the
curent issue.



*
***
*****
*******
*********
***********
*************
***************
*********
*****************
***************
*************
***********
*********
*******
*****
***
*
Just to make it clear: everyone here does want to help you learn. We
just have experience of a lot of people wanting quick answers instead
of taking the time to understand how things work. You'd be amazed at
how much time people will put into helping you once we're all pulling
in the same direction :)

I know. These comp.lang forums are amazing. Thanks.
 
Ben said:
Yes, but... you do need to put the time in to thinking about logical
problems such as the assignment you quote. It is, after all, a general
programming problem, not specifically about C# at all.

I just meant that my goal was to know C# syntax but as far as
programming is concerned I was concentrating on Java - the industry I
want to get in is using Java mainly at the moment - but this C#
exercise is taking more time but that's ok. This type of logic is good
practise since it's a tranferrable skill, like you said. I do need to
get to SErvlets and JSP stuff ASAP soon after I becomes efficeint with
arrays, exception, file handling + I alos need to know Collection well.


I am in taking C# for syntax so that I can put it on resume.
Knowing how to
solve problems like that one[1] is a transferrable skill.
There is no
real substitute for putting in the time and thinking about it.
True.


[1] Ummm... not knowing how to solve it, exactly, more knowing how to
go about solving similar problems in a programming context.
 
Amanda said:
Rihgt now I am working on the diamond. I am just doing w/o the option
of giving the user to enter an odd integer, like this:

public static void Diamond()
{


for (int i = 1, j = 0; i <9; i++)
{
for (int p = 8; p >=j; p--)
Console.Write(" ");
for (int p = 0; p <= j; p++)
Console.Write("*");
for (int p = 1; p <= j; p++)
Console.Write("*");
j++;
Console.WriteLine();
}

for (int i = 0, j = 0; i < 9; i++)
{
for (int c = 8; c >= j; c--)
Console.Write("*");
Console.WriteLine();
for (int p = 0; p <= j; p++)
Console.Write(" ");
for (int p = 9; p > j; p--)
Console.Write("*");
j++;
}

Okay. Before moving on to changing what the code does, I'm going to do
two things:

1) Put braces around *every* loop, even the inner ones.
2) Get rid of j.

To explain the second bit further:
In both of your loops, j is computable from i - in the first case,
j=i-1, in the second case j=i. It's very rare to see this sort of
double initialization in production code (at least in my experience)
partly because it makes it harder to understand. The whole goal of this
first stage is to change the code to make it easier to understand,
without changing the behaviour. The technical name for that activity is
*refactoring*.

So, here's your code after the above changes:

public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
for (int p = 8; p >= (i-1); p--)
{
Console.Write(" ");
}
for (int p = 0; p <= (i-1); p++)
{
Console.Write("*");
}
for (int p = 1; p <= (i-1); p++)
{
Console.Write("*");
}
Console.WriteLine();
}

for (int i = 0; i < 9; i++)
{
for (int c = 8; c >= i; c--)
{
Console.Write("*");
}
Console.WriteLine();
for (int p = 0; p <= i; p++)
{
Console.Write(" ");
}
for (int p = 9; p > i; p--)
{
Console.Write("*");
}
}
}

Now let's apply another refactoring suggested by chanmm: change every
inner loop to use <, and count upwards from 0:

public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
for (int p = 0; p < 10-i; p++)
{
Console.Write(" ");
}
for (int p = 0; p < i; p++)
{
Console.Write("*");
}
for (int p = 0; p < i-1; p++)
{
Console.Write("*");
}
Console.WriteLine();
}

for (int i = 0; i < 9; i++)
{
for (int c = 0; c < 9-i; c++)
{
Console.Write("*");
}
Console.WriteLine();
for (int p = 0; p < i+1; p++)
{
Console.Write(" ");
}
for (int p = 0; p < 9-i; p++)
{
Console.Write("*");
}
}
}

Now the nice thing about those loops is that they're all writing either
spaces or stars, with the number of spaces or stars to write being the
right hand side of the middle expression. We'll now use the "extract
method" refactoring to make things simpler again, by having one method
to write stars and one to write spaces. (We could have a single method
which took a parameter which was the character to write, but that's
just a variation on the theme.)

public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
WriteSpaces (10-i);
WriteStars (i);
WriteStars (i-1);
Console.WriteLine();
}

for (int i = 0; i < 9; i++)
{
WriteStars (9-i);
Console.WriteLine();
WriteSpaces(i+1);
WriteStars(9-i);
}
}

static void WriteStars(int count)
{
for (int i=0; i < count; i++)
{
Console.Write("*");
}
}

static void WriteSpaces(int count)
{
for (int i=0; i < count; i++)
{
Console.Write(" ");
}
}

Now, I'll pause there - partly because we've done an awful lot already,
and partly because I don't know whether you're allowed to use extra
methods in your code to make it more readable. Apologies if you've
already stated this in another post.

Now, as for the extra line - note that you're calling Console.WriteLine
at the *end* of each iteration of the first loop, but *in the middle*
of each iteration of the second loop. That should give you a hint as to
how to change the logic.

Do let us know how you get on with the above - and if any of it doesn't
make sense, please say and I'll go back and explain in more detail.
 
Hi Cor,

In this case, I suspected that some of the misunderstanding on the part of
the readers might have contributed to the problem. If you choose to
interpret my intention as otherwise, that is your choice. All I can say is
that it was my intention to be a peacemaker. Miscommunication occurs even
among people who speak the same language. We tend to hear what we want to
hear.

--
HTH,

Kevin Spencer
Microsoft MVP
Computer Control Freak
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.
 
Jon wrote:
[..]
Okay. Before moving on to changing what the code does, I'm going to do
two things:

1) Put braces around *every* loop, even the inner ones.
2) Get rid of j.

To explain the second bit further:
In both of your loops, j is computable from i - in the first case,
j=i-1, in the second case j=i. It's very rare to see this sort of
double initialization in production code (at least in my experience)
partly because it makes it harder to understand. The whole goal of this
first stage is to change the code to make it easier to understand,
without changing the behaviour. The technical name for that activity is
*refactoring*.
Ok.


So, here's your code after the above changes: []


Now let's apply another refactoring suggested by chanmm: change every
inner loop to use <, and count upwards from 0:
[]


Now the nice thing about those loops is that they're all writing either
spaces or stars, with the number of spaces or stars to write being the
right hand side of the middle expression. We'll now use the "extract
method" refactoring to make things simpler again, by having one method
to write stars and one to write spaces.

I plan to do that as a final step but the way I was looping would have
been a nightmare. Thanks for removing j and making all loops starts
from index 0.
(We could have a single method
which took a parameter which was the character to write, but that's
just a variation on the theme.)

public static void Diamond()
{
for (int i = 1; i < 9; i++)
{
WriteSpaces (10-i);
WriteStars (i);
WriteStars (i-1);
Console.WriteLine();
}

for (int i = 0; i < 9; i++)
{
WriteStars (9-i);
Console.WriteLine();
WriteSpaces(i+1);
WriteStars(9-i);
}
}

static void WriteStars(int count)
{
for (int i=0; i < count; i++)
{
Console.Write("*");
}
}

static void WriteSpaces(int count)
{
for (int i=0; i < count; i++)
{
Console.Write(" ");
}
}

Now, I'll pause there - partly because we've done an awful lot already,
and partly because I don't know whether you're allowed to use extra
methods in your code to make it more readable. Apologies if you've
already stated this in another post.

He didn't say anything specifically on that. He gave his executable
file and we can look at the display as example. What extra method were
you thinking of?
Now, as for the extra line - note that you're calling Console.WriteLine
at the *end* of each iteration of the first loop, but *in the middle*
of each iteration of the second loop. That should give you a hint as to
how to change the logic.

Oh..I failed to look inside the for loop for the lower part of the
diamond shape.
Do let us know how you get on with the above - and if any of it doesn't
make sense, please say and I'll go back and explain in more detail.

Thanks. I now have a perfect diamond; the code would work with user's
choice of an odd integer too.
 
Hi Amanda,

I'm glad I could help. And your response is certain to encourage people here
to help you whenever they can.

:-)

Kevin Spencer
Microsoft MVP
Computer Control Freak
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

Hey don't worry too much. I know that Jon assumed that I was a college
kid and trying to guide but I have a MS in Chemistry and am taking a
class so that I would be forced to do the work - I get sidtracked
otherwise - and learn C# syntax. Frankly, I don't like online class but
for C#, that's the only option at this school.
<snip>
 
If I might make another observation about Jon's excellent discussion: This
process of refactoring is an excellent way to solve a problem. Quite often,
it is difficult to write the most elegant, or the most optimal code in the
beginning. A good developer will often begin by writing out a long solution,
what I like to call "bludgeoning your way through" the problem, ignoring the
optimizations in the beginning. Once the first draft of the code is done,
one can look it over for sections that can be refactored, repeated code that
can be made into loops or functions, and other optimizations. As Jon
illustrated, the re-analysis and refactoring process can be repeated, as
each new configuration of the code tends to suggest its own optimizations.
This process generally ends up taking less time than attempting to write the
leanest code on the first run.

And of course, for someone who is learning (as we all are, I would hope),
the most important goal is realized first, which is writing code that does
the job. As you progress with your skill and practice, you will be able to
spot more and more ways of making it leaner, more reusable (writing methods
and classes that perform common tasks), and more extensible.

--
HTH,

Kevin Spencer
Microsoft MVP
Computer Control Freak
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.
 
Kevin,

I had not any doubt that your intentions were as you wrote, I only ask you
to leave the phrases about the knowledge of English out of your messages.
You would once go to England, than you can see how English is completely
different used inside 100 miles. If you go to Great Brittain, you will
probably be even more suprised (Although all inhabitants of Great Brittain
understand each other mostly perfectly)

Cor
 
He didn't say anything specifically on that. He gave his executable
file and we can look at the display as example. What extra method were
you thinking of?

The WriteStars and WriteSpaces ones I added.
Oh..I failed to look inside the for loop for the lower part of the
diamond shape.

Just as an idea for if that kind of thing happens again, one of the
things I'd probably have done in your situation is put:
Console.WriteLine ("Finished first loop");

between the two big loops. That would make it clear which part was
going wrong. Easy to say in retrospect though :)
Thanks. I now have a perfect diamond; the code would work with user's
choice of an odd integer too.

Cool :)
 
Jon said:
The WriteStars and WriteSpaces ones I added.

That should be ok. I was calling Traingles1(), Triangles2(), etc. in
DisplayTrianglesOneBelowAnother().

Just as an idea for if that kind of thing happens again, one of the
things I'd probably have done in your situation is put:
Console.WriteLine ("Finished first loop");

between the two big loops. That would make it clear which part was
going wrong.

Yeah, I need to develope the habit of doing that.
 
Kevin said:
If I might make another observation about Jon's excellent discussion: This
process of refactoring is an excellent way to solve a problem. Quite often,
it is difficult to write the most elegant, or the most optimal code in the
beginning. A good developer will often begin by writing out a long solution,
what I like to call "bludgeoning your way through" the problem, ignoring the
optimizations in the beginning. Once the first draft of the code is done,
one can look it over for sections that can be refactored, repeated code that
can be made into loops or functions, and other optimizations. As Jon
illustrated, the re-analysis and refactoring process can be repeated, as
each new configuration of the code tends to suggest its own optimizations.
This process generally ends up taking less time than attempting to write the
leanest code on the first run.

And of course, for someone who is learning (as we all are, I would hope),
the most important goal is realized first, which is writing code that does
the job. As you progress with your skill and practice, you will be able to
spot more and more ways of making it leaner, more reusable (writing methods
and classes that perform common tasks), and more extensible.

Yes, these days I am a bit more aware of that (and will try to do that)
than when I was just learning programming. At that time, I barely had
enough time to meet deadline ( taking for grade at the time) and so
once I finished it, I didn't have the luxury to go back and optimize
it.
 
I had not any doubt that your intentions were as you wrote, I only ask you
to leave the phrases about the knowledge of English out of your messages.
You would once go to England, than you can see how English is completely
different used inside 100 miles. If you go to Great Brittain, you will
probably be even more suprised (Although all inhabitants of Great Brittain
understand each other mostly perfectly)

Och aye! 'Appen! Awight?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top