2. ACKNOWLEDGEMENT
I would like to convey my heartful thanks to
……………………… (Computer Science) who
always gave valuable suggestions & guidance for
completion of my project.
He helped me to understand & remember
important details of the project. My project has
been a success only because of his guidance.
I am especially indented & I am also beholden to
my friends. And finally I thank to the members of
my family for their support & encouragement.
3. CERTIFICATE
This is to certify that ………………. of class
XII of ……………PUBLIC SENIOR
SECONDARY SCHOOL , …………… has
completed his project under my supervision. He
has taken proper care & shown sincerity in
completion of this project.
I certify that this project is up to my
expectation & as per the guideline issued by
CBSE.
……………………….
(Computer Science faculty )
4. INDEX
S.NO. PROGRAMS SIGNATURE
1 C++ Program - Find Largest Element in Array
2 C++ Program to accept the 10 numbers in an array and search array
using - Linear Search
3 C++ Program to accept the numbers in an array and Reverse Array
4 C++ Program to accept the numbers in an array and Insert new Element
in Array
5 C++ Program to accept the numbers in an array and Delete Element from
Array
6 C++ Program to accept the numbers in an array arr1, arr2 and Merge
Two Arrays in third array merge
7 C++ Program to accept the numbers in an array and sort them using
Bubble Sort
8 C++ Program to accept the numbers in an array and sort them using
Selection Sort
9 C++ Program to accept the numbers in an array and sort them Insertion
Sort
10 C++ Function Overloading - This C++ program demonstrates the concept
of function overloading in C++ practically.
11 C++ Function Overloading - This C++ program demonstrates the working
of default arguments in C++
12 C++ Function Overloading - This C++ program illustrates the working
of function overloading as compared to default arguments in C++
13 C++ Function Overloading - C++ Program Example demonstrating function
overloading in C++
14 C++ Function Overloading - Example program demonstrating function
overloading in C++
15 C++ Classes and Objects - This C++ program stores price list of 5
items and to print the largest price as well as the sum of all prices
using class in C++
16 C++ Program to create a Class student with rollno,name,marks and
grade and using Object invoke read() and display()
17 C++ Classes program to illustrates the call by reference mechanism on
objects
18 C++ program demonstrates the working of a function returning an
object
19 C++ program demonstrates the working of a Constructors and
Destructors - Example Program
20 C++ program uses an overloaded constructor
21 C++ program illustrates the working of function overloading as
compared to default arguments
22 C++ program to explain the concept of single inheritance
23 C++ program illustrate the working of constructors and destructors in
multiple inheritance
24 C++ program demonstrates the concept of Pushing and Popping from the
stack-array in C++
25 C++ Stack program demonstrates the concept Pushing and Popping from
the linked-stack in C++
26 C++ Queue - Example Program of C++ Queue program demonstrates the
concept of
Insertion and deletion in an array queue in C++
27 C++ Queue - Example Program of C++ Queue to demonstrates the concept
of
Insertion and deletion from the linked queue in C++
28 C++ Pointers and Arrays. This C++ program demonstrates the concept of
close association between arrays and pointers in C++.
29 C++ program to accept string in a pointer array
5. 30 C++ Pointers and Functions. This C++ program demonstrates about
functions returning pointers in C++
31
C++ program to demonstrates the structure pointer in C++
32 C++ Pointers and Objects.This C++ program demonstrates about the
“this” pointer in C++
33 C++ program add two 3*3 matrices to form the third matrix
34 C++ Program ask to the user to enter any two 3*3 array elements to
subtract them i.e., Matrix1 - Matrix2, then display the subtracted
result of the two matrices (Matrix3)*/
35 C++ Program ,ask to the user to enter any 3*3 array/matrix element to
transpose and display the transpose of the matrix */
36
C++ Program ask to the user to enter the two 3*3 matrix elements, to
multiply them to form a new matrix which is the multiplication result
of the two entered 3*3 matrices, then display the result */
37
C++ Program accept the string and print Length of String
38 C++ Program accept two string and Compare Two String
39
C++ Program to accept the string and Delete Vowels from String
40 C++ Program accept the string and Delete Words from Sentence
41 C++ Program - Count Word in Sentence
42 C++ Program - Read and Display File
43 C++ Program - Merge Two Files
6. /* C++ Program - Find Largest Element in Array */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int large, arr[50], size, i;
cout<<"Enter Array Size (max 50) : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Searching for largest number ...nn";
large=arr[0];
for(i=0; i<size; i++)
{
if(large<arr[i])
{
large=arr[i];
}
}
cout<<"Largest Number = "<<large;
getch();
}
7. /* C++ Program to accept the 10 numbers in an array and search array using - Linear Search */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
getch();
}
8. /* C++ Program to accept the numbers in an array and Reverse Array */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, i, j, temp;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
j=i-1; // now j will point to the last element
i=0; // and i will be point to the first element
while(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
cout<<"Now the Reverse of the Array is : n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
9. /* C++ Program to accept the numbers in an array and Insert new Element in Array */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, insert, i, pos;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be insert : ";
cin>>insert;
cout<<"At which position (Enter index number) ? ";
cin>>pos;
// now create a space at the required position
for(i=size; i>pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
cout<<"Element inserted successfully..!!n";
cout<<"Now the new array is : n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}
getch();
10. /* C++ Program to accept the numbers in an array and Delete Element from Array */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, i, del, count=0;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be delete : ";
cin>>del;
for(i=0; i<size; i++)
{
if(arr[i]==del)
{
for(int j=i; j<(size-1); j++)
{
arr[j]=arr[j+1];
}
count++;
break;
}
}
if(count==0)
{
cout<<"Element not found..!!";
}
else
{
cout<<"Element deleted successfully..!!n";
cout<<"Now the new array is :n";
for(i=0; i<(size-1); i++)
{
cout<<arr[i]<<" ";
}
}
getch();
}
11. /* C++ Program to accept the numbers in an array arr1, arr2 and Merge Two Arrays in third
array merge */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];
cout<<"Enter Array 1 Size : ";
cin>>size1;
cout<<"Enter Array 1 Elements : ";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter Array 2 Size : ";
cin>>size2;
cout<<"Enter Array 2 Elements : ";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
for(i=0; i<size1; i++)
{
merge[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
merge[k]=arr2[i];
}
cout<<"Now the new array after merging is :n";
for(i=0; i<size; i++)
{
cout<<merge[i]<<" ";
}
getch();
}
12. /* C++ Program to accept the numbers in an array and sort them using Bubble Sort */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!n";
cout<<"Sorted list in ascending order :n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
13. /* C++ Program to accept the numbers in an array and sort them using Selection Sort */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort...n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
14. /* C++ Program to accept the numbers in an array and sort them Insertion Sort */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
15. /* C++ Function Overloading - This C++ program demonstrates the concept of function
overloading in C++ practically. */
#include<iostream.h>
#include<conio.h>
class printData
{
public:
void print(int i) // function 1
{
cout<<"Printing int: "<<i<<"n";
}
void print(double f) // function 2
{
cout<<"Printing float: "<<f<<"n";
}
void print(char* c)
{
cout<<"Printing characters (string): "<<c<<"n";
}
};
void main()
{
clrscr();
printData pdobj;
pdobj.print(5); // called print() to print integer
pdobj.print(50.434); // called print() to print float
pdobj.print("C++ Function Overloading"); // called print() to print string
getch();
}
16. /* C++ Function Overloading - This C++ program demonstrates the working of default
arguments in C++ */
#include<iostream.h>
#include<conio.h>
void amount(float pri, int tim=2, float rat=0.06);
void amount(float pri, int tim, float rat)
{
cout<<"ntPrincipal Amount = "<<pri;
cout<<"ntTime = "<<tim;
cout<<"ntRate = "<<rat;
cout<<"ntInterest Amount = "<<(pri*tim*rat)<<"n";
}
void main()
{
clrscr();
cout<<"Results on amount(2000)";
amount(2000);
cout<<"nResults on amount(2500, 3)";
amount(2500, 3);
cout<<"nResults on amount(2300, 3, 0.11)";
amount(2300, 3, 0.11);
cout<<"nResults on amount(2500, 0.12)";
amount(2500, 0.12);
getch();
}
17. /* C++ Function Overloading - This C++ program illustrates the working of function
overloading as compared to default arguments in C++ */
#include<iostream.h>
#include<conio.h>
void amount(float pr, int ti, float ra)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = "<<ti<<" years";
cout<<"tRate = "<<ra;
cout<<"ntInterest Amount = "<<(pr*ti*ra)<<"n";
}
void amount(float pr, int ti)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = "<<ti;
cout<<"tRate = 0.04";
cout<<"ntInterest Amount = "<<(pr*ti*0.04)<<"n";
}
void amount(float pr, float ra)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = 2 years";
cout<<"tRate = "<<ra;
cout<<"ntInterest Amount = "<<(pr*2*ra)<<"n";
}
void amount(int ti, float ra)
{
cout<<"ntPrincipal Amount = 2000";
cout<<"ttTime = "<<ti;
cout<<"tRate = "<<ra;
cout<<"ntInterest Amount = "<<(2000*ti*ra)<<"n";
}
void amount(float pr)
{
cout<<"ntPrincipal Amount = "<<pr;
cout<<"ttTime = 2 years";
cout<<"tRate = 0.04";
cout<<"ntInterest Amount = "<<(pr*2*0.04)<<"n";
}
void main()
{
clrscr();
cout<<"Results on amount(2000.0F)";
amount(2000.0F);
cout<<"nResults on amount(2500.0F, 3)";
amount(2500.0F, 3);
cout<<"nResults on amount(2300.0F, 3, 0.11F)";
amount(2300.0F, 3, 0.11F);
cout<<"nResults on amount(2, 0.12F)";
amount(2, 0.12F);
cout<<"nResults on amount(6, 0.07F)";
amount(6, 0.07F);
getch();
}
18.
19. /* C++ Function Overloading - C++ Program Example demonstrating function overloading in
C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float calarea(float a, float b, float c)
{
float s, are;
s = (a+b+c)/2;
are = sqrt(s*(s-a)*(s-b)*(s-c));
return are;
}
float calarea(float a, float b)
{
return a*b;
}
float calarea(float a)
{
return a*a;
}
void main()
{
clrscr();
int choice, s1, s2, s3, a;
do
{
cout<<"nArea Calculation Main Menun";
cout<<"1.Trianglen";
cout<<"2.Squaren";
cout<<"3.Rectanglen";
cout<<"4.Exitn";
cout<<"Enter your choice (1-4): ";
cin>>choice;
cout<<"n";
switch(choice)
{
case 1: cout<<"Enter three sides: ";
cin>>s1>>s2>>s3;
a = calarea(s1, s2, s3);
cout<<"Area = "<<a;
break;
case 2: cout<<"Enter a side: ";
cin>>s1;
a = calarea(s1);
cout<<"Area = "<<a;
break;
case 3: cout<<"Enter length and breadth: ";
cin>>s1>>s2;
a = calarea(s1, s2);
cout<<"Area = "<<a;
break;
case 4: cout<<"Exiting...press any key...";
getch();
exit(1);
default:cout<<"Wrong choice..!!";
}
cout<<"n";
}while(choice>0 && choice<=4);
getch();
}
20.
21. /* C++ Function Overloading - Example program demonstrating function overloading in C++*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int divide(int num, int den)
{
if(den==0)
{
return -1;
}
if((num%den)==0)
{
return 1;
}
else
{
return 0;
}
}
int divide(int a)
{
int j = a/2, flag = 1, i;
for(i=2; (i<=j) && (flag); i++)
{
if(a%i == 0)
{
flag = 0;
}
}
return flag;
}
void main()
{
clrscr();
int choice, res, a, b;
do
{
cout<<"1.Check for divisibilityn";
cout<<"2.Check for Primen";
cout<<"3.Exitn";
cout<<"Enter your choice(1-3): ";
cin>>choice;
cout<<"n";
switch(choice)
{
case 1: cout<<"Enter numerator and denominator: ";
cin>>a>>b;
res = divide(a, b);
if(res == -1)
{
cout<<"Divide by zero error..!!n";
break;
}
cout<<((res) ? "It is" : "It is not")<<"n";
break;
case 2: cout<<"Enter the number: ";
cin>>a;
res = 0;
res = divide(a);
cout<<((res) ? "It is" : "It is not")<<"n";
break;
case 3: cout<<"Exiting...press any key...";
39. /* C++ program illustrate the working of constructors and destructors in multiple
inheritance */
#include<iostream.h>
#include<conio.h>
class BASE1
{ protected:
int a;
public:
BASE1(int x)
{
a=x;
cout<<"Constructing BASE1n";
}
~BASE1()
{
cout<<"Destructing BASE1n";
}
};
class BASE2
{
protected:
int b;
public:
BASE2(int y)
{
b=y;
cout<<"Constructing BASE2n";
}
~BASE2()
{
cout<<"Destructing BASE2n";
}
};
class DERIVED:public BASE2, public BASE1
{
int c;
public:
DERIVED(int i, int j, int k):BASE2(i),BASE1(j)
{
c=k;
cout<<"Constructing DERIVEDn";
}
~DERIVED()
{
cout<<"Destructing DERIVEDn";
}
void show()
{
cout<<"1."<<a<<"t2."<<b<<"t3."<<c<<"n";
}
};
void main()
{
clrscr();
DERIVED obj(10,11,12);
obj.show();
getch();
}
40.
41. /* C++ program demonstrates the concept of Pushing and Popping from the stack-array in C+
+ */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
int pop(int [], int &);
int push(int [], int &, int);
void display(int [], int);
const int SIZE = 50;
void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key to exit..n";
getch();
exit(1);
}
cout<<"nThe Stack now is:n";
display(stack, top);
cout<<"nWant to enter more ? (y/n).. ";
cin>>ch;
}
cout<<"Now the deletion of elements starts..n";
ch='y';
while(ch=='y' || ch=='Y')
{
res = pop(stack, top);
if(res==-1)
{
cout<<"nUnderflow..!!..Aborting..!!..Press a key to exit..n";
getch();
exit(2);
}
else
{
cout<<"nElement deleted is: "<<res<<endl;
cout<<"nThe Stack now is:n";
display(stack, top);
}
cout<<"Want to delete more ? (y/n).. ";
cin>>ch;
}
getch();
}
int push(int stack[], int &top, int elem)
{
if(top == SIZE-1)
{
return -1;
}
else
44. /* C++ Stack program demonstrates the concept Pushing and Popping from the linked-stack
in C++ */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int info;
node *next;
} *top, *newptr, *save, *ptr;
node *create_new_node(int);
void push(node *);
void pop();
void display(node *);
void main()
{
clrscr();
int inf;
char ch='y';
top=NULL;
while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"nSorry..!!..Cannot create new node..!!..Aborting..!!n";
cout<<"Press any key to exit..n";
getch();
exit(1);
}
push(newptr);
cout<<"nWant to enter more ? (y/n).. ";
cin>>ch;
}
clrscr();
do
{
cout<<"The Stack now is: n";
display(top);
cout<<"nWant to pop an element ? (y/n).. ";
cin>>ch;
if(ch=='y' || ch=='Y')
{
pop();
}
cout<<"n";
}while(ch=='y' || ch=='Y');
getch();
}
node *create_new_node(int x)
{
ptr = new node;
ptr->info = x;
47. /* C++ Queue - Example Program of C++ Queue program demonstrates the concept of
Insertion and deletion in an array queue in C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int delete_from_queue(int []);
int insert_in_queue(int [], int);
void display(int [], int, int);
const int SIZE = 50;
int queue[SIZE];
int front=-1;
int rear=-1;
void main()
{
clrscr();
int item, check;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
check = insert_in_queue(queue, item);
if(check == -1)
{
cout<<"nOverflow..!!..Aborting..!!..Press a key to exit..n";
getch();
exit(1);
}
cout<<"Item inserted successfully..!!n";
cout<<"nNow the Queue (Front...to...Rear) is:n";
display(queue, front, rear);
cout<<"nWant to insert more ? (y/n).. ";
cin>>ch;
}
clrscr();
cout<<"Now deletion of elements starts...n";
ch='y';
while(ch=='y' || ch=='Y')
{
check = delete_from_queue(queue);
if(check == -1)
{
cout<<"nUnderflow..!!..Aborting..!!..Pres a key to exit..n";
getch();
exit(2);
}
else
{
cout<<"nElement deleted is: "<<check<<"n";
cout<<"Now the Queue (Front...to...Rear) is:n";
display(queue, front, rear);
}
cout<<"nWant to delete more ? (y/n)... ";
cin>>ch;
}
50. /* C++ Queue - Example Program of C++ Queue to demonstrates the concept of
Insertion and deletion from the linked queue in C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
node *next;
} *front, *newptr, *save, *ptr, *rear;
node *create_new_node(int);
void insert(node *);
void delete_node_queue();
void display(node *);
void main()
{
clrscr();
front = rear = NULL;
int inf;
int count=0;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"nSorry..!!..Cannot create new node..!!..Aborting..!!n";
cout<<"Press any key to exit..n";
getch();
exit(1);
}
insert(newptr);
cout<<"nNow the Queue (Front...to...Rear) is:n";
display(front);
cout<<"nWant to enter more ? (y/n).. ";
cin>>ch;
}
clrscr();
do
{
cout<<"The Linked-Queue now is (Front...to...Rear) is:n";
display(front);
if(count==0)
{
cout<<"nWant to delete ? (y/n).. ";
count++;
}
else
{
cout<<"nWant to delete more ? (y/n).. ";
}
cin>>ch;
if(ch=='y' || ch=='Y')
{
delete_node_queue();
53. /* C++ Pointers and Arrays. This C++ program demonstrates the concept of close
association between arrays and pointers in C++. */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *iptr[5];
int fa=65, fb=66, fc=67, fd=68, fe=69;
int i;
// initialize array pointers by making them point to 5 different ints
iptr[0] = &fa;
iptr[1] = &fb;
iptr[2] = &fc;
iptr[3] = &fd;
iptr[4] = &fe;
// now prints the values being pointed to by the pointers
for(i=0; i<5; i++)
{
cout<<"The pointer iptr["<<i<<"] points to "<<*iptr[i]<<"n";
}
cout<<"n";
// now print the addresses stored in the array
cout<<"The base address of the array iptr of pointers is "<<iptr<<"n";
for(i=0; i<5; i++)
{
cout<<"The address stored in iptr["<<i<<"] is "<<iptr[i]<<"n";
}
getch();
}
Here is the sample run of the above C++ program
54. /* C++ program to accept string in a pointer array */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *names[] = {"Sachin", "Dhoni", "Sehwag", "Raina", "Yuvraj"};
int len=0;
len=strlen(names[1]); // length of 2nd string
cout<<"Originally:ntstring 2 is ";
cout.write(names[1],len).put('n');
cout<<"tand string 4 is ";
cout.write(names[3],len).put('n');
// now exchange the position of string 2 and 4
char *tptr;
tptr = names[1];
names[1] = names[3];
names[3] = tptr;
// now print the exchanged string
cout<<"nExchanged:ntstring 2 is ";
cout.write(names[1],len).put('n');
cout<<"tand string 4 is ";
cout.write(names[3],len).put('n');
getch();
}
Here is the sample output of the above C++ program
55. /* C++ Pointers and Functions. This C++ program demonstrates about functions returning
pointers in C++ */
#include<iostream.h>
#include<conio.h>
int *biger(int &, int &);
void main()
{
clrscr();
int num1, num2, *c;
cout<<"Enter two integersn";
cin>>num1>>num2;
c = biger(num1, num2);
cout<<"The bigger value = "<<*c;
getch();
}
int *biger(int &x, int &y)
{
if(x>y)
{
return(&x);
}
else
{
return(&y);
}
}
58. /* C++ Pointers and Objects.This C++ program demonstrates about the “this” pointer in C+
+*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Salesman
{
char name[1200];
float total_sales;
public:
Salesman(char *s, float f)
{
strcpy(name, "");
strcpy(name, s);
total_sales = f;
}
void prnobject(void)
{
cout.write(this->name, 26); // use of this pointer
cout<<" has invoked prnobject().n";
}
};
void main()
{
clrscr();
Salesman Rajat("Rajat", 21450), Ravi("Ravi", 23190), Vikrant("Vikrant", 19142);
/* above statement creates three objects */
Rajat.prnobject();
Vikrant.prnobject();
Ravi.prnobject();
getch();
}
Above C++ program will produce the following output :
59. /* C++ program add two 3*3 matrices to form the third matrix */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
cout<<"Enter matrix 1 elements :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter matrix 2 elements :";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Adding the two matrix to form the third matrix .....n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
cout<<"The two matrix added successfully...!!";
cout<<"The new matrix will be :n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"n";
}
getch();
}
60.
61. /* C++ Program ask to the user to enter any two 3*3 array elements to subtract them
i.e., Matrix1 - Matrix2, then display the subtracted result of the two matrices
(Matrix3)*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[3][3], arr2[3][3], arr3[3][3], sub, i, j;
cout<<"Enter 3*3 Array 1 Elements : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr1[i][j];
}
}
cout<<"Enter 3*3 Array 2 Elements : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr2[i][j];
}
}
cout<<"Subtracting array (array1-array2) ... n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arr3[i][j]=arr1[i][j]-arr2[i][j];
}
}
cout<<"Result of Array1 - Array2 is :n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<arr3[i][j]<<" ";
}
cout<<"n";
}
getch();
}
62. /* C++ Program ,ask to the user to enter any 3*3 array/matrix element to transpose and display the transpose of the
matrix */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[3][3], i, j, arrt[3][3];
cout<<"Enter 3*3 Array Elements : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr[i][j];
}
}
cout<<"Transposing Array...n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
arrt[i][j]=arr[j][i];
}
}
cout<<"Transpose of the Matrix is :n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<arrt[i][j];
}
cout<<"n";
}
getch();
}
When the above C++ program is compile and executed, it will produce the following result:
63. /* C++ Program ask to the user to enter the two 3*3 matrix elements, to multiply them
to form a new matrix which is the multiplication result of the two entered 3*3
matrices, then display the result */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
cout<<"Enter first matrix element (3*3) : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter second matrix element (3*3) : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Multiplying two matrices...n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
{
sum = sum + mat1[i][k] * mat2[k][j];
}
mat3[i][j] = sum;
}
}
cout<<"nMultiplication of two Matrices : n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"n";
}
getch();
}
64.
65. /* C++ Program accept the string and print Length of String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[20], len;
cout<<"Enter a string : ";
gets(str);
len=strlen(str);
cout<<"Length of the string is "<<len;
getch();
}
66. /* C++ Program accept two string and Compare Two String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str1[100], str2[100];
cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not equal";
}
Getch();
}
67. /* C++ Program to accept the string and Delete Vowels from String */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str[20];
int len, i, j;
cout<<"Enter a string : ";
gets(str);
len=strlen(str);
for(i=0; i<len; i++)
{
if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
for(j=i; j<len; j++)
{
str[j]=str[j+1];
}
len--;
}
}
cout<<"After deleting the vowels, the string will be : "<<str;
getch();
}
68. /* C++ Program accept the string and Delete Words from Sentence */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i, j = 0, k = 0, count = 0;
char str[100], str1[10][20], word[20];
cout<<"Enter the String : ";
gets(str);
/* Converting the string into 2D Array */
for (i=0; str[i]!='0'; i++)
{
if (str[i]==' ')
{
str1[k][j] = '0';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '0';
cout<<"Enter a word to be delete : ";
cin>>word;
/* Comparing the string with the given word */
for (i=0; i<k+1; i++)
{
if (strcmp(str1[i], word) == 0)
{
for (j=i; j<k+1; j++)
{
strcpy(str1[j], str1[j + 1]);
k--;
}
}
}
cout<<"The new String after deleting the word : n";
for (i=0; i<k+1; i++)
{
cout<<str1[i]<<" ";
}
getch();
}
69.
70. /* C++ Program - Count Word in Sentence */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char strs[100], countw=0, strw[15], i;
cout<<"Write a sentence : ";
gets(strs);
int len=strlen(strs);
for(i=0; i<len; i++)
{
if(strs[i]==' ')
{
countw++;
}
}
cout<<"Total number of words in the sentence is "<<countw+1;
getch();
}
71. /* C++ Program - Read and Display File */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream ifile;
char s[100], fname[20];
cout<<"Enter file name to read and display its content (like file.txt) : ";
cin>>fname;
ifile.open(fname);
if(!ifile)
{
cout<<"Error in opening file..!!";
getch();
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
cout<<"n";
ifile.close();
getch();
}
72. /* C++ Program to Merge Two Files */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream ifiles1, ifiles2;
ofstream ifilet;
char ch, fname1[20], fname2[20], fname3[30];
cout<<"Enter first file name (with extension like file1.txt) : ";
gets(fname1);
cout<<"Enter second file name (with extension like file2.txt) : ";
gets(fname2);
cout<<"Enter name of file (with extension like file3.txt) which will store the
contents of the two files (fname1 and fname1) : ";
gets(fname3);
ifiles1.open(fname1);
ifiles2.open(fname2);
if(ifiles1==NULL || ifiles2==NULL)
{
perror("Error Message ");
cout<<"Press any key to exit...n";
getch();
exit(EXIT_FAILURE);
}
ifilet.open(fname3);
if(!ifilet)
{
perror("Error Message ");
cout<<"Press any key to exit...n";
getch();
exit(EXIT_FAILURE);
}
while(ifiles1.eof()==0)
{
ifiles1>>ch;
ifilet<<ch;
}
while(ifiles2.eof()==0)
{
ifiles2>>ch;
ifilet<<ch;
}
cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
ifiles1.close();
ifiles2.close();
ifilet.close();
getch();
}