Question 1.
WAP to compute the sum of the first n terms of the following series:
S=1-(1/22)+(1/33)-…(1/nn)
The number of terms n is to be taken from the user through the command line. If the command line
argument is not found then prompt the user to enter the value of n.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main (int argc, char*argv[])
{
int i ;
cout<<"Command line arguments\n";
for(i=0;i<argc;i++)
cout<<argv[i]<<endl;
int n=atoi(argv[1]);
cout<<"n="<<n;
float sum=0;
for(int i=1; i<=n; i++)
sum+=pow(-1,i+1)*1/pow(i,i);
cout<<"The sum of the series is"<<sum;
return 0;
}
Output:
Command line arguments
E:\sadhana electronics(c++)\command [Link]
5
n=5The sum of the series is0.783451
Press Enter to return to Quincy...
Question 2.)
WAP to remove duplicates from an array.
Code:
#include<iostream>
using namespace std;
void input(int a[],int n)
{
cout<<"enter aray:";
for (int i=0; i<n; i++)
{
cin>>a[i];
}
}
void removedup(int a[],int& n)
{
for (int i=0; i<n-1;i++)
{
if (a[i]==a[i+1])
{
for (int k=i;k<n-1;k++)
a[k]=a[k+1];
n=n-1;
}
}
}
void display(int a[],int n)
{
cout<<"array after removing duplicate:";
for (int i=0; i<n; i++)
cout<<a[i];
}
int main()
{
int a[10],n;
cout<<"enter number of elements:";
cin>>n;
input(a,n);
removedup(a,n);
display(a,n);
return 0;
}
Output:
enter number of elements:8
enter aray:2 2 3 4 5 6 6 7
array after removing duplicate:234567
Press Enter to return to Quincy...
Question 3.)
WAP that prints a table indicating the number of occurrences of each alphabet in the text entered as
command line arguments.
Code:
#include<iostream>
#include <string.h>
using namespace std;
int main(int argc, char*argv[])
{
char a[20];
strcpy(a,argv[1]);
for(int i='A';i<='z';i++)
{
int count=0;
for(int j=0; j<strlen(a);j++)
if (a[j]==i)
count++;
if(count!=0)
cout<<"alphabet"<<"\t"<<(char)i<<"\t"<<"appears"<<"\t"<<count<<"\t"<<"times"<<endl;
}
}
Output:
alphabet a appears 1 times
alphabet g appears 2 times
alphabet i appears 1 times
alphabet m appears 2 times
alphabet n appears 1 times
alphabet o appears 1 times
alphabet p appears 1 times
alphabet r appears 2 times
Press Enter to return to Quincy...
Question 4.)
#include<iostream>
using namespace std;
class String
{
char a[20];
public:
String(){}
void input()
{
cout<<endl<<"Enter the string\n";
cin>>a;
}
void display()
{
cout<<a<<endl;
}
void showaddress()
{
for(int i=0;a[i]!='\0';i++)
cout<<"\nAddress of"<<a[i]<<"="<<(void*)&a[i];
}
void concat(String o1)
{
int i;
for(i=0;a[i]!='\0';i++){}
for(int j=0;o1.a[j]!='\0';j++)
{
a[i]=o1.a[j];
i++;
}
a[i]='\0';
}
int comparison(String o1)
{
for(int i=0; a[i]!='\0'&& o1.a[i]!='\0';i++)
if (a[i]!=o1.a[i])
return a[i]-o1.a[i];
return 0;
}
int length()
{
int i;
for( i=0;a[i]!='\0';i++);
return i;
}
void lowertoupper()
{
cout<<"string from lower to upper case characters:\t";
for(int i=0;a[i]!='\0';i++)
if(a[i]>='a' and a[i]<='z')
a[i]=a[i]-32;
}
void reverse()
{
cout<<"string after reversing:\t";
int l=length();
for(int i=0,j=l-1;i<l/2;i++,j--)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
};
int main()
{
String o1;
[Link]();
[Link]();
[Link]();
String o2;
[Link]();
[Link]();
[Link](o2);
cout<<"String after concatenation:\t";
[Link]();
cout<<"length="<<[Link]()<<endl;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
int x=[Link](o2);
if(x==0)
cout<<"strings are equal";
else if (x>0)
cout<<"first string will come alphabetically after the second string";
else
cout<<"first string will come alphabetically befor the second string";
return 0;
}
Output:
Enter the string
sadhana
sadhana
Address ofs=0x6afef8
Address ofa=0x6afef9
Address ofd=0x6afefa
Address ofh=0x6afefb
Address ofa=0x6afefc
Address ofn=0x6afefd
Address ofa=0x6afefe
Enter the string
aditi
aditi
String after concatenation: sadhanaaditi
length=12
string from lower to upper case characters: SADHANAADITI
string after reversing: ITIDAANAHDAS
Enter the string
rashi
Enter the string
the
first string will come alphabetically befor the second string
Press Enter to return to Quincy...
Question 5.)
WAP to merge two ordered arrays to get a single ordered array.
Code:
#include<iostream>
using namespace std;
class mergeord
{
int a[10],b[10],c[20];
int m,n,k;
public:
void input()
{
cout<<"How many element in array a";
cin>>m;
cout<<"Enter the elements";
for(int i=0;i<m;i++)
cin>>a[i];
cout<<"How many element in array b";
cin>>n;
cout<<"Enter the elements";
for(int i=0;i<n;i++)
cin>>b[i];
}
void merge()
{
int i,j;
i=0;
j=0;
k=0;
while(i<m && j<n)
{
if (a[i]<=b[j]){
c[k]=a[i];
k++;
i++;
}
else
{
c[k]=b[j];
k++;
j++;
}
}
if(i<m)
while(i<m)
c[k++]=a[i++];
if(j<n)
while(j<n)
c[k++]=b[j++];
}
void display()
{
cout<<"First array";
for(int i=0;i<m;i++)
cout<<a[i]<<"\t";
cout<<endl;
cout<<"Second array";
for(int i=0;i<n;i++)
cout<<b[i]<<"\t";
cout<<endl;
cout<<"Merged array";
for(int i=0;i<k;i++)
cout<<c[i]<<"\t";
}
};
int main()
{
mergeord o1;
[Link]();
[Link]();
[Link]();
return 0;
}
Output:
How many element in array a6
Enter the elements 1 23 43 54 65 76
How many element in array b7
Enter the elements1 22 45 66 75 98 100
First array1 23 43 54 65 76
Second array1 22 45 66 75 98 100
Merged array1 1 22 23 43 45 54 65 66 75 76 98 100
Press Enter to return to Quincy...
Question 6.)
WAP to search a given element in a set of N numbers using Binary Search
a. With recursion
b. Without recursion
a.)
Code:
#include<iostream>
using namespace std;
void binsearch(int a[],int n,int x, int low, int high)
{
int mid;
if (low>high)
{
cout<<"element not found";
return;
}
else
{
mid=(low+high)/2;
if (a[mid]==x)
{
cout<<"element found";
return;
}
else if(a[mid]>x)
{
binsearch(a,n,x,low,mid-1);
}
else
{
binsearch(a,n,x,mid+1,high);
}
}
}
int main()
{
int a[]={1,2,3,4,5,6,7,8};
binsearch(a,8,3,0,7);
return 0;
}
Output:
element found
Press Enter to return to Quincy...
b.)
Code:
#include<iostream>
using namespace std;
void binsearch(int a[],int n,int x)
{
int low,mid,high;
low=0;
high=n-1;
mid=(low+high)/2;
while (low<=high)
{
if(a[mid]==x)
{
cout<<"element found";
return;
}
else if (a[mid]<x)
low=mid+1;
else
high=mid-1;
mid=(low+high)/2;
}
if(low>high)
{
cout<<"element not present";
}
}
int main()
{
int a[10],n;
cout<<"how many elements";
cin>>n;
cout<<"enter the elements";
for(int i=0;i<n;i++)
cin>>a[i];
binsearch(a,n,14);
return 0;
}
Output:
(i)
how many elements5
enter the elements2 3 14 23 45
element found
Press Enter to return to Quincy...
(ii)
how many elements5
enter the elements2 3 565 323 54 32
element not present
Press Enter to return to Quincy...
Question 7.)
WAP to calculate GCD of two numbers
a. With recursion
b. Without recursion
a.)
Code:
#include<iostream>
using namespace std;
int GCD(int a,int b)
{
int r;
r=a%b;
if (r==0)
{
return b;
}
else
return GCD(b,a%b);
int main()
{
int z,x,y;
cout<<"enter the two numbers:";
cin>>x>>y;
z=GCD(x,y);
cout<<"gcd of two numbers is:"<<z;
return 0;
}
Output:
enter the two numbers:5 25
gcd of two numbers is:5
Press Enter to return to Quincy...
b.)
Code:
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int r;
r=a%b;
while(r!=0)
{
a=b;
b=r;
r=a%b;
}
return b;
}
int main()
{
int z,x,y;
cout<<"enter the two numbers:";
cin>>x>>y;
z=gcd(x,y);
cout<<"gcd of two numbers is:"<<z;
return 0;
Output:
enter the two numbers:20 60
gcd of two numbers is:20
Press Enter to return to Quincy...
Question 8.)
Create a matrix class. Write a menu-driven program to perform following matrix operations (exception
should be thrown by the functions if matrices passed to them are incompatible and handled by the
main() function):
a. Sum
b. Product
c. Transpose
Code:
#include<iostream>
using namespace std;
class matrix
{
int a[4][4];
int rows,cols;
public:
matrix()
{
rows=-1;
cols=-1;
}
matrix(int r, int c)
{
rows=r;
cols=c;
}
void input()
{
int i ,j;
for (int i=0; i<rows;i++)
{
cout<<"\n enter the elements of row"<<(i+1);
for (int j=0; j<cols; j++)
{
cin>>a[i][j];
}
}
}
void display();
matrix transpose();
matrix sum(matrix);
};
void matrix::display()
{
int i,j;
for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
cout<<a[i][j];
cout<<endl;
}
}
matrix matrix::sum(matrix o2)
{
matrix o3;
int i,j;
[Link]=rows;
[Link]=cols;
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++)
o3.a[i][j]=a[i][j]+o2.a[i][j];
return o3;
matrix matrix::transpose()
{
matrix o3;
[Link]=cols;
[Link]=rows;
for (int i=0; i<[Link]; i++)
for(int j=0; j<[Link]; j++)
o3.a[i][j]=a[j][i];
return(o3);
}
int main()
{
int x;
char ch;
matrix o1(2,3),o2(2,3),o3,o4;
do
{
cout<<"1 Input\n";
cout<<"2 display\n";
cout<<"3 Sum\n";
cout<<"4 Transpose\n";
cout<<"enter the choice";
cin>>x;
switch(x)
{
case 1: [Link]();
[Link]();
break;
case 2: [Link]();
[Link]();
break;
case 3: o3=[Link](o2);
cout<<"Sum ofmatice is";
[Link]();
break;
case 4: o4=[Link]();
[Link]();
break;
}
cout<<"Do you want to continue";
cin>>ch;
}
while(ch=='y');
return 0;
}
Output:
1 Input
2 display
3 Sum
4 Transpose
enter the choice1
enter the elements of row1 1 2 3
enter the elements of row24 5 6
enter the elements of row17 8 9
enter the elements of row210 22 34
Do you want to continuey
1 Input
2 display
3 Sum
4 Transpose
enter the choice2
123
456
789
102234
Do you want to continuey
1 Input
2 display
3 Sum
4 Transpose
enter the choice3
Sum ofmatice is81012
142740
Do you want to continuey
1 Input
2 display
3 Sum
4 Transpose
enter the choice4
14
25
36
Do you want to continuen
Press Enter to return to Quincy...
Question 9.)
Code:
#include <iostream>
using namespace std;
class person
{
char name[20];
public:
person(){}
person(char*n)
{
strcpy(name,n);
}
virtual void display()
{
cout<<"\n Name:"<<name;
}
};
class student:public person
{
float marks;
char course[20];
int year;
public:
student():person(){}
student(char n[],float m, char course1[], int y):person (n)
{
marks=m;
strcpy (course , course1);
year=y;
}
void display()
{
person::display();
cout<<"\n Marks"<<marks<<"\n Course"<<course<<"\n Year"<<year;
}
};
class employee:public person
{
char department[20];
float salary;
public:
employee():person(){}
employee(char n[],char d[], float salary1):person(n)
{
strcpy(department,d);
salary=salary1;
}
void display()
{
cout<<"\n Department"<<department<<"\n Salary"<<salary;
}
};
int main()
{
person*bp;
student o1("Riya", 89, "CS", 2024);
employee o2("Ravi", "IT", 343434);
bp=&o1;
bp->display();
bp=&o2;
bp->display();
return 0;
}
Output:
Name:Riya
Marks89
CourseCS
Year2024
DepartmentIT
Salary343434
Press Enter to return to Quincy...
Question 10.)