Get Email Subscription

Enter your email address:

Delivered by FeedBurner

Showing posts with label Outof Syllabus. Show all posts
Showing posts with label Outof Syllabus. Show all posts

Wednesday, April 21, 2010

This program reverses an integer value. (C++)


This program reverses an integer value. (C++)
 
# include < iostream.h> 
# include < conio.h> 
 
 
void revfunction(int num); 
 
 
void main() 
{
 
int nnum;
 
cout < < "this program will give the inverse of any number:"< < "\t";
 
cin>>nnum; 
 
revfunction(nnum); 
 
cout< < "\n"< < "Press a Key..."< < "\t";
 
getch();
 
}
 
 
void revfunction(int num)
 
{
 
int p=1,f,i;
 
while (p!=0)
 
{
 
 
p=num/10; 
 f=p*10; 
 i=num-f;
num=p; 
 cout< < i; 
 
}
 
 
}

read more "This program reverses an integer value. (C++)"

This program is used to play tic tac toe game (C++)


This program is used to play tic tac toe game (C++)
 
#include< stdio.h>
#include< conio.h>
#include< stdlib.h>
 
int board[10] = {2,2,2,2,2,2,2,2,2,2};
int turn = 1,flag = 0;
int player,comp;
 
void menu();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void player_first();
void put_X_O(char ch,int pos);
 
 
main()
{
        clrscr();
        _setcursortype(_NOCURSOR);
        menu();
        getch();
        return(0);
}
 
void menu()
{
        int choice;
        printf("\n--------MENU--------");
        printf("\n1 : Play with X");
        printf("\n2 : Play with O");
        printf("\n3 : Exit");
        printf("\nEnter your choice:>");
        scanf("%d",&choice);
 
        turn = 1;
        switch (choice)
        {
               case 1:
                       player = 1;
                       comp = 0;
                       player_first();
                       break;
               case 2:
                       player = 0;
                       comp = 1;
                       start_game();
                       break;
               case 3:
                       exit(0);
               default:
                       menu();
        }
}
 
int make2()
{
        if(board[5] == 2)
               return 5;
        if(board[2] == 2)
               return 2;
        if(board[4] == 2)
               return 4;
        if(board[6] == 2)
               return 6;
        if(board[8] == 2)
               return 8;
        return 0;
}
 
int make4()
{
        if(board[1] == 2)
               return 1;
        if(board[3] == 2)
               return 3;
        if(board[7] == 2)
               return 7;
        if(board[9] == 2)
               return 9;
        return 0;
}
 
int posswin(int p)
{
// p==1 then X   p==0  then  O
        int i;
        int check_val,pos;
 
        if(p == 1)
               check_val = 18;
        else
               check_val = 50;
 
        i = 1;
        while(i< =9)//row check
        {
               if(board[i] * board[i+1] * board[i+2] == check_val)
               {
                       if(board[i] == 2)
                               return i;
                       if(board[i+1] == 2)
                               return i+1;
                       if(board[i+2] == 2)
                               return i+2;
               }
               i+=3;
        }
 
        i = 1;
        while(i< =3)//column check
        {
               if(board[i] * board[i+3] * board[i+6] == check_val)
               {
                       if(board[i] == 2)
                               return i;
                       if(board[i+3] == 2)
                               return i+3;
                       if(board[i+6] == 2)
                               return i+6;
               }
               i++;
        }
 
        if(board[1] * board[5] * board[9] == check_val)
        {
               if(board[1] == 2)
                       return 1;
               if(board[5] == 2)
                       return 5;
               if(board[9] == 2)
                       return 9;
        }
 
        if(board[3] * board[5] * board[7] == check_val)
        {
               if(board[3] == 2)
                       return 3;
               if(board[5] == 2)
                       return 5;
               if(board[7] == 2)
                       return 7;
        }
        return 0;
}
 
void go(int n)
{
        if(turn % 2)
               board[n] = 3;
        else
               board[n] = 5;
        turn++;
}
 
void player_first()
{
        int pos;
 
        check_draw();
        draw_board();
        gotoxy(30,18);
        printf("Your Turn :> ");
        scanf("%d",&pos);
 
        if(board[pos] != 2)
               player_first();
 
        if(pos == posswin(player))
        {
               go(pos);
               draw_board();
               gotoxy(30,20);
               textcolor(128+RED);
               cprintf("Player Wins");
               getch();
               exit(0);
        }
 
        go(pos);
        draw_board();
        start_game();
}
 
