Thursday, 6 June 2013

Some more C++ Sample programs



Sample program which takes in variables and input statements

We will read two numbers from the keyboard and display the average on the screen. A sample C++ program for this purpose is given below


C++ Program to calculate the average of two numbers

#include<iostream>

using namespace std;

int main()
{

          float number1, number2, sum , average;        //Declaring the variables as float datatype
          cout<<"Enter the two numbers : ";                // String displayed on screen asking for two inputs
          cin>>number1;                                              //Reading Input 1 from the keyboard
          cin>>number2;                                              //Reading Input 2 from the keyboard

         sum=number1 + number2;                            //Calculating the sum of two numbers
         average = sum/2;                                           //Calculating the average of two numbers

         cout<<"Sum = " <<sum<<"\n";                    //Displaying the sum
         cout<<"Average of two numbers = "<<average<<"\n";      //Displaying the result of the average

         return 0;
}
        

The output of this program would be something like this :-

Output of the above program

 

Variables

This program uses four variables number1, number2, sum and average. They are declared as float type by the statement
 
  float number1, number2, sum, average;
 
All variables must be declared before they are used in a program.
 

Input Operator

The statement - cin>>number1;

is an input statement and causes the program to wait for the user to type in a number. The number keyed in is placed in the variable number1. The identifier cin is a predefined object in C++ that correspons to the standard input stream.

The operator >> is known as extraction or get from operator. It extracts or takes the value from the keyboard and assigns it to the variable on its right.

Cascading of Input/Output Operators

We have used the insertion operator <<repeatedly in the last two statements for printing results.

The statement
cout<<"Sum ="<<sum<<<<"\n";

first sends the string "Sum=" to cout and then sends the value of sum. Finally it sends the newline character so that the next output wil be in the new line. The multiple use of << in one statement is called cascading. When cascading an output operator , we should ensure necessary blank spaces between different items. Using the cascading technique the last two statements can be combined as follows :-

cout<<"Sum="<<sum<<"\n"<<"Average= "<<average<<"\n";

We can cascade the input operator as well

cin>>number1>>number2;

Note :-  The values are assigned from left to right. That is if we key in two values, say 10 and 20, then 10 will be assigned to number1 and 20 will be assigned to number2.


An example with class

One of the major functions of C++ is classes. They bind the functions which operate on them. Like structure in C , classes are user defined data types.

The following program shows the use of a class in a C++ program.

#include<iostream>

using namespace std;

class person
{
               char name[30];
               int age;
              public :
                         void getdata(void);
                         void display(void);
};

void person::getdata(void)
{
            cout<<"Enter the name of the person: ";
            cin>>name;
            cout<<"\nEnter age of the person: ";
            cin>>age;
}

void person::display(void)
{
            cout<<"\nName = "<<name<<"\n";
            cout<<"\nAge= "<<age<<"\n";
}

int main()
{
             person p;
             p.getdata();
             p.display();
             return 0;
}


Analysis of the program

This program defines person as a new data of type class. The class person includes two basic data type items and two functions to operate on that data. These functions are called member functions. The main program uses person to declare variables of its type. As said earlier class variables are known as objects. Here , p is an object of type person. Class objects are used to invoke the functions defined in that class.

Note :- cin can read only one word and therefore we cannot uses blank spaces to write a full name.

Assignments on the concepts learnt today :-

Q1. Write a program to display the following output using a single cout statement.

  • Maths = 90
  • Physics = 77
  • Chemistry = 80.
Q2. Write a program to read two numbers from keyboard and display the larger value on the screen.

Q3. Write a program to read the values of a,b and c and display the value of x, where

            x = a/b-c
Test your program for the following set of values :-

i) a=250, b=85, c=25;
ii) a=300, b=70, c=70.

Q4. Write a C++ Program that will ask for a temperature in Fahrenheit and diplay its corresponding temperature in Celsius.

Please try doing all the programs yourself if u face any problem do use the comment section.

Answers for all these problems will be posted tommorow.

 

No comments:

Post a Comment

Thank you for commenting.