7 Replies - 2538 Views - Last Post: 29 March 2012 - 06:38 PM Rate Topic: ***-- 2 Votes

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

C Pattern Printing

Posted 26 March 2012 - 12:09 AM

Well our instructor instructed us to learn all the possible ways to print patterns in C and submit some of them as homework. For example:

*
***
*****
*****
***
*


Well I tried a few and learned a few. In the mean time what I also learned that there are several sites that provide the direct code, thus helping students not to learn the concepts :(!): Several of my friends copied and did their home task. However what I really felt that I am unable to grasp the concepts and I am unable to compute all but some of the bread and butter patters in C and that direct copying would be cheating and it will affect my coding career later. Can you people please help me to learn the concept so that I can write/print any type of pattern given to me? I really want to know how it's done.

Thanks.

This post has been edited by Jeet.in: 26 March 2012 - 12:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C Pattern Printing

#2 NoMoreWords  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 57
  • Joined: 09-March 11

Re: C Pattern Printing

Posted 26 March 2012 - 12:40 AM

It isn't bad if you see the code anywhere on the internet. The bad comes when you just copy it without trying to understand it. Learning from somebody else's work is also a nice way of studying programming.

But still: The idea of what you've given me is simple. The first thing you need to realize is that you're going to use 2 loops. The first beginning from 1 and looping through N when increasing by 2. Or something like that for(int a=1;a<=n;a+=2)(We assume that N is the number of rows you want)
When this loop finishes its job, it will be time to create the second loop which will output the second part of the asterisks. This time it will be decreasing. Since on each line there are 2n-1 asterisks(once again n is the line number) you will need a second loop within the big loop to output. The same goes for the first half. You can use that method there too.
for(int a=n;a>=1;a--)
{
   for(int b=1;b<=2*n-1;b++)cout<<"*";
   cout<<endl;
}


In this case b starts at 1 and prints the first asterisk, then increments to 2 and outputs the second asterisk and so on.
Was This Post Helpful? 1
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: C Pattern Printing

Posted 26 March 2012 - 08:36 AM

Thanks ! Waiting for other replies !
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: C Pattern Printing

Posted 26 March 2012 - 10:40 AM

You don't necessarily need loops. You could use recursion which might get you some bonus points with your teacher/professor. If you don't know what recursion is, then a simple explanation is that a function that either calls itself directly or indirectly is said be reentrant or recursive. In certain situations, it can be better to use recursion instead of loops.

When writing a recursive function, you need to find the "base case". The base case decides what will stop the recursion. Let's do a simple recursive function to figure out the fibonacci sequence. The base case, or "stopping case", for the fibonacci sequence is 0 and 1 because we know the fibonnaci number for both is 1. For any input above 1, the sequence goes: fib(n - 1) + fib(n - 2).
unsigned fibonacci(unsigned n)
{
	if(n == 0 || n == 1)		// base case (stopping case)
		return 1;
	else
		return fibonacci(n - 1) + fibonacci(n - 2);
}


I don't know if you'll see it right away, but the pattern you posted could be done easily with a recursive function. You just need to figure out the stopping case and how to get to the stopping case from the input.

This post has been edited by vividexstance: 26 March 2012 - 10:41 AM

Was This Post Helpful? 1
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: C Pattern Printing

Posted 26 March 2012 - 01:13 PM

View PostJeet.in, on 26 March 2012 - 07:09 AM, said:

However what I really felt that I am unable to grasp the concepts and I am unable to compute all but some of the bread and butter patters in C and that direct copying would be cheating and it will affect my coding career later.
Writing a program for even the most complicated problems is trivial when you understand that problem; in other words, the skill you need is the ability to understand, at the highest level, what is being asked of you, and to dissect it into smaller sub-problems until you find yourself with something which you can manage.

it doesn't matter if your first attempt at solving a problem is inelegant, inefficient or long-winded; in fact, it doesn't even matter if it doesn't work properly either - the important thing is to get something which you can start with (And besides, you will never know for sure if it works properly or not until you have something which you can test!)


If the problem is too difficult to start out with, then start with something simpler - e.g. Can you write a program which prints out multiple characters in a line? or several lines of multiple '*' characters like this?
*******
*******
*******
*******
*******


If you can do that, you can begin to play with it, and start looking at ways in which you can vary the number of '*' characters per line. Maybe your second attempt might do this
*
**
***
****
*****
******


Working this way is not uncommon for tackling "big" problems in the real world - getting something which works but doesn't solve the ultimate problem; it just gives a starting point which may help your understanding and give you something to work with and refactor as you're learning to understand the problem (Learning to solve problems is something which never really stops - not even for people with decades of experience)