void start_game()
{
        // p==1 then X   p==0  then  O
        if(posswin(comp))
        {
               go(posswin(comp));
               flag = 1;
        }
        else
        if(posswin(player))
               go(posswin(player));
        else
        if(make2())
               go(make2());
        else
               go(make4());
        draw_board();
 
        if(flag)
        {
               gotoxy(30,20);
               textcolor(128+RED);
               cprintf("Computer wins");
               getch();
        }
        else
               player_first();
}
 
void check_draw()
{
        if(turn > 9)
        {
               gotoxy(30,20);
               textcolor(128+RED);
               cprintf("Game Draw");
               getch();
               exit(0);
        }
}
 
void draw_board()
{
        int j;
 
        for(j=9;j< 17;j++)
        {
               gotoxy(35,j);
               printf("|       |");
        }
        gotoxy(28,11);
        printf("-----------------------");
        gotoxy(28,14);
        printf("-----------------------");
 
        for(j=1;j< 10;j++)
        {
               if(board[j] == 3)
                       put_X_O('X',j);
               else
               if(board[j] == 5)
                       put_X_O('O',j);
        }
}
 
void put_X_O(char ch,int pos)
{
        int m;
        int x = 31, y = 10;
 
        m = pos;
 
        if(m > 3)
        {
               while(m > 3)
               {
                       y += 3;
                       m -= 3;
               }
        }
        if(pos % 3 == 0)
               x += 16;
        else
        {
               pos %= 3;
               pos--;
               while(pos)
               {
                       x+=8;
                       pos--;
               }
        }
        gotoxy(x,y);
        printf("%c",ch);
}
read more "This program is used to play tic tac toe game (C++)"

This program is used to overload operators in c++


This program is used to overload operators in c++
 
#include< iostream.h>
#include< conio.h>
class list
{
int count;
public:
//constructor for initializing the variable count to 0
list()
{
count=0;
}
//parametrized constructor for initializing the variable count
list(int i)
{
count=i;
}
// operator ++ overloaded
void operator ++()
{
++count;
}
//Function for displaying the value of count
void showdata()
{
cout< < count;
}
};
void main()
{
clrscr();
list l;
cout< < endl< < "C= ";
l.showdata();
//calling the overloaded operator
++l;
cout< < endl< < "C = ";
l.showdata();
}
read more "This program is used to overload operators in c++"

This program is used to demonstrate multiple inheritance. (C++)


This program is used to demonstrate multiple inheritance. (C++)
 
Though the program may not be as important the concept of multiple inheritence is. This concept should be very well understood by the students as this will be helpfull in the long run. 
 
#include< iostream.h>
#include< conio.h>
 
 
class item
{
private:
char title[20];
float price;
public:
 
void getdata()
{
cout< < endl< < "Enter name and cost ::";
cin>>title>>cost;
}
 
 
void displaydata()
{
cout< < endl< < "name and cost ::";
>cout< < title< < "\t"< < cost;
}
};
 
 
class sales
{
private:
float salesfig[3];
public:
void getdata()
{
cout< < endl< < "Enter sales figure for three months ::";
> for(inti=0;i< 3;i++)
cin>> salesfig[i];
}
 
 
void displaydata()
{
cout< < endl< < "Sales figure for three months ::";
> for(inti=0;i< 3;i++)
cout< < salesfig[i]< < "\t";
> }
};
 
 
class hwitem : private item,private sales
{
private:
char category[10];
char oem[10];
public:
 
void getdata()
{
 
item:: getdata();
cout< < endl< < "Enter category and Oem ::";
cin>>category>>oem;
 
sales::getdata();
}
 
 
void displaydata()
{
 
item:: displaydata();
cout< < endl< < "Category and Oem ::";
cout< < category< < "\t"< < oem;
 
sales::displaydata();
}
};
 
 
class switem:private item, private sales
{
private:
char category[10];
char os[10];
public:
void getdata()
{
item::getdata();
cout< < endl< < "Enter the Category and Os ::";
>cin>>category>>os;
sales::getdata();
}
void displaydata()
{
item::displaydata();
cout< < endl< < "Category and Os ::";
cout< < category< < "\t"< < os;
sales::displaydata();
}
};
 
void main()
{
clrscr();
 
 
hwitem h1,h2;
h1.getdata();
h1.displaydata();
h2.getdata();
h2.displaydata();
 
 
switem s1,s2;
s1.getdata();
s1.displaydata();
s2.getdata();
s2.displaydata();
}
 
 
 
 

