Answered by:
C# please help doing this ?

Question
-
we have this code :
static void DrawStars(int n){
int i;
for(i=1;i<=n;i++)
console.write("*");
console.writeline();
}
so how to make this when you have num=4 :
*
*****
****
***
**
*
when you have this :
static void Main()
{
int i,num;
console.writeline("Enter number of lines");
num=int.parse(console.readline());
and the mission is to continue the code and I do not know how to ?
can you help me ?
Thanks.- Edited by c sharp programmer Thursday, March 14, 2013 6:47 PM
Answers
-
This looks like a homework assignment so we cannot provide you the code as that would violate the Code of Conduct. We can point you in the right direction though.
It is unclear whether your loop is controlling how many lines to print or whether it specifies how many lines to print with increasing stars and then you want to repeat the loop again with decreasing stars.
If the user enters N and you want to print that many lines of increasing stars then your initial loop is just fine. The only issue is that you're printing a single star on each line. Instead you'll want to print a series of stars based upon the current value of i. One approach to doing that is using the string constructor that accepts a character and a count. It will then populate the string with the corresponding # of characters. For example:
var str = new string('*', 4); // str = "****"You need to now loop again to get the descending star lines. Rather than having i go from 1 to N have it go from N to 1 using the decrement operator. You'll get close to what you want. You'll probably need to tweak the code a little.
If the user enters N and you want to print half as ascending and half descending then the code remains mostly the same. The only difference is that rather than i going from 1 to N (or N to 1) you'll want to divide N by 2 in order to get half the lines ascending and half descending. It isn't clear what you should do if N is not an even number.
If you want to get fancy then you could do it all in a single loop by using an if statement and a formula but I think the 2 loops is clearer to understand. Good luck.
Michael Taylor - 3/14/2013
http://msmvps.com/blogs/p3net
- Proposed as answer by Mike FengModerator Friday, March 15, 2013 3:42 PM
- Marked as answer by Mike FengModerator Wednesday, April 3, 2013 4:28 PM
-
plz use "num" as a for loop's end point number to print out your previous four lines with star.
And then, plz use for loop to print the next 3 lines with stars, but remember the starting row stars' num begins from 3, and end to 1.
Plz write down ur codes, anything urgent or error or exceptions, plz list your codes;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- Proposed as answer by Mike FengModerator Friday, March 15, 2013 3:43 PM
- Marked as answer by Mike FengModerator Wednesday, April 3, 2013 4:28 PM
-
Look Ma, no loops!
void PrintStars(int n, bool upTo=true, bool downFrom=true) { if (n<=0) return; if (upTo) PrintStars(n-1, true, false); Console.WriteLine(new String('*', n)); if (downFrom) PrintStars(n-1, false, true); }
To start it up
PrintStars(5);
And the best thing - there's no way you could convince your teacher that you wrote this!
Paul Linton
- Proposed as answer by puni_vidhya Friday, March 22, 2013 10:23 AM
- Marked as answer by Mike FengModerator Wednesday, April 3, 2013 4:27 PM
All replies
-
This looks like a homework assignment so we cannot provide you the code as that would violate the Code of Conduct. We can point you in the right direction though.
It is unclear whether your loop is controlling how many lines to print or whether it specifies how many lines to print with increasing stars and then you want to repeat the loop again with decreasing stars.
If the user enters N and you want to print that many lines of increasing stars then your initial loop is just fine. The only issue is that you're printing a single star on each line. Instead you'll want to print a series of stars based upon the current value of i. One approach to doing that is using the string constructor that accepts a character and a count. It will then populate the string with the corresponding # of characters. For example:
var str = new string('*', 4); // str = "****"You need to now loop again to get the descending star lines. Rather than having i go from 1 to N have it go from N to 1 using the decrement operator. You'll get close to what you want. You'll probably need to tweak the code a little.
If the user enters N and you want to print half as ascending and half descending then the code remains mostly the same. The only difference is that rather than i going from 1 to N (or N to 1) you'll want to divide N by 2 in order to get half the lines ascending and half descending. It isn't clear what you should do if N is not an even number.
If you want to get fancy then you could do it all in a single loop by using an if statement and a formula but I think the 2 loops is clearer to understand. Good luck.
Michael Taylor - 3/14/2013
http://msmvps.com/blogs/p3net
- Proposed as answer by Mike FengModerator Friday, March 15, 2013 3:42 PM
- Marked as answer by Mike FengModerator Wednesday, April 3, 2013 4:28 PM
-
Hi, you may do it this way:
int i,j, num; Console.WriteLine("Enter the number of lines"); int.TryParse(Console.ReadLine(), out num); for (i = 1; i < num; i++) { for (j = 1; j <= i; j++) Console.Write("*"); } for (i = num; i > 0; i--) { for (j = 1; j <= i; j++) Console.Write("*"); Console.WriteLine(""); }
- Edited by BGQQ Thursday, March 14, 2013 8:45 PM
-
-
BGQQ, it against the Code of Conduct on these forums to post answers to questions that are homework or potentially malicious in nature. Please do not provide solutions to these problems.
OK, sure, this seems very reasonable. By the way, waht is the "Code of Conduct"? -
plz use "num" as a for loop's end point number to print out your previous four lines with star.
And then, plz use for loop to print the next 3 lines with stars, but remember the starting row stars' num begins from 3, and end to 1.
Plz write down ur codes, anything urgent or error or exceptions, plz list your codes;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats- Proposed as answer by Mike FengModerator Friday, March 15, 2013 3:43 PM
- Marked as answer by Mike FengModerator Wednesday, April 3, 2013 4:28 PM
-
Code of Conduct (CoC) are the rules that are expected to be followed within the forums. Some of the rules you agreed to when you signed up (it was in the fine print). This includes things like no spamming, no profanity, no advertising, etc. The rest of the CoCs are rules that were set up by the forum members and moderators to ensure that the forums remain a place where folks can get professional assistance without worrying about issues that are common in some other forums - flaming, name calling, bad information, etc. I believe some of the CoCs are pinned to the top of most of the forums for easier access.
As part of these we include not asking for solutions to homework and providing said answers. That's because the person asking the question is ultimately going to hurt themselves and folks providing answers aren't helping the matter. If a student gets the solution (not just help) to an assignment then they'll likely just turn it in. Professors can generally pick off code that a student didn't write (I should know as I'm a professor as well). This is generally considered cheating so the student would be punished (a 0 on the assignment all the way up to expulsion). Even if it gets by the professor the next assignment will likely build upon that so the student will be even more lost. It is really sad when a student spends all the time and money to get a degree and then is unable to find a job because they "cheated" their way through the courses. It can be difficult to tell assignments from real questions and mistakes are made but generally it is pretty obvious. We are glad to provide suggestions and guidance but the CoC we don't provide the solution.
The other big area the CoC covers is answering questions that could lead toward malicious code. Hackers use these forums too so any question that seems of questionable use (i.e. how do I grab a user's password when they log into Windows without them knowing). Sometimes there are reasonable requests but most of the time it isn't.
Thanks for understanding and welcome to the forums.
-
Hi C charp programmer,
BGQQ's code doesn't generate the same result as you wanted, but it provides a way, so you can keep try this way on correct this code or take the other suggestions provides by members.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
-
How !?
Hi there,
As members suggested, you need to try to figure it by yourself. Or you can post your progress here and tell us what block you up.
Thank you.
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
"Code of Conduct (CoC) are the rules that are expected to be followed within the forums. "
Is there a reference available to this document?
Can I ask? Why did you not use the 'Report As Abuse' => 'Other Term of Use or Code of Conduct violation' option to get the offending post deleted? Why is the post still present? Is it just because the code is wrong?
Paul Linton
-
How !?
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
Look Ma, no loops!
void PrintStars(int n, bool upTo=true, bool downFrom=true) { if (n<=0) return; if (upTo) PrintStars(n-1, true, false); Console.WriteLine(new String('*', n)); if (downFrom) PrintStars(n-1, false, true); }
To start it up
PrintStars(5);
And the best thing - there's no way you could convince your teacher that you wrote this!
Paul Linton
- Proposed as answer by puni_vidhya Friday, March 22, 2013 10:23 AM
- Marked as answer by Mike FengModerator Wednesday, April 3, 2013 4:27 PM
-
Official CoC: http://msdn.microsoft.com/en-US/cc300389.aspx
Unofficial
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dd4c2e57-ba8b-476c-8f39-38bf623e64ff
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/ef62be78-e33b-4499-9b1a-d8b96600a10cWith the archiving and combination of forums I cannot find the remaining pinned guidelines that have been posted but most of them were similar to the above.
I didn't move the thread because it wasn't clear whether this was a homework assignment or not. I was wanting to let the OP know that we don't provide homework support because it hurts everybody. If they had responded that it wasn't homework but an example from a book or something then it would have been appropriate to allow the post to continue.
-
OK. So the CofC actually doesn't mention the word homework. The "How to avoid aiding the development of malicious code" uses the detection of homework as an example of how to try to detect questions relating to malicious code, so no prohibitions relating to homework there either. Similarly, the 'Tips' post does not mention the word homework.
Bottom line - there is nothing in the Code of Conduct which prohibits asking or answering homework questions.
Don't get me wrong, I've been a teacher and understand the importance of people not being handed answers on a plate. I have railed long and hard at people for giving homework answers in this and other forums. Giving homework answers is a bad idea, it's just not against the code of conduct.
Paul Linton
-
The moderators on these forums tend to follow the rule that we don't provide answers to homework questions. There was a post a long time ago about it (IIRC) and the moderators still tend to enforce it. If you want to spend your time searching for documented proof go ahead. Be warned that it is probably in the archived forums now.
There is nothing from MS that officially prohibits it so people won't get banned. However our goal is to provide help to those who need it and not to those who would use it for ill means (malicious or otherwise). Whether you agree or not is your choice. However I think you will find that you are in the minority if you decide to provide answers to obvious homework questions. I will continue to identify it as a breach of the CoC that we follow on these forums as it goes against the rule of providing answers to questions that are intent for ill means (in this case cheating).
-
PaulLinton,
Thanks for your nice work on this.
It would be better to give a student some ideas according to what he/she has learnt, to guide him/her how to cope with a new problem with old knowledge...;)
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats -
"The moderators on these forums tend to follow the rule that we don't provide answers to homework questions."
And that is a good thing that I agree with 100%. It is OK to hold that opinion, but it is not part of the Code of Conduct that we signed up to, as you claimed. As moderators you are able to enforce that opinion just don't try to get quasi-legalistic about it.
Paul Linton
-
-
we have this code :
static void DrawStars(int n){
int i;
for(i=1;i<=n;i++)
console.write("*");
console.writeline();
}
so how to make this when you have num=4 :
*
*****
****
***
**
*
when you have this :
static void Main()
{
int i,num;
console.writeline("Enter number of lines");
num=int.parse(console.readline());
and the mission is to continue the code and I do not know how to ?
can you help me ?
Thanks.
One possible way without a formal loop (maybe, not what you're looking for) is:
namespace Print_Stars { class Program { static void Main(string[] args) { Action<ushort> PrintStars = n => new string[(n << 1) - 1] .Aggregate("*", (a, s) => "*" + ((Func<string, string>)(x => { Console.WriteLine(x.Substring(0, x.Length <= n ? x.Length : (n << 1) - x.Length)); return x; }))(a) ); PrintStars(10); // print from 1 up to 10 starts, and then, down to 1 Console.ReadKey(); } } }
•