Saturday 23 March 2013

Lets try one.

1. Write a program to add two no. 3 and 37.

The most basic of program is adding two no.'s when the no.'s are given.Here the numbers are 3 and 37. So, the result should show 40 as its answer. So, lets start...

#include<stdio.h>                          /*invoking first header file(compulsory)*/
#include<conio.h>                         /*invoking second header file(compulsory if you want to see the output)*/
main()                                                /*to write the logic of adding*/
{                                                        /*for the body of the logic of the main() function*/
int a=3,b=37,c;                                   /* or taking the variables and supplying some with given values*/
c=a+b;                                                /*for carrying out the sum*/
printf("The sum of 3 and 37 is: %d",c); /*for the printing of result*/
getch();                                                /*for getting the output to be displayed*/
}                                                          /*ending the epic addition procedure*/

This is just a small program of how to add two no. The basic step is here and gradually the level of programming gets tougher. The best and easiest way to understand a program is to understand the logic because most programs have common initial and final statements. The logic changes just!

Common functions

The following are some of the most common and important functions in basic understanding of C programming :-

1. main():- One of the most important functions of C programming. It is a standard function which deals with the main logic of the program. Generally all the main operations which are to be done for the module are done here.The syntax of the function is as follows:

                          main()

2. scanf() :- Another important function which is used for taking inputs from the user. The syntax of the function is as follows:
       
                           scanf("%<format string>",&<variable>);

               The format string is ' % ' followed by a letter which specifies the type of variable to be used. Eg:          %c specifies character, %d specifies integer type,etc.

3. printf() :- Another important function which is used for displaying outputs on the screen. The syntax of the function is as follows:
       
                           printf("%<format string>",<variable>);

4. getch() :- This function is used to stop the program flow at a point where the user has to press a key to continue. Simply, it forces the user to input any "keyboard letter" which is not visible at that time.The syntax of the function is as follows:

                            getch();

5. clrscr() :- This function has to be activated in Turbo C. It has the function of clearing the screen for the next program to implement.The syntax of the function is as follows:

                             clrscr();


Tuesday 19 March 2013

Dry Run

The most important thing before going into programming is one thing: The dry running of programs..!! Every program, after you have created or you are seeing someone else's creation, to understand the logic behind the program,try to dry run it!

Now, you all must be wondering what dry run is...

DRY RUN is not being dry and running... but analyzing every step of the program, say for example, you reach a code statement such as:

..
..
int c=0,i;
for(i=0;i<5;i++)
{
printf("CP ");
}
..
..

This code prints "CP CP CP CP CP ". How did i get it without running in Dev C++ or Turbo C? Its all dry running..

To understand the dry running of this module(a part of programming code), we go step by step:
1. we see that initialization occurs of c as 0 i.e. c has an initial value of 0.

2. i has got no initial value at first. So we assume i has garbage value(default value always stored in any variable whose value can be got by printing it without any initialisation ... try it....).

3. when it comes to loop, i gets a value of 0 and a condition that i should be less than 5 and increment of i by 1(a++ increments only when a next gets operated and ++a increments then and there itself).

4.Now entering into the loop, we find "CP " being printed once and the exiting.

5. After exiting the first time, looping statement goes to the next value of increment which makes i =1 and increment is done for next time it gets the value.

6. Similar thing goes on till i=4 and on completion of the i=4 loop, when it comes to the looping statement again, i becomes 5 and condition kicks in.

7. The program halts and gets out of the loop.


This is how a dry run is carried out. every program has to be done dry-run for improvement in the logic.

Monday 18 March 2013

Learning part-1

For the introduction we have to learn about the various "things" used in a program.... For a sample, we take a program with the following question:

Write a program (W.A.P) to print the no 31.

The program question has the following code:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("31");
getch();
}

output:

31

Now for the overall analysis:

# :- is known as 'preprocessor directives' and is used for "including" the header files for the code to be used in                                                                                                                             the program.

include :- it is used for taking in the header files used in the code.

stdio.h :- standard input output library..... a header file with many functions but most prominent the input output functions

conio.h :- console input output library..... another header file with many functions... mostly used are getch() and clrscr() {unknown for now... you will know later.. patience people}

void :- as the name suggests represents nothing... don't be confused,people ..... "nothing" in the sense- nothing to return to the main function - main() which brings us to

main() :- a standard function representing the main part of a program.

clrscr() :- a function to clear the screen for the next time output displaying.

printf() :- the function to print something.

getch() :-function accepting character and appearing to stop the screen until any value taken.


A simple program like this has so many things to be understood... think how difficult it is to make program codes for games like Dave....

Many "things" such as functions and return types, etc will be understood gradually....

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

Introduction

Hi, i am Suraj De... A normal student of Computer Science branch in National Institute of Technology, Agartala. And have a average knowledge of Computer programming. I would like to share the knowledge with the people who read my blog. The programs are to be run and compiled in either Dev C++ or  Turbo C  for the programs which i will be dealing with in the recent time because the language is C. There are some minor differences in between Turbo C and Dev C++. Functions like clrscr(),getch(), etc have applications in one and not in other. I will be using both as i go on and its on you to realize which one is suitable. I insist on running the programs cause that way the understanding of the programs increase.