0% found this document useful (0 votes)
54 views18 pages

XII CS1 Journal (C++)

C++ codes

Uploaded by

sumeetavhad1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views18 pages

XII CS1 Journal (C++)

C++ codes

Uploaded by

sumeetavhad1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Note for writing Computer Science (Paper I) Journal

1. Purchase 100 or 200 pages journal book one side ruled for CS-1 for the year 2023-2024

2. On the blank side (left side) write the output and on the ruled side (right
side) write the respective program.

3. First write all C++ programs (total 09 programs), then the HTML program (02 program) and
then Visual Basics programs(total 06 programs).

4. Write the Journal neatly, it carries 20 marks.


Practical 1:

Write a program in C++ that initializes an array of given 10 real numbers. The program
should sort the numbers in ascending/descending order using Bubble sort method. It
should print the given list of numbers as well as the sorted list.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,j,temp;
cout<<"Enter the elements of the array\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Original array is";
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nAfter Bubble sort Ascending order is\n";
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
}
getch();
}

OUTPUT
Enter the elements of the array
10 8 6 4 2 9 7 5 3 1
Original array is
10 8 6 4 2 9 7 5 3 1
After Bubble sort Ascending order is
1 2 3 4 5 6 7 8 9 10
Practical 2:
Write a program in C++ that first initializes an array of given 10 real numbers. The program
must verify whether a given element belongs to this array or not using Binary Search. The
element (to be searched) is to be taken at the time of program execution. If the number is
found the program should print its position in the array otherwise it should print “The
Number is not found”.

#include<iostream.h>
#include<conio.h>
int bsearch(int[],int);
void main()
{
int a[10],i,d,loc;
clrscr();
cout<<"enter any 10 numbers in ascending order\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"enter the number to be searched\t";
cin>>d;
loc=bsearch(a,d);
if(loc)
cout<<"\nNumber found at location:\t"<<loc;
else
cout<<"\nNumber not found";
getch();
}
int bsearch(int y[],int dt)
{
int mid,lb,ub;
lb=0; ub=10-1;
while(ub>=lb)
{
mid=(ub+lb)/2;
if(y[mid]==dt)
return mid+1;
if(y[mid]<dt)
lb=mid+1;
else
ub=mid-1;
}
return 0;
}

OUTPUT

enter any 10 numbers in ascending order


5 10 15 20 25 30 35 40 45 50
enter the number to be searched 20

Number found at location: 4

enter any 10 numbers in ascending order


5 10 15 20 25 30 35 40 45 50
enter the number to be searched 29

Number not found


Practical 3:
Write a program in C++ that exchanges data (passing by reference) using swap function to
interchange the given two numbers.

#include<iostream.h>
#include<conio.h>
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}

void main()
{
clrscr();
int x,y;
cout<<"Enter two numbers\n";
cin>>x>>y;
cout<<"\nBefore Swapping\t";
cout<<x<<"\t"<<y;
swap(x,y);
cout<<"\n\n After Swapping\t";
cout<<x<<"\t"<<y;
getch();
}
Enter two numbers
76 15

Before Swapping 76 15

After Swapping 15 76
Practical 4:
Write a program in C++ that first initialize an array of five given numbers
(short/float/double). The program must add these numbers by traversing this array with a
pointer. The output should print the starting address of the array with the size of the number
to which it points. The program must also print the sum and pointer address with the
addition of every number as well as the ending address.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[5];
float *ptr,sum=0;
cout<<"Enter 5 Floating Number\n";
for(int i=0; i<5;i++)
{
cin>>a[i];
}
ptr=a;
cout<<"Starting address\tsize \t Ending address \tValue of sum";
for(i=0;i<5;i++)
{
sum=sum+*ptr;
cout<<"\n"<<ptr<<"\t\t"<<sizeof(*ptr)<<"\t";
ptr=ptr+1;
cout<<ptr<<"\t\t"<<sum;
}
getch();
}
Enter 5 Floating Number
10.5
5.4
2.7
3.14
8.90
Starting address size Ending address Value of sum
0x8f4dffde 4 0x8f4dffe2 10.5
0x8f4dffe2 4 0x8f4dffe6 15.9
0x8f4dffe6 4 0x8f4dffea 18.6
0x8f4dffea 4 0x8f4dffee 21.74
0x8f4dffee 4 0x8f4dfff2 30.639999
Practical 5:
Write a program in C++ to input the given string (including spaces) and reverse it using a
function which locates the end of the string and swaps the first character with the last
character, the second character with the second last character and so on.

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char s[100], temp;
cout<<"Enter String\t\t";
gets(s);
int len=strlen(s);
int l=len-1;
for(int i=0;i<len/2;i++)
{
temp=s[i];
s[i]=s[l];
s[l]=temp;
l--;
}
cout<<"\nReverse of the string\t"<<s;
getch();
}
Enter String Computer Science

Reverse of the string ecneicS retupmoC


Practical 6:
Write a program in C++ with a ratio class using member function like assign() to initialize
its member data (integers – numerator and denominator) , convert() to convert the ratio into
double, invert() to get the inverse of the ratio and print() the ratio and its reciprocal.

#include<iostream.h>
#include<conio.h>
class ratio
{
int num,den;
public:void assign();
double convert();
void invert();
void print();
};
void ratio::assign()
{
cout<<"Enter numerator \t";
cin>>num;
cout<<"Enter denominator\t";
cin>>den;
}
double ratio::convert()
{
return (num/den);
}
void ratio::invert()
{
int temp;
temp=num;
num=den;
den=temp;
}
void ratio::print()
{
cout<<num<<"/"<<den<<endl;
}
void main()
{
clrscr();
ratio r;
r.assign();
cout<<"\nOriginal ratio is=\t";
r.print();
cout<<"\nInverse ratio is=\t";
r.convert();
r.invert();
r.print();
getch();
}

Enter numerator 20
Enter denominator 29

Original ratio is= 20/29

Inverse ratio is= 29/20


Practical 7:
Implement class circle in C++. Each object of this class will represent a circle, storing its
radius and x,y co-ordinates of the centre point as float. Include a default constructor. Access
functions- area() and circumference(). The program must print the area, circumference and
the centre point co-ordinates in the output.

#include<iostream.h>
#include<conio.h>
class circle
{
private: float r,a,c,x,y;
public: circle()
{
cout<<"Enter Radius\t";
cin>>r;
cout<<"Enter X Co-ordinate\t";
cin>>x;
cout<<"Enter Y Co-ordinate\t";
cin>>y;
}
void area()
{
a=3.14*r*r;
}
void circum()
{
c=2*3.14*r;
}
void print()
{
cout<<"The radius of the circle is\t"<<r;
cout<<"\n\nThe x,y coordinate of center is\t"<<x<<" , "<<y;
cout<<"\n\nThe area of circle is\t"<<a;
cout<<"\n\nThe circumference of circle is\t"<<c;
}
};
void main()
{
clrscr();
circle c;
c.area();
c.circum();
c.print();
getch();
}

Enter Radius 5
Enter X Co-ordinate 6
Enter Y Co-ordinate 8
The radius of the circle is 5

The x,y coordinate of center is 6 , 8

The area of circle is 78.5

The circumference of circle is 31.4


Practical 8:
Write a program in C++ that initializes the ration class with no parameters as default
constructor. The program must print the message “OBJECT IS BORN” during
initialization. It should display the message “NOW X is alive” when the first member
function of ratio class is called. The program must display “OBJECT DIES” when the class
destructor is invoked for the object when it reaches the end of its scope.

#include<iostream.h>
#include<conio.h>
class ratio
{
public: ratio()
{
cout<<"\n\nThe Object is born";
}
void function1()
{
cout<<"\n\nNow object is alive";
}
~ratio()
{
cout<<"\n\nObject dies";
getch();
}
};
void main()
{
clrscr();
ratio r;
r.function1();
getch();
}

The Object is born

Now object is alive

Object dies
Practical 9:
Write a program in C++ using virtual function. The function must declare P to be a pointer
to object of the base class Person. First , the program must assign p to point an instance
x(name of the person e.g. BOB) of class Person. The program must then assign p to point at
an instance y (name of the student e.g. TOM) of the derived class student. Define a print
function in the base class to print the name of the Person. The second call to the print function
must print the name of the student using derived class’s print function.

#include<string.h>
#include<iostream.h>
#include<conio.h>
class Person
{
private: char name[80];
public: Person()
{
strcpy(name,"BOB");
}
virtual void print()
{
cout<<"\nName of the person assigned through base object is\t"<<name;
}
};
class student:public Person
{
private: char name1[80];
public: student()
{
strcpy(name1,"TOM");
}
void print()
{
cout<<"\nName of the person assigned through derived object\t"<<name1;
}
};
void main()
{
clrscr();
Person *p,x;
student y;
p=&x;
p->print();
p=&y;
p->print();
getch();
}

Name of the person assigned through base object is BOB


Name of the person assigned through derived object TOM

You might also like