Sponsored Links

Friday, August 5, 2011

C++ Program Logic Building-Program 1


Let us start learning C++ by understanding an example program. This program is used to
1) input two whole numbers,
2) to calculate their total and
3) to show this total on screen.
The program code ( set of instructions ) is given below in a table and shown in colors according to C++ coding conventions in the figure above:

So let's start with program planning. First we write an algorithm of the given programming problem. This will make writing the program easier:



Q: Write a program to input two whole numbers, calculate and show their total.


  Algorithm: Total of two whole numbers
  1. Start
  2. Include iostream.h
  3. Declare three integer variables a,b and c.
  4. Display a message (prompt) on screen "Enter First Number:"
  5. Input a.
  6. Display a message (prompt) on screen "Enter Second Number:"
  7. Input b.
  8. Calculate c=a+b.
  9. Show a message "toatl=" and value stored in c variable.
  10. Stop


Now here we convert this algorithm into a C++ program.



#include<iostream.h> // include iostream.h header file
int main() // start of main function (that will return an integer value)
{ // start of main function body
int a,b,c; // declare 3 integer variables a,b,c
cout << "Enter First Number:"; //show message to user to enter first number
cin>>a; //input first number
cout<<"Enter Second Number:"; // show message for second number
cin>>b; // enter second number
c=a+b; // add a and b and store answer in c
cout<<"total="<<c; //show a message and answer in c
return 0; // return 0
} // end of main function body

Output Of Program
Enter First Number:10
Enter Second Number:20
total=30



How This Program Works:


1. #include<iostream.h> // include iostream.h header file
 This C++ instruction is divided in two parts
1. Brown part  : this instruction is used to include ( insert ) a header file named iostream.h into source code. This header file is needed because we have used two objects cout and cin. And this file has necessary information about cout and cin. So if we use these objects in our program, we must write this instruction to include iostream.h in our source program.

2. green part : this part is a single line comment. Comments are used to show any remarks about the instruction. Single line Comments are started with //. These comments are ignored by C++ compiler. 

2. int main()
This indicates the start of the main function. The main function is a compulsory function in a C++ program. int tells that main function will return an integre( whole number ) value. Normally 0 is returned to show successful execution of program.

3. { // start of main function body
 Curly bracket or opening braces is used to indicate start of main function body. We write C++ statements  (  instructions  ) in main function body. Each instruction tells the computer to perform a single task. Each instruction end with a semi colon.


4. int a,b,c; // declare 3 integer variables a,b,c
This instruction is used to declare three variable named a, b and c. A variable is a named memory location. A variable is a small place holder in memory to store some data. For example in this program, we are inputting two whole numbers. So we declared two variable a and b of type int. int is a data type that can support manipulating whole numbers like 10, 4, 500 and 67 etc. No fractional part will be involved. When we will add two numbers in a and b variable then we will store its answer in a third variable called c. So we declared three integer variables in this program


5. cout << "Enter First Number:"; //show message to user to enter first number
cout object helps to show some ouput on screen in C ++  programs. This statement is used to show a text on screen that is enclosed in quotation marks. When we place a text in quotation marks after cout<<, this text will be shown on computer screen as it is. So this statement will show "Enter First Number:" on screen

 6. cin>>a; //input first number
 cin object gets some input from the user while program is running and places it into a variable. In above statement number 6, cin>>a; tells the computer to wait until user enters a number from keyboard and presses Enter key. After pressing enter key the number will be placed in variable a.


7. cout<<"Enter Second Number:"; // show message for second number
See explanation of 5.

8. cin>>b; // enter second number 
Take a number from user and place it into a variable named b.

9. c=a+b; // add a and b and store answer in c 
add the value in variable a to the value of variable b and place the answer in variable c.


10. cout<<"total="<<c; //show a message and answer in c
This statement will show the text "total=" and the value of c. For example, suppose that we enter 10 in a and 20 in b, so there will be 30 in variable c. Now this staement will show the message as:
total=30

11.  return 0; // return 0 
Normally we end the main function with returning 0 that shows the successful execution of the program.

12. } // end of main function body
This closing braces show the end of main function body and end of the C++ program too.

13. After the program a sample run of the program is shown as output of the program in table and in figure above.