Friday, April 18, 2014

Pattern – C Program

Problem Question


Write a program to produce the following output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Explanation of Problem


This is a simple pattern printing program. A carefully taken set of loops (and nested loop) with well formatted printf() function makes this a very easy program to make. The only aim of this program is to print the above pattern. The above pattern is a Pascal’s Triangle. Each value in Pascal’s Triangle can be computed in two ways:
  1. Sum of two terms from upper row
  2. By combination logic: n! / k! * (n-k)!, where n is the row number, and k represents the kth entry of that row. I am going to use this method in my program.

Code


#include <stdio.h>
/*PATTERN2.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: Code::Block 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 18-04-2014**/
int main()
{
  int i, j, k = 1, iFactorial, jFactorial, dFactorial;
  for (i = 0; i < 5; i++)
  {
    for (j = i - 4; j <= 0; j++)
      printf(" ");
    iFactorial = 1;
    for (k = i; k > 0; k--)
      iFactorial = iFactorial * k;
    for (j = 0; j <= i; j++)
    {
      jFactorial = 1;
      for (k = j; k > 0; k--)
        jFactorial = jFactorial * k;
      dFactorial = 1;
      for (k = i - j; k > 0; k--)
        dFactorial = dFactorial * k;
      printf("%d ", iFactorial/(jFactorial * dFactorial));
    }
    printf("\n");
  }
  system("pause");
  return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

int i, j, k = 1, iFactorial, jFactorial, dFactorial; -> The variable ‘i’ is the loop variable for outer loop, ‘j’ is the loop variable for the inner loop that prints the numbers of the pattern, and the variable ‘k’ is used as the loop variable for the loops that computer the factorial values of the row number(n! which is iFactorial), entry number(k! which is jFacotrial), and the factorial of their difference (n-k)! which is dFactorial in this program.

The outer for loop, for (i = 0; i < 5; i++) , is used to print the pattern in 5 distinct lines. To achieve this, all other printing loops are nested inside this, and at the end of this loop there is a printf() function printing a newline character.
The counter ‘i’ of the outermost loop is used to print a specific number of spaces at the beginning of each line using the first nested loop, for (j = i - 4; j <= 0; j++). It is quite evident that this loop prints, 4 spaces in the first line, 3 in 2nd, 2 in 3rd, 1 in 4th and no space in 5th line.

for (j = 0; j <= i; j++) -> This loop is used to calculate the entries and print them.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Rest of the program is self-explanatory, or can be inferred correctly from the logic explained under the ‘explanation of problem’ heading.

Output(s)



No comments:

Post a Comment

Need help?