Translate

Friday 11 May 2012

C++ Program Structure



Comment:- It is a non executable statement i.e. it doesn’t have any effect on the program. This statement is mainly used to indicate purpose of a program, a function, a block etc. There are 2 types of comments statement:-
                                I.            Single line comment:- Comment is written in 1 line only with 1 set of ‘//’.
example:-  // program to fing sum of
                     // two numbers
                              II.            Double line comment:- Comment can be written in multiple lines.
example:-  /*program to find sum
                       of two numbers*/

Header files:- These are used to invoke library functions such as pow(), sqrt() etc and use the objects cin, cout etc.
      example:- #include <math.h>
          #include “math.h”
The header file written within angular brackets is searched from default directory i.e. the directory created during installation of C++.
The header file written within angular brackets is searched from default directory as well as from the current directory i.e. the directory created by user. Please note that now a days in modern C++ compiler instead of <iostream.h> <iostream> is used.

Main function:- The execution of C++ program starts and ends within the main() function. This function derives statements, library functions, user defined functions etc used in program. In C++ each function is written with return type hence main is generally written as
                                void main()
                                or
                                int main()            /* assuming return 0; is present in main function*/

Declaration:- The variables or constants used in a program should be declared with a datatype.
Example:-            int a,b,c;               const int n=12;

Input statement:- This statement is written with input functions such as scanf(); gets(); cin; etc.
cin is an object defined in header file iostream.h ‘or’ istream.h and hence header file iostream.h ‘or’ istream.h should be included to invoke the object cin.
syntax:-
cin>>a;
>> is called extraction operator.
cin>>a>>b>>c;
The above format is used to input a set of values. Here a,b,c are variables.

Processing statement:-These statement includes assignment statement, conditional statement, statement for calling functions etc.
example:-                 a=3.14*r*r;
                                x=sqrt(y);

Output statement:- These statements display the result on the screen. The function printf(); puts(); and the object cout is used to display results. cout is an object defined in header file iostream.h ‘or’ istream.h and hence header file iostream.h ‘or’ istream.h should be included to invoke the object cout.
 syntax:-  cout<<a;
<< is called insertion operator

Now it’s time for some simple examples.

/*Program to find perimeter and area of rectangle*/
#include <iostream.h>
#include <conio.h>
void main()
{     
       float l,b;
       clrscr();
       cout <<"Enter length and breadth";
       cin>>l>>b;                // l and b are stored in memory
       float a=l*b;
       float p= 2*(l+b);
       cout<<”Area=”<<a<<endl;   //endl is used to send cursor to
       cout<<”Perimeter=”<<p;    // new line or to print “Enter”
       getch();
}



/*Program to read time in second and display in hours, minutes and seconds.*/
#include <iostream.h>
#include <conio.h>
void main()
{     
                  int s,hh,mm,ss;
       clrscr();
       cout<<”Enter time in seconds”;
       cin>>s;
       hh=s/3600;
       mm=(s%3600)/60;     // % is used to find remainder
       ss=(s%3600)%60;
       cout<<”Time is”<<hh<<”hours”<<mm<<”minutes”<<ss<<"sec";         
       getch();
}

Exercise:-
Q. Write a program in C++ to swap two values without using a third variable.
Q. Write a program in C++ to read length, breadth and height of a cuboid and display its volume and surface area.
Q. Write a program in C++ to read 3 sides nd display area of triangle.
Hint area= sqrt(s*(s-a)*(s-b)*(s-c))

No comments:

Post a Comment

Total Pageviews