If you find it difficult to spot patterns in the problem which you're trying to solve by reading about it on paper then you may have an easier time spotting patterns in your program where you've attempted to solve part of it (i.e. repeated code which might have been written using something akin to copy+paste with a few 'tweaks' here and there). When patterns in code start to emerge, it should become clearer how you can generalise the solution to your problem; solving problems is very often a continual, iterative process of trial/error and improvement.

This post has been edited by Bench: 26 March 2012 - 01:16 PM

Was This Post Helpful? 2
  • +
  • -

#6 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: C Pattern Printing

Posted 26 March 2012 - 02:14 PM

For patterns you might want to start with one "*".

Move up to adding a star "**"

Move up to adding a row:
"**"
"**"

The trick would probably be to use only one * in a variable.
Call that variable each time you want to use a *.

Each new row would of course be a newline.

You can use spaces stored in variables like " ".
To generate space so you can make complex patterns.

" ""*" ""\n"
"*""*""*""\n"
" ""*"" ""\n"
The quotes represent that they were strings.

The actual output would be a diamond:
 *
***
 *


You could also just prinf the pattern with printf containing each patters row.
This would become cumbersome though.

A loop to produce both columns and rows would be very useful instead of writing out rows to print in a loop.

If you use that method you would have to specify columns and rows.
You can check for a character of " ", "*", or "\n" by specifying if a numerical identifier exists.
An array would work.
arr[0] = " "
arr[1] = "*"
arr[2] = "\n"

Then you can write a function for each pattern.
Each pattern function will take arr as input and you can use an else if statement to check which character to use in which column in a row.

Alternatively you can also just have one pattern function.
In it's scope you would have arr.
Use the argument to input an array of numbers to specify the order of the characters with the else if, and you can have what ever pattern you want with just one printf.

With that you would also have to increment the output string of printf along with the input variables.

Or you can just print each character one by one.

This post has been edited by hiddenghost: 26 March 2012 - 02:23 PM

Was This Post Helpful? 1
  • +
  • -

#7 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: C Pattern Printing

Posted 27 March 2012 - 07:52 AM

Thank you so much guys. I am really getting to know what I needed to know !
Was This Post Helpful? 0
  • +
  • -

#8 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: C Pattern Printing

Posted 29 March 2012 - 06:38 PM

View PostBench, on 26 March 2012 - 04:13 PM, said:

... Writing a program for even the most complicated problems is trivial when you understand that problem; in other words, the skill you need is the ability to understand, at the highest level, what is being asked of you, and to dissect it into smaller sub-problems until you find yourself with something which you can manage.

it doesn't matter if your first attempt at solving a problem is inelegant, inefficient or long-winded; in fact, it doesn't even matter if it doesn't work properly either - the important thing is to get something which you can start with (And besides, you will never know for sure if it works properly or not until you have something which you can test!)


If the problem is too difficult to start out with, then start with something simpler - e.g. Can you write a program which prints out multiple characters in a line? or several lines of multiple '*' characters like this?
*******
*******
*******
*******
*******


If you can do that, you can begin to play with it, and start looking at ways in which you can vary the number of '*' characters per line. Maybe your second attempt might do this
*
**
***
****
*****
******


Working this way is not uncommon for tackling "big" problems in the real world - getting something which works but doesn't solve the ultimate problem; it just gives a starting point which may help your understanding and give you something to work with and refactor as you're learning to understand the problem (Learning to solve problems is something which never really stops - not even for people with decades of experience)

If you find it difficult to spot patterns in the problem which you're trying to solve by reading about it on paper then you may have an easier time spotting patterns in your program where you've attempted to solve part of it (i.e. repeated code which might have been written using something akin to copy+paste with a few 'tweaks' here and there). When patterns in code start to emerge, it should become clearer how you can generalise the solution to your problem; solving problems is very often a continual, iterative process of trial/error and improvement.



Great post to pin or add to a faq list ... But this:

Quote

Writing a program for even the most complicated problems is trivial when you understand that problem
... may be a bit of an exaggeration. But I can well attest that writing code does become easier ... and even fun ... when you also have enough experience with some programming language ...and sufficent practice using it ... to solve various programming problems. In an other recent post 'Bench' mentioned the value of a good text book ... the merits of being exposed to 'good' example and well commented code (code like Bench posts here at DIC) ... with step by step demonstrations of coding various problems in progressive difficulty ... I suspect that this can not be over rated :)

This post has been edited by David W: 29 March 2012 - 06:52 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1