read more "This program is used to demonstrate multiple inheritance. (C++)"

This program is used to carry out binary search. (C++)


#include< iostream.h>
#include< conio.h>
int binary_search(int array[],int value,int size)
{
int found =0;
inthigh=size,low=0,mid;
mid=(high+low)/2;
cout< < "\n\nLooking for "< < value< < endl;
while((!found) && (high>=low))
{
cout< < "Low "< < low< < " Mid "< < mid< < " High "< < high< < endl;
if (value==array[mid])
found=1;
else if(value< array[mid])
high=mid-1;
else low=mid+1;
mid=(high+low)/2;
}
return((found)?mid: -1);
}
void main(void)
{
clrscr();
int array[100],i;
for(i=0;i< 100;i++)
array[i]=i+20;
cout< < "Result of search "< < binary_search(array,15,100)< < endl;
cout< < "Result of search "< < binary_search(array,23,100)< < endl;
}

read more "This program is used to carry out binary search. (C++)"

This program is used for Selection Sort. (C++)


This program is used for Selection Sort. (C++)
 
 
#include< stdlib.h>
#include< iostream.h>
#include< conio.h>
#define NUM_ITEMS 10
void selectionSort(int numbers[], int array_size);
 
int numbers[NUM_ITEMS];
 
int main()
{
clrscr();
int i;
//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand() %100;
//display the unsorted items 
cout< < "Unsorted elements are::\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
cout< < endl;
//perform selection sort on array
selectionSort(numbers, NUM_ITEMS);
cout< < "Sorted elements are::\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
}
void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
 
 

read more "This program is used for Selection Sort. (C++)"

This program is used for Quick sort. (C++)


This program is used for Quick sort
 
#include< iostream.h>
#include< conio.h>
#include< stdlib.h>
void quick_sort(int array[],int first, int last)
{
int temp,low, high, pivot;
low=first;
high=last;
pivot=array[(first+last)/2];
do{
while(array[low]< pivot)
low++;
while(array[high]>pivot)
high--;
if(low< =high)
{
temp=array[low];
array[low++]=array[high];
array[high--]=temp;
}
} while(low< =high);
if(first< high)
quick_sort(array,first,high);
if(low< last)
quick_sort(array,low,last);
}
void main(void)
{
clrscr();
int values[100],i;
cout< < "\n Unsorted list is as follows \n";
for(i=0;i< 20;i++)
{
values[i]=rand() %100;
cout< < " "< < values[i];
}
quick_sort(values,0,19);
cout< < "\nSorted list as follows \n";
for(i=0;i< 20;i++)
cout< < " "< < values[i];
} 

read more "This program is used for Quick sort. (C++)"

This program is used for Matrix Multiplication. (C++)


This program is used for Matrix Multiplication. (C++)
 
#include< iostream.h>
#include< conio.h>
#include< iomanip.h> 
 
 
 
struct matrix
{
int arr[3][3];// Double dimensional array 
};
matrix operator * (matrix a,matrix b); //Operator overloding
 
// function declaration for printing matrix
void matprint(matrix p);
 
void main()
{
clrscr();
// Declaring matrix values 
matrix a={ 1,1,3,
4,5,7,
2,9,6
};
matrix b={ 2,3,8,
7,4,1,
1,5,6
};
 
matrix c,d;
//overloaded operator will be called 
c=a*b;
//display the matrix
matprint(c);
}
//overloaded operator defination for matrix Multiplication
 
matrix operator *(matrix a, matrix b)
{
matrix c;
int i, j,k;
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
c.arr[i][j]=0;
for(k=0;k< 3;k++)
c.arr[i][j]+=a.arr[i][j]*b.arr[i][j];
}
}
return c;
}
 
//displaying matrix 
void matprint(matrix p)
{
int i,j;
cout< < endl< < endl;
for(i=0;i< 3;i++)
{ cout< < endl;
for(j=0;j< 3;j++)
cout< < setw(5)< < p.arr[i][j];
}
}
 
 
 
 
--------------------------------------------------------------------------------
 
 
The output of the programe will be ::
 
 
12      22      27
50      67      79      
73      72      61

read more "This program is used for Matrix Multiplication. (C++)"

This program is used for Insertion sort. (C++)


 
#include< iostream.h>
#include< conio.h>
 
int key;
int insert(int*);
void display(int*, int);
int insert(int list[])
{
int i=0;
int j,k;
cout< < "element to be inserted break condition is -0 : ";
cin>>key;
cout< < "Selected element is "< < key;
while(key!=-0)
{
k=i-1;
while((key< list[k] &&(k>=0)))
{
list[k+1]=list[k];
--k;
}
 
 
 
list[k+1]=key;
cout< < "List after inserting an element";
for(j=0;j< =i;j++)
cout< <" "< < list[j];
cout< <" \nElement to be inserted break condition is -0 ::";
cin>>key;
cout< <"\nSelected Element is : "< < key;
i++;
}
return i;
}
void display(int list[],int n)
{
int j;
cout< < "\n Fianl sorted list is as follows :\n";
for(j=0;j< n;j++)
cout< < " "< < list[j];
}
void main()
{
clrscr();
int list[200];
int n;
n=insert(list);
display(list,n);
}
 
 
 
 
--------------------------------------------------------------------------------
 
 
The output of the program ::
 
 
element to be inserted break condition is -0 : 12
Selected element is 12List after inserting an element 12
Element to be inserted break condition is -0::23
 
Selected Element is : 23List after inserting an element 12 23
Element to be inserted break condition is -0::2
 
Selected Element is : 2List after inserting an element 2 12 23
Element to be inserted break condition is -0::21
 
Selected Element is : 21List after inserting an element 2 12 21 23
Element to be inserted break condition is -0::31
 
Selected Element is : 31List after inserting an element 2 12 21 23 31
Element to be inserted break condition is -0::10
 
Selected Element is : 10List after inserting an element 2 10 12 21 23 31
Element to be inserted break condition is -0::0
 
Selected Element is : 0
Fianl sorted list is as follows :
2 10 12 21 23 31
 
 

read more "This program is used for Insertion sort. (C++)"

This program is used for heap sort. (C++)


#include < stdlib.h>
#include < iostream.h>
#include < conio.h>
#define NUM_ITEMS 10
 
 
void heapSort(int numbers[], int array_size);
 
void siftDown(int numbers[], int root, int bottom);
int numbers[NUM_ITEMS];
 
int main()
{
clrscr();
int i;
 
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand() %100 ;
cout< < "\n\nUnsorted elements are as follows::\n\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
 
heapSort(numbers, NUM_ITEMS);
cout< < "\n\nSorted elements are as follows::\n\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< <" "< < numbers[i];
}
 
 
void heapSort(int numbers[], int array_size)
{
int i, temp;
for (i = (array_size / 2)-1; i > = 0; i--)
siftDown(numbers, i, array_size);
for (i = array_size-1; i > = 1; i--)
{
temp = numbers[0];
numbers[0] = numbers[i];
numbers[i] = temp;
siftDown(numbers, 0, i-1);
}
}
 
 
void siftDown(int numbers[], int root, int bottom)
{
int done, maxChild, temp;
done = 0;
while ((root*2 < = bottom) && (!done))
{
if (root*2 == bottom)
maxChild = root * 2;
else if (numbers[root * 2] > numbers[root * 2 + 1])
maxChild = root * 2;
else
maxChild = root * 2 + 1;
if (numbers[root] < numbers[maxChild])
{
temp = numbers[root];
numbers[root] = numbers[maxChild];
numbers[maxChild] = temp;
root = maxChild;
}
else
done = 1;
}
}
 
 

read more "This program is used for heap sort. (C++)"

This program is used for Bubble sort. (C++)


 
 
#include< iostream.h>
#include< conio.h>
#include< stdlib.h>
void bubble_sort(int array[],int size)
{
int temp,i,j;
for(i=0;i< size;i++)
for(j=0;j< size-i-1;j++)
if(array[j]< array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
void main(void)
{
clrscr();
int values[30],i;
cout< < "Unsorted list is as follows\n";
for(i=0;i< 10;i++)
{
values[i]=rand()%100;
cout< < values[i]< < " ";
}
bubble_sort(values,10);
cout< < "\nSorted list is as follows\n";
for(i=0;i< 10;i++)
cout< < values[i]< < " ";
}

read more "This program is used for Bubble sort. (C++)"

This program is uesd for Shell Sort. (C++)


 
#include < stdlib.h>
#include < stdio.h>
#include< conio.h>
#include < iostream.h>
#define NUM_ITEMS 15
 
void shellSort(int numbers[], int array_size);
int numbers[NUM_ITEMS];
int main()
{
clrscr();
int i;
//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand() %100;
cout< < "\nUnsorted list is as follows\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
//perform shell sort on array
shellSort(numbers, NUM_ITEMS);
cout< < "\n\nSorted list is as follows:\n";
for (i = 0; i < NUM_ITEMS; i++)
cout< < " "< < numbers[i];
}
void shellSort(int numbers[], int array_size)
{
int i, j, increment, temp;
increment = 3;
while (increment > 0)
{
for (i= 0;i < array_size; i++)
{
j = i;
temp = numbers[i];
while ((j >= increment) && (numbers[j-increment] > temp))
{
numbers[j] = numbers[j - increment];
j = j - increment;
}
numbers[j] = temp;
}
if (increment/2 != 0)
increment = increment/2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}
 
read more "This program is uesd for Shell Sort. (C++)"

This program generates fibonacci series in a recursive manner. (C++)


This program generates fibonacci series in a recursive manner. (C++)
 
This program is very famous among interviewers and will be asked in technical interviews. 
 
 
 
#include< iostream.h>
#include< conio.h>
long fibo_number(int );
long fibo_number(int n)
{
int r;
if((n==1)||(n==0))
return 0;
else
if(n==2)
return 1;
else
return(fibo_number(n-1)+ fibo_number(n-2));
}
/* main function */
main()
{
int n;
int i=0;
cout< < endl< < " Input the number for fibonacci series, you want to generate:";
cin>> n;
cout< < "\n Fibonacci Series is as follows: \n";
while(i< n)
{
++i;
cout< < " "< < fibo_number(i));
}
}
 
 
 
 
--------------------------------------------------------------------------------
 
 
The output of the program:
 
 
Input the number for fibonacci series, you want to generate:12
 
Fibonacci Series is as follows:
0 1 1 2 3 5 8 13 

read more "This program generates fibonacci series in a recursive manner. (C++)"

This program demonstrates the use of copy constructor. (C++)


This program demonstrates the use of copy constructor. (C++)
 
This is again a very important interview question. Understand the concept very well.
 
#include< iostream.h>
#include< conio.h>
class rect
{
int area;
float len,width;
public:
rect()
{
}
rect(float ll,float wd)
{
len=ll;
width=wd;
area=ll*wd;
}
 
rect operator =(rect &r)
{
cout< < endl< < "Assignment operator invoked";
> area=r.area;
len=r.len;
width=r.width;
return rect(len,width);
}
 
rect(rect &r)
{
cout< < endl< < "Copy constructor invoked";
> len=r.len;
width=r.width;
area=r.len*r.width;
}
void display()
{
cout< < endl< < < "Area= "< < area;
>cout< < endl< < endl< <" Length= "< < len;
> cout< < endl< < endl< < "Width= "< < width;
}
};
void main()
{
clrscr();
rect r1(4.5,10);
rect r2;
r2=r1;
 
rect r3=r1;
r1.display();
r2.display();
r3.display();
}
read more "This program demonstrates the use of copy constructor. (C++)"

This program demonstrates the use of constructor and destructor. (C++)


This program demonstrates the use of constructor and destructor
 
This is a very common interview question so please understand this concept very well.
 
 
#include< iostream.h>
#include< conio.h>
#include< stdio.h>
class student
{
private:
char *name;
char *sub;
int marks;
public:
student()//constructor( same name as class)
{
cout< < "Inside the constructor"< < endl;
// allocate the space in the memory.
name=new char[20];
sub=new char[20];
}
void input();
void output();
~student()//destructor(same name with tilde)
{
cout< < endl< < "Inside the distructor"< < endl;
//frees the allocated space
delete name;
delete sub;
}
};
main()
{
clrscr();
student st;
st.input();
st.output();
}
void student::input()
{
cout< < "Enter the name:: ";
gets(name);
cout< < endl< < "Enter the Subject:: ";
gets(sub);
cout< < endl< < "Enter the marks:: ";
cin>>marks;
}
void student::output()
{
cout< < "Name:: "< < name< < endl;
cout< < "Subject:: "< < sub< < endl;
cout< < "Marks:: "< < marks;
}
read more "This program demonstrates the use of constructor and destructor. (C++)"

About This Blog

Lorem Ipsum

  © Blogger templates Newspaper III by Ourblogtemplates.com 2008

Back to TOP