HSC CS 1 C++ Practicals
HSC CS 1 C++ Practicals
10.1
8.1
7.1
6.4
1.1
4.5
2.1
3.2
6.7
4.9
10.1
8.1
7.1
6.7
6.4
4.9
4.5
3.2
2.1
1.1
/*Write a function in C++ that exchanges data (passing)by reference using
swap functiopn to interchange the given two numbers */
#include <iostream.h>
#include <conio.h>
void swap(int &,int &);
void main()
{
int a,b;
clrscr();
cout<<"\nEnters two numbers";
cin>>a>>b;
cout<<endl<<"Original values : ";
cout<<endl<<"a = "<<a<<" b = "<<b;
swap(a,b);
getch();
}
void swap(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
cout<<endl<<"New values : ";
cout<<endl<<"a = "<<x<<" b = "<<y;
}
Enters two numbers
Original values :
a=4 b=5
New values :
a=5 b=4
/*Write a function in C++ that exchanges data (passing)by reference using
swap functiopn to interchange the given two numbers */
#include<iostream.h>
#include<conio.h>
void swap(float &x,float &y);
void main()
{
clrscr();
float a,b;
cout<<"\n Enters two floating numbers";
cin>>a>>b;
cout<<"\nBefore swap";
cout<<"a="<<a<<" b="<<b;
swap(a,b);
cout<<"\nAfter swap";
cout<<"a="<<a<<" b="<<b;
getch();
}
void swap(float &x, float &y)
{
float t=x;
x=y;
y=t;
}
Enters two floating numbers
8.3
2.5
50.1
100.2
150.3
200.4
250.5
300.6
350.7
400.8
450.9
500.10
400.8
The no is at position=8
600
Size of (short):2
p=0x8fc7ffea
*p=19
sum=19
p=0x8fc7ffec
*p=38
sum=57
p=0x8fc7ffee
*p=57
sum=114
p=0x8fc7fff0
*p=76
sum=190
p=0x8fc7fff2
*p=95
sum=285
Ending Address:0x8fc7fff4
/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double.The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what 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();
const size=5;
float a[size]={0.3,1.2,0.8,2.2,3.4};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (float):"<<sizeof(float)<<endl;
float *end=a+size;
float sum=0;
cout<<"The traversed array is:\n\n";
for(float*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8f7effdc
Size of (float):4
p=0x8f7effdc
*p=0.3
sum=0.3
p=0x8f7effe0
*p=1.2
sum=1.5
p=0x8f7effe4
*p=0.8
sum=2.3
p=0x8f7effe8
*p=2.2
sum=4.5
p=0x8f7effec
*p=3.4
sum=7.9
Ending Address:0x8f7efff0
/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double.The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what 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();
const size=5;
double a[size]={1.11,2.22,3.33,4.44,5.55};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (double):"<<sizeof(double)<<endl;
double *end=a+size;
double sum=0;
cout<<"The traversed array is:\n\n";
for(double*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8f7effc4
Size of (double):8
p=0x8f7effc4
*p=1.11
sum=1.11
p=0x8f7effcc
*p=2.22
sum=3.33
p=0x8f7effd4
*p=3.33
sum=6.66
p=0x8f7effdc
*p=4.44
sum=11.1
p=0x8f7effe4
*p=5.55
sum=16.65
Ending Address:0x8f7effec
/*Write a program in C++ that first initialises an array of five numbers
such as short/float/double.The program must add these numbers by
traversing this array with a pointer. The o/p should print the starting
address of an array with the size of number (n bytes)to what 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();
const size=5;
long double a[size]={150.1,300.2,450.3,500.4,550.5};
cout<<"Starting Address is:"<<a<<endl;
cout<<"Size of (long double):"<<sizeof(long double)<<endl;
long double*end=a+size;
long double sum=0;
cout<<"The traversed array is:\n\n";
for(long double*p=a;p<end;p++)
{
sum=sum+*p;
cout<<"\tp="<<p<<endl;
cout<<"\t*p="<<*p<<endl;;
cout<<"\tsum="<<sum<<endl;
}
cout<<"\nEnding Address:"<<end<<endl;
getch();
}
Starting Address is:0x8f7dffb8
p=0x8f7dffb8
*p=150.1
sum=150.1
p=0x8f7dffc2
*p=300.2
sum=450.3
p=0x8f7dffcc
*p=450.3
sum=900.6
p=0x8f7dffd6
*p=500.4
sum=1401
p=0x8f7dffe0
*p=550.5
sum=1951.5
Ending Address:0x8f7dffea
/*Write a function in C++ to i/p the given string (including spaces)
and reverses it using function which locates the end of the string and
swap the first character with the last character, the second character
with the second last character and so on */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void swap(char *rev);
void main()
{
clrscr();
char str[80];
cout<<"\nEnters string\n";
cin.getline(str,80);
cout<<"\nThe original string is="<<str;
swap(str);
cout<<"\nAfter swapping string is="<<str;
getch();
}
void swap(char *rev)
{
strrev(rev);
}
Enters string
india is my country
10
^Z
10->9->3->7->6->5->X
/*Write a C++ with ratio class using member functions like assign()
function to initialise its member data(integer numerator and denominator),
convert()function converts the ratio into double, invert() function to get the
inverse of the ratio and print() function to print the ratio and its reciprocal*/
#include<iostream.h>
#include<conio.h>
class ratio
{
int num,den;
public:void assign()
{
cout<<"\nEnter numerator";
cin>>num;
cout<<"\nEnter denominator";
cin>>den;
cout<<"\nRatio and Reciprocal is";
}
public:void convert()
{
double c;
c=((num/den));
cout<<"\nThe double conversion is="<<c;
}
public:void invert()
{
int temp;
temp=num;
num=den;
den=temp;
cout<<"\nThe inversion of ratio and reciprocal=";
}
public:void print()
{
cout<<num<<"/"<<den;
}
};
void main()
{
clrscr();
ratio x;
x.assign();
x.print();
x.convert();
x.invert();
x.print();
getch();
}
Enter numerator
Enter denominator
center of co-ordinate3.5
Enter radius
Enters radius5
Total marks=140
Percentage=46
#include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{
public: char *name;
person(char *s)
{
name=new char[strlen(s+1)];
strcpy(name,s);
}
virtual void print()
{
cout<<"My name is="<<name<<endl;
}
};
class student :public person
{
float rno;
public:
student(char *s,float r):person(s),rno(r)
{
}
void print()
{
cout<<"My name is="<<name<<"\nMy Roll Number="<<rno;
}
};
void main()
{
clrscr();
person *p;
person x("BOB");
p=&x;
p->print();
student y("TOM",101);
p=&y;
p->print();
getch();
}
My name is=BOB
My name is=TOM
My Roll Number=101
//program of name of country by using file handeling
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ofstream fout;
fout.open("country");
fout<<"\nIndia";
fout<<"\nPakistan";
fout<<"\nEngland";
fout<<"\nSothafrica";
fout<<"\nAustralia";
fout.close();
fout.open("capital");
fout<<"\nDelhi";
fout<<"\nLahor";
fout<<"\nLondon";
fout<<"\nCaptown";
fout<<"\nSidany";
fout.close();
const int n=80;
char line[n];
ifstream fin;
fin.open("country");
cout<<"\ncontent of country file\n";
while(fin.eof()==0)
{
fin.getline(line,n);
cout<<"\n"<<line;
}
fin.close();
fin.open("capital");
cout<<"\nContents of capital file\n";
while(fin.eof()==0)
{
fin.getline(line,n);
cout<<"\n"<<line;
}
fin.close();
getch();
}
content of country file
India
Pakistan
England
Sothafrica
Australia
Delhi
Lahor
London
Captown
Sidany