Thursday, April 17, 2014

Pattern – C Program

Problem Question


Write a program to produce the following output:


   1
  2 3
 4 5 6
7 8 9 10


Explanation of Problem


The problem needs us to make a program that would print the above pattern. It is a pretty simple program, with a simple logic of printing a number of spaces before each line, and a single space between each number.

Code


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

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; -> Variables ‘i’ and ‘j’ are loop variables. Variable ‘k’ is used to print the numbers which we require in the pattern, which is why I initialised it to 1 and post-increment it in the printf() function.

The outer for loop, for (i = 1; i <= 4; i++) is used because I wish to print 4 lines in my pattern. At the end of this loop, I use printf() to print a new line at each iteration.

The first inner for loop, for (j = i - 3; j <= 0; j++) is used to print the spaces at the beginning of each line in the pattern. It is quite evident from the question that we need 3 spaces before 1(beginning of first line), 2 before 2(beginning of second line), 1 before 4(beginning of third line), and no space before 7(beginning of fourth line). Thus the loop refers to the value of ‘i’ of the the outer loop to achieve this.

The final inner for loop, for (j = i; j > 0; j--) does the printing job of the numbers in the pattern. It prints the numbers (‘i’ numbers per line) of each line, and a space after each number.

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.

Output(s)



No comments:

Post a Comment

Need help?