C for Loop. Arrays in C Programming. Pointers in C. Find roots of a quadratic equation. Print Pyramids and Patterns. Check prime number. Print the Fibonacci series. Reference Materials string. Another approach would be to pass PrintPyramid a TextWriter , so that you can have it write to wherever you want.
I would rename the method to WriterPyramid , since that is the usual theme in. You can then pass it System. Out to print to the console, but you can pass it something else if you need to such as a StringWriter , e.
For something so simple this probably isn't worth it, but if you were writing lots of text, or would be writing it out in a later method call, then passing TextWriter s around can be very handy.
It is generally advisable to define variables as close to their usage as possible, as it makes their purpose more apparent, and reduces the opportunity for misusing them. For example, as Rufus L suggests, don't forward-declare i and k. They are just counters, and shouldn't be available outside of their respective loops. Be consistent with your white-space. For example, it should be obvious at a glance how many for loops a given line of code is inside. With this code as-is, two different Console.
Write commands that actually execute the same number of times are at different indentation levels. There are also inconsistencies with spacing around operators. Personally I prefer the last. Some modern languages don't even have them. Even the technical reviewer of the C specification regrets their inclusion scroll to 3. Avoiding the use of these operators can prevent confusing scenarios. Minimize Scope: This may not seem important, but the variable k is never referenced outside of its for loop.
Declare it there, as you did for p. The same goes for i. Restricting variable references to the minimum possible scope makes the code easier to read. As it stands, I have to hold the variable "k" in my head as I read through the function. If it only existed inside its own loop, I could to safely forget about it.
This may feel inefficient - if you are declaring i many times, are you not allocating memory for it multiple times? The answer is no. The C compiler is smarter than that, so the performance is exactly the same. You'll likely be able to solve your own problem if you think about this. I used some build-in methods e. Repeat and String. PadLeft , not the pure C-language way. The is a number pyramid How are we doing?
Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Making pyramid using c Ask Question. Asked 10 years, 11 months ago. Active 1 year, 6 months ago.
Viewed 66k times. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming.
0コメント