Monday, February 4, 2013

Escape Sequences in c++

#include <cstdio>
#include <iostream>

int main()
{
    // escape sequence covered - \' \" \? \\ \0 \a \b \n \r \t
    printf("\n 1. sandeep \t dheeraj\n");

    printf("\n 2. sandeep \v dheeraj\n");
 
    printf("\n 3. sandeep \r dheeraj\n");
 
    printf("\n 4. sandeep \a\a\a dheeraj\n");
 
    printf("\n 5. sandeep\b dhe\0eraj extra\n");
 
    printf("\n 6. \'sandeep\' \\ \n");  // used \' \" \\
 
    printf("\n 7. Ram? \n");
 
    printf("\n 8. Ram\? \n");    //why we use \? this escape sequence
 
    printf("\n 9. \"dheeraj\" \n");
}

OUTPUT:

C++ 'in class variable' and 'static variable' initialization

#include <iostream>
using namespace std;

class Demo

{
  public:
  int a  = 1;
    // static int b=2;  //this gives error
  static int b;
};

int Demo::b=2;  //initialize the static variable of class

int main()
{
    Demo d;
    cout << "\n value of variable a: " << d.a << endl;
    cout << "\n value of static variable b: " << Demo::b << endl;
}


OUTPUT:

Simple Program to find out Prime Number

//c program to find prime number
#include<stdio.h>
#include<conio.h>

int main()
{
    int num, count=0, i;
    printf ( "enter your number:" );
    scanf ( "%d" ,&num);
   
        for(i=1; i<=num; i++)
        {
            if (num%i==0)
            {
                count++;
            }
        }
       
        if (count <= 2)
        {
            printf ("number is prime");
        }
       
        else
        {
            printf ("number is not prime");
        }
    return 0;
}

----------------------------------------------------------------------------------------------

//c++ program to find prime number
#include <iostream>
using namespace std;

int main()
{
    int num, count=0, i;
    cout << "\n enter your number: ";
    cin >> num;
   
    if (num==1)
    {
        cout << "\n 1 is a prime number";
    }
   
    else
    {
        for(i=1; i<=num; i++)
        {
            if (num%i==0)
            {
                count++;
            }
        }
       
        if (count==2)
        {
            cout << "number is prime";
        }
       
        else
        {
            cout << "number is not prime";
        }
    }
    return 0;
}

Sunday, February 3, 2013

Average of 5 Number using vector in c++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> grades;   //declaration of vector
    int grade, total;
    double average;
    total = 0;
   
    for(int i=1; i<=5; ++i)        //Taking the input from user
    {
        cout << "Enter a grade: ";
        cin >> grade;
        grades.push_back(grade);
    }
   
    for(int i = 0; i < grades.size(); ++i)
    {
        //cout << grades[i] << " ";
        total += grades[i];
    }
    
    average = total / grades.size();
    cout << "the average is " << average << endl;
    
    return 0;
}


OUTPUT:

A vector<int> (meaning 'a vector of ints') in much the same way as you would use an ordinary C array, except that vector eliminates the chore of managing dynamic memory allocation by hand.

vector<int> v(3); // Declare a vector (of size 3 initially) of ints
     v[0] = 7;
     v[1] = v[0] + 3;
     v[2] = v[0] + v[1]; // v[0] == 7, v[1] == 10, v[2] == 17
     v.push_back(13); // add another element - the vector has to expand

Note the use of the push_back function - adding elements this way will make the container expand.

Remember that:

1. vector elements can move! insert, for instance, changes its 1st argument.
2. vector doesn't have arithmetic operations: valarray (which is described
later) does. p.51
3. vector doesn't have range-checking by default.