Vous êtes sur la page 1sur 34

CS Program file

Question 1: Declare a structure to represent complex number. Write C++ program to add, subtract,
multiply and divide two complex numbers.
#include<iostream.h>
#include<conio.h>
#include<math.h>
struct complex
{
float rel;
float img;
};
void add(complex s1,complex s2)
{
float a,b;
a=(s1.rel)+(s2.rel);
b=(s1.img)+(s2.img);
cout<<"\nAddition: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
void sub(complex s1,complex s2)
{
float a,b;
a=(s1.rel)-(s2.rel);
b=(s1.img)-(s2.img);
cout<<"\nSubtraction: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
void multi(complex s1,complex s2)
{
float a,b;
a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));
b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));
cout<<"\nMultiplication: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
void div(complex s1,complex s2)
{
float a,b;
a=(((s1.rel)*(s2.rel))+((s1.img)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
b=(((s2.rel)*(s1.img))-((s1.rel)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
cout<<"\nDivision: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
}
void main()
{
clrscr();
complex s1,s2;
int i,n;
char ch;
cout<<"\n Enter the complex no.s :- \n";

cout<<endl;
cout<<"\nEnter real part of 1st complex number :";
cin>>s1.rel;
cout<<"Enter imaginary part of 1st complex number :";
cin>>s1.img;
cout<<endl;
cout<<"\nEnter real part of 2nd complex number :";
cin>>s2.rel;
cout<<"Enter imaginary part of 2nd complex number :";
cin>>s2.img;
while(ch!='n')
{
clrscr();
cout<<"\nEnter your choice :-\n";
cout<<endl;
cout<<"\n1.Add ";
cout<<"\n2.Subtract ";
cout<<"\n3.Multiply ";
cout<<"\n4.Devide \n";
cout<<endl;
cin>>n;
if(n==1)
add(s1,s2);
else if(n==2)
sub(s1,s2);
else if(n==3)
multi(s1,s2);
else if(n==4)
div(s1,s2);
else
cout<<"\nWrong choice !!!\n";
cout<<endl;
cout<<"\nDo you want to continue ? (y/n) \n";
cin>>ch;
}
getch();
}

Question 2: An array to store details of 25 students. Write a menu driven program to create such a
array, display the record of the student, search student admission number wise and print out a list of
students who pay fees more than the average fee.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class student

{
int rno;
char name[25];
float m1,m2,m3;
public:
int getrno(){ return rno; }
float marks(){ return m1+m2+m3; }
void in()
{
cout<<"\nEnter the roll no. :- ";cin>>rno;
cout<<"Enter the name :- ";gets(name);
cout<<"Enter the marks 1 ( Out of 100 ) :- ";cin>>m1;
cout<<"Enter the marks 2 ( Out of 100 ) :- ";cin>>m2;
cout<<"Enter the marks 3 ( Out of 100 ) :- ";cin>>m3;
}
void out()
{
cout<<"\nRoll no. :- ";cout<<rno;
cout<<"\nName :- ";puts(name);
cout<<"Marks 1 :- ";cout<<m1;
cout<<" Marks 2 :- ";cout<<m2;
cout<<" Marks 3 :- ";cout<<m3;
}
}s[25];
void main()
{
clrscr();
int i,n,x,r;
char ch,na[20];

while(ch!='n')
{
clrscr();
cout<<"\nEnter your choice :-\n";
cout<<endl;
cout<<"\n1.Add records ";
cout<<"\n2.Display records";
cout<<"\n3.Search for records";
cout<<"\n4.Students who failed in more than one subject\n";
cout<<endl;
cin>>x;
if(x==1)
{
clrscr();
cout<<"Enter the no. of entries\n";
cin>>n;

for(i=0;i<n;i++)
s[i].in();
}
else if(x==2)
{
clrscr();
for(i=0;i<n;i++)
{
s[i].out();
cout<<endl;
}
}
else if(x==3)
{
clrscr();
cout<<"Enter the roll no. :- ";cin>>r;
for(i=0;i<n;i++)
{
if(r==s[i].getrno())
s[i].out();
}
}
else if(x==4)
{
int pos=0;
for(i=0;i<n;i++)
{
if(s[i].marks()<99)
pos=i;
}
s[pos].out();
}
else
cout<<"\nWrong choice !!!\n";
cout<<endl;
cout<<"\nDo you want to continue ? (y/n) \n";
cin>>ch;
}
getch();
}

Question 3: Write a menu driven program to input a string and give various output using pointer
method.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();

char str1[80],str2[80],temp;
int ch,i,j,flag=1,l,words=0;
cout<<"Enter string:\t";
cin.getline(str1,80);
cout<<"==>What do you wish to do....?\n";
cout<<" 1. Count the length of string\n";
cout<<" 2. Count number of words in string\n";
cout<<" 3. Concatenate one string contents to another\n";
cout<<" 4. Compare two strings they are exact equal or not\n";
cout<<" 5. Check a string is palindrome or not\n";
cout<<" 6. Find a substring within a string. If found display its starting position.\n";
cout<<" 7. Reverse a string.\n";
cout<<" 8. Convert a string in uppercase.\n";
cout<<" 9. Exit\n";
cin>>ch;
switch (ch)
{
case 1:for (i=0;str1[i]!='\0';i++);
cout<<"Length of string is:\t"<<i;
break;
case 2:for (i=0;str1[i]!='\0';i++)
{
if (str1[i]==' ')
{
words++;
}
}
cout<<"The number of words:\t"<<words+1<<endl;
break;
case 3:cout<<"Enter second string:\t";
gets(str2);
for (l=0;str1[l]!='\0';l++);
for (i=0;str2[i]!='\0';i++)
{
str1[l++]=str2[i];
}
str1[l]='\0';
cout<<"\nThe first string after adding second string's content:\t"<<str1;
break;
case 4:cout<<"Enter second string: ";
gets(str2);
for (i=0; str1[i]==str2[i] && str1[i]!='\0' && str2[i]!='\0' ;i++);
if (str1[i]-str2[i]==0)
cout<<"Strings are equal";
else
cout<<"Strings are not equal";
break;
case 5:for (int g=0;str1[g]!='\0';g++);
for (i=0,j=g-1; i<g/2 ;i++,j--)
{
if (str1[i]!=str1[j])
{

flag=0;
break;
}
}
if (flag==1)
cout<<"Given string is Palindrome";
else
cout<<"Given string is not Palindrome";
break;
case 6:cout<<"Enter sub-stringto be found:\t";
gets(str2);
for (l=0;str2[l]!='\0';l++);
for (i=0,j=0; str1[i]!='\0' && str2[j]!='\0' ;i++)
{
if (str1[i] == str2[j])
{
j++;
}
else
{
j=0;
}
}
if (j==l)
cout<<"Substring found at position "<<i-j+1;
else
cout<<"Substring not found";
break;
case 7:for (l=0;str1[l]!='\0';l++);
for (i=0,j=l-1; i<l/2 ;i++,j--)
{
temp=str1[i];
str1[i]=str1[j];
str1[j]=temp;
}
cout<<"Reverse string:\t"<<str1;
break;
case 8:for (i=0;str1[i]!='\0';i++)
{
if ( str1[i]>='A' && str1[i]<='Z' )
str1[i]+=32;
else if ( str1[i]>='a' && str1[i]<='z' )
str1[i]-=32;
}
break;
}
getch();
}

Question 4: Program to maintain employee records using binary files and classes.
#include<fstream.h>
#include<conio.h>

#include<stdio.h>
#include<string.h>
#include<process.h>
class emp
{
int empno;
char name[20];
float sal;
public:
void in()
{
cout<<"Enter name of the employee:\t\t";
gets(name);
cout<<"Enter salary paid to the employee:\t";
cin>>sal;
}
void out()
{
cout<<"\n\nEmployee number:\t\t";
cout<<empno;
cout<<"\nName of the employee:\t\t";
puts(name);
cout<<"Salary paid to the employee:\t";
cout<<sal;
}
char *getname()
{
return name;
}
int getempno()
{
return empno;
}
float getsal()
{
return sal;
}
void setempno(int a)
{
empno=a;
}
void modify();
};
void emp::modify()
{
char ch;
do
{
clrscr();
cout<<"Fields available for modification:\n";
cout<<" 1. Salary paid to employee\n";
cout<<" 2. Return\n";
cin>>ch;
} while (ch<='0'||ch>'2');

switch (ch)
{
case '1':cout<<"Enter new salary for the employee:\t";
cin>>sal;
cout<<"Success....\n";
break;
case '2':cout<<"No problem....\n";
break;
}
}
void main()
{
clrscr();
int n=0,flag=0,no;
char a,name[20];
float sal;
emp e,e1;
ofstream f1;
ifstream f2;
fstream f;
do
{
do
{
clrscr();
cout<<"What do you wish to do....?\n";
cout<<" 1. Enter a record\n";
cout<<" 2. Display all records\n";
cout<<" 3. Search and display\n";
cout<<" 4. Insert a new record\n";
cout<<" 5. Delete a record\n";
cout<<" 6. Modify existing data\n";
cout<<" 7. Delete record file\n";
cout<<" 8. Exit\n";
cin>>a;
} while (a<='0'||a>'8');
clrscr();
switch (a)
{
case '1':f2.open("Employee",ios::binary);
if (f2)
{
while (f2)
{
f2.read((char*)&e,sizeof(emp));
}
n=e.getempno()+1;
}
else
n=1;
f2.close();
f1.open("Employee",ios::binary|ios::app);

do
{
clrscr();
e.in();
e.setempno(n);
n++;
f1.write((char*)&e,sizeof(emp));
clrscr();
cout<<"Do you wish to enter more....?\n";
cin>>a;
} while (a=='y'||a=='Y');
f1.close();
clrscr();
cout<<"Thank You for entering....\n";
break;
case '2':f2.open("Employee",ios::binary);
f2.read((char*)&e,sizeof(emp));
while (f2)
{
e.out();
f2.read((char*)&e,sizeof(emp));
}
f2.close();
break;
case '3':do
{
cout<<"Enter by which field you wish to search:\n";
cout<<" 1. Employee number\n";
cout<<" 2. Name of the employee\n";
cout<<" 3. Salary paid by employee\n";
cin>>a;
} while (a<='0'||a>'3');
f2.open("Employee",ios::binary);
switch (a)
{
case '1':cout<<"Enter employee number to be searched:\t";
cin>>no;
f2.read((char*)&e,sizeof(emp));
while (f2)
{
if (e.getempno()==no)
{
e.out();
flag=1;
}
f2.read((char*)&e,sizeof(emp));
}
if (flag==0)
cout<<"Record not found....!";
flag=0;
break;
case '2':cout<<"Enter name to be searched";
gets(name);

f2.read((char*)&e,sizeof(emp));
while (f2)
{
if(strcmp(e.getname(),name)==0)
{
e.out();
flag=1;
}
f2.read((char*)&e,sizeof(emp));
}
if (flag==0)
cout<<"Record not found....!";
flag=0;
break;
case '3':cout<<"Enter salary paid to employee to be searched:\t";
cin>>sal;
f2.read((char*)&e,sizeof(emp));
while (f2)
{
if (e.getsal()==sal)
{
e.out();
flag=1;
}
f2.read((char*)&e,sizeof(emp));
}
if (flag==0)
cout<<"Record not found....!";
flag=0;
break;
}
f2.close();
break;
case '4':cout<<"Enter employee number:\t\t\t";
cin>>no;
e1.setempno(no);
e1.in();
f2.open("Employee",ios::binary);
if (!f2)
{
f2.close();
f1.open("Employee",ios::binary);
f1.write((char*)&e1,sizeof(emp));
f1.close();
}
else
{
f1.open("Temp",ios::binary);
f2.read((char*)&e,sizeof(emp));
while (f2)
{
if (e1.getempno()>=e.getempno())
{
f1.write((char*)&e,sizeof(emp));

}
else
{
f1.write((char*)&e1,sizeof(emp));
flag=1;
break;
}
f2.read((char*)&e,sizeof(emp));
}
if (flag==0)
f1.write((char*)&e1,sizeof(emp));
flag=0;
if (f2)
{
while (f2)
{
f1.write((char*)&e,sizeof(emp));
f2.read((char*)&e,sizeof(emp));
}
}
f2.close();
f1.close();
remove("Employee");
rename("Temp","Employee");
}
cout<<"Success....\n";
break;
case '5':cout<<"Enter employee number of that record:\t";
cin>>no;
f2.open("Employee",ios::binary);
f1.open("Temp",ios::binary);
f2.read((char*)&e,sizeof(emp));
while (f2)
{
if (e.getempno()==no)
{
clrscr();
e.out();
cout<<"\nDo you still wish to delete this record....?\n";
cin>>a;
if (a=='n'||a=='N')
f1.write((char*)&e,sizeof(emp));
}
else
f1.write((char*)&e,sizeof(emp));
f2.read((char*)&e,sizeof(emp));
}
f1.close();
f2.close();
remove("Employee");
rename("Temp","Employee");
cout<<"Success....\n";
break;

case '6':clrscr();
cout<<"Enter your employee number:\t";
cin>>no;
f.open("Employee",ios::binary|ios::in|ios::out);
while (f.read((char*)&e,sizeof(emp)))
{
if (e.getempno()==no)
{
cout<<"\nExisting record:";
e.out();
getch();
cout<<"\nDo you wish to modify the data:\t";
cin>>a;
if (a=='y'||a=='Y')
{
e.modify();
f.seekg(f.tellg()-sizeof(emp));
f.write((char*)&e,sizeof(emp));
flag=1;
}
else
{
cout<<"As you wish....\n";
flag=1;
}
}
}
if (flag==0)
cout<<"Record not found....!\n";
flag=0;
f.close();
break;
case '7':remove("Employee");
cout<<"Success....\n";
break;
case '8':exit(0);
}
getch();
clrscr();
cout<<"Do you wish to do somethin' else....?\n";
cin>>a;
} while (a=='y'||a=='Y');
}
Question 5: Program to perform operations on a text file.
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
void main()

{
clrscr();
ifstream f("Out.txt");
int w=0,c=0,v=0;
char ch,word[20],s[80];
while (f.get(ch))
{
if ( (ch>=65&&ch<=90) || (ch>=97&&ch<=122) )
c++;
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
v++;
}
f.seekg(0);
while (!f.eof())
{
f>>ch;
w++;
}
cout<<"\nNumber of alphabets:\t"<<c;
cout<<"\nNumber of vovels:\t"<<v;
cout<<"\nNumber of words:\t\t"<<w;
getch();
clrscr();
f.seekg(0);
while (!f.eof())
{
for (int i=0;i<10;i++)
{
f.getline(s,80);
puts(s);
}
}
getch();
f.close();
}
Question 6: Create a candy machine.
#include<conio.h>
#include<iostream.h>
class dispensor
{ int stock,cost,id;
public:
dispensor()
{stock=50;
cost=50;
}
dispensor(int a,int b,int p)
{stock=a;
cost=b;
id=p;

}
dispensor(int a)
{id=a;
stock=50;
cost=50;
}
int getnoitem()
{return stock;
}
void makesale()
{stock--;
}
void stockdisplay(dispensor candy,dispensor chip,dispensor cookie)
{cout<<endl<<"Item\t\tStock\tPrice\n";
cout<<"1. Candy\t"<<candy.stock<<"\t"<<candy.cost<<"\\-"<<"\n";
cout<<"2. Gum\t\t"<<stock<<"\t"<<cost<<"\\-"<<"\n";
cout<<"3. Chips\t"<<chip.stock<<"\t"<<chip.cost<<"\\-"<<"\n";
cout<<"4. Cookies(2)\t"<<cookie.stock<<"\t"<<cookie.cost<<"\\-"<<"\n";
}
int disp(int i,int j,dispensor &candy,dispensor &chip,dispensor &cookie);
}candy(1),gum(2),chip(40,10,3),cookie(4);
int dispensor::disp(int i,int j,dispensor &candy,dispensor &chip,dispensor &cookie)
{ if (i==id)
{stock-=j;
return cost*j;}
else if (i==candy.id)
{candy.stock-=j;
return candy.cost*j;}
else if (i==chip.id)
{ chip.stock-=j;
return chip.cost*j;}
else if (i==cookie.id)
{ chip.stock-=j;
return cookie.cost*j; }
else
return 0;
}
class cashregistor
{long cashonhand;
public:
cashregistor()
{ cashonhand=0;
}
cashregistor(int a)
{cashonhand=a;
}
int gcbalance()
{return cashonhand;
}
void acbalance(long a)
{ cashonhand+=a;
}

};
int checkpass()
{int i;char a[]="asv",b[3];
cout<<"Enter Password\n";
for(i=0;i<3;i++)
{b[i]=getch();
cout<<"*";
if(a[i]==b[i]);
else
break;
}
if(i==3)
return 1;
else
return 0;
}
void main()
{int p,q;
cashregistor c;
char ch='e',ch1='e';
do{ clrscr();
cout<<"1. BUY CANDY!!\n"<<"2. SEE MENU \n3. Owner options\n4. EXIT";
cin>>p;
switch(p)
{
case 1:gum.stockdisplay(candy,chip,cookie);
int a,j,i;
cout<<"Enter Choice ";
cin>>i;
cout<<"Enter amount ";
cin>>j;
a=gum.disp(i,j,candy,chip,cookie);
cout<<"ENter "<<a<<"Rupees\n";
getch();
cout<<"Money accepted";
getch();
c.acbalance(a);
break;
case 2:gum.stockdisplay(candy,chip,cookie);
getch();
break;
case 3:q=checkpass();
if(q==0)
{cout<<"WRONG PASSWORD ";
getch();
break;}
else
do{ clrscr();
int r;
cout<<"1.Check Money in machine\n2.Withdraw Money\n3.Return to main menu\n";
cin>>r;
clrscr();
switch (r)
{ case 1: cout<<"Money in machine "<<c.gcbalance()<<"\\-";

getch();
clrscr();
break;
case 2: int i;
cout<<"Enter Amount to Withdraw";
cin>>i;
if(c.gcbalance()<i)
{cout<<"NOT ENOUGH MONEY ";
getch();}
else
{c.acbalance(-1*i);
cout<<"Withdrawal Succesful ";
getch();
}
break;
case 3: ch1='o';
break;
default :cout<<"WRONG CHOICE ";
break;
}
}while(ch1!='o');
break;
case 4: ch='o';
break;
default:cout<<"Wrong Choice ";
getch();
}
}while(ch!='o');
}
Question 7: Program to check special conditions for numbers
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
char ch;
int n,m,i=1,j,count=0,flag=1,sum=0,remainder,b,temp,fact;
do
{
clrscr();
cout<<"What do you want to do....?\n";
cout<<" 1. To check for prime number\n";
cout<<" 2. To check for palindrome number\n";
cout<<" 3. To check for armstrong number\n";
cout<<" 4. To check for perfect number\n";
cout<<" 5. To check for special number\b";
cin>>ch;
} while (ch<='0'||ch>'5');
switch (ch)

{
case '1':cout<<"Enter a number:\t";
cin>>n;
temp=n;
for (i=2;i<=n/2;i++)
{
if (n%i==0)
flag=0;
}
if (flag)
cout<<temp<<" is a prime number";
else
cout<<temp<<" is not a prime number";
break;
case '2':cout<<"Enter the number to find:\t";
cin>>n;
temp=n;
while (n>0)
{
remainder=n%10;
sum=sum*10+remainder;
n=n/10;
}
if (sum==temp)
cout<<temp<<" is a palindrome number....\n";
else
cout<<temp<<"is not a palindrome number....\n";
break;
case '3':cout<<"Enter the number to find:\t";
cin>>n;
b=n;
temp=n;
while (temp)
{
temp=temp/10;
count++;
}
while (n>0)
{
remainder=n%10;
sum=sum+pow(remainder,count);
n=n/10;
}
if (b==sum)
cout<<b<<" is an amstrong number....";
else
cout<<b<<" is not an amstrong number....";
break;
case '4':cout<<"Enter a number:\t";
cin>>n;
while (i<n/2)
{

if (n%i==0)
sum=sum+i;
i++;
}
if (sum==n)
cout<<n<<" is a perfect number";
else
cout<<n<<" is not a perfect number";
break;
case '5':cout<<"Enter the number:\t";
cin>>n;
temp=n;
while (temp)
{
fact=1;
for(i=1;i<=n;i++)
{
fact*=count;
}
sum+=fact;
temp/=10;
}
if (n==sum)
cout<<n<<" is a special number";
else
cout<<n<<" is not a special number";
break;
}
getch();
}
Question 8: Write a menu driven program to calculate area of square, rectangle, circle and triangle
using function overloading concept.
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
float area(float a,float b, float c)
{
float s,ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
float area(float a,float b)
{
return a*b;
}
float area(float a)

{
return a*a;
}
float area (int r)
{
int ar;
ar=(3.14*r*r);
return ar;
}
void main()
{
clrscr();
int ch,ar;
char choice='y';
cout<<"*********MENU*********";
cout<<"\n1)To Calculate Area of Triangle";
cout<<"\n2)To Calculate Area of Rectangle";
cout<<"\n3)To Calculate Area of Square";
cout<<"\n4)To Calculate Area of Circle";
cout<<"\n5)exit";
while(choice=='y')
{
cout<<"\n\nEnter Your Choice:";
cin>>ch;
if(ch==1)
{
float s1,s2,s3;
cout<<"\nEnter The Three Sides:";
cout<<"\nSide 1 :";
cin>>s1;
cout<<"\nSide 2 :";
cin>>s2;
cout<<"\nSide 3 :";
cin>>s3;
ar=area(s1,s2,s3);
cout<<"The Area Of The Triangle is: "<<ar<<"sq units";
}
if(ch==2)
{
float l,b;
cout<<"Enter The Length and Breadth of the Rectangle:";
cout<<"\nLength : ";
cin>>l;
cout<<"\nBreadth : ";
cin>>b;
ar=area(l,b);
cout<<"The Area Of The Reactangle is : "<<ar<<" sq units";
}
if(ch==3)

{
float s;
cout<<"Enter The Side of The Square : ";
cin>>s;
ar=area(s);
cout<<"The Area Of The Square is : "<<ar<<" sq units";
}
if(ch==4)
{
int r;
cout<<"Enter The Radius Of The Circle : ";
cin>>r;
ar=area(r);
cout<<"The Area Of The Circle is: "<<ar<<" sq units";
}
if(ch==5)
exit(0);
cout<<"\nDo You Wish To Conitnue(y/n) :";
cin>>choice;
}
getch();
}
Question 9: Write a program to covert the binary number in to decimal and vice versa.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int ch;
cout<<"\n*******************MENU*****************";
cout<<"\n1)Convert a Binary Number into Decimal Number";
cout<<"\n2)Convert a Decimal Number into Binary Number";
cout<<"\n\nEnter Your Choice";
cin>>ch;
if(ch==1)
{
long n;
cout<<"\nEnter a Binary Number:";
cin>>n;
int decimalno=0,i=0,remainder;
while(n!=0)
{
remainder=n%10;
n=n/10;
decimalno+= remainder*pow(2,i);
i++;

}
cout<<"\nThe Number in Decimal Form is:"<<decimalno;
}
else if(ch==2)
{
int n;
cout<<"\nEnter a Decimal Number:";
cin>>n;
long binaryno=0;
int remainder,i=1;
while(n!=0)
{
remainder=n%2;
n=n/2;
binaryno += remainder*i;
i*=10;
}
cout<<"\nThe Number in Binary Form is"<<binaryno;
}
getch();
}

Question 10: Write a menu driven C++ program to do following operation on two-dimensional array A of
size m x n. You should use user-defined functions which accept 2-D Array A, and its size m and n as
arguments. The options are:
To input elements into matrix of size m x n
To display elements of matrix of size m x n
Sum of all elements of matrix of size m x n
To display row-wise sum of matrix of size m x n
To display column-wise sum of matrix of size m x n
To create transpose of matrix B of size n x m
Main diagonal sum
Alternate diagonal sum
#include<iostream.h>
#include<conio.h>
void in(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<" Enter The Element["<<i+1<<"]["<<j+1<<"] : ";
cin>>a[i][j];
}
}
}

void out(int a[][10],int m,int n)


{
int i,j;
cout<<"\nThe Matrix is:\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
getch();
}
void sum(int a[][10],int m,int n)
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
sum+=a[i][j];
}
cout<<"\nThe Sum Of All Elements of Matrix is ";
cout<<sum;
}
void rowsum(int a[][10],int m,int n)
{
int i,j,sum1;
for(i=0;i<m;i++)
{
sum1=0;
for(j=0;j<n;j++)
sum1+=a[i][j];
cout<<"\nSum Of All Elements of Row "<<i+1<<" is :"<<sum1;
}
}
void columnsum(int a[][10],int m,int n)
{
int sum2,j,i;
for(i=0;i<m;i++)
{
sum2=0;
for(j=0;j<n;j++)
sum2+=a[i][j];
cout<<"\nSum Of All Elements of Column "<<i+1<<" is :";
cout<<sum2;
}
}
void transpose(int a[][10],int m,int n)
{
int b[10][10],i,j;
cout<<"\nTranspose of The Given Matrix is :\n ";
for(i=0;i<m;i++)

{
for(j=0;j<n;j++)
{ b[i][j]=a[j][i];
cout<<b[i][j]<<" ";
}
cout<<endl;
}
}
void maindiag(int a[][10],int m,int n)
{
int i,j,sum3=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sum3+=a[i][j];
}
}
cout<<"\nThe Main Diagonal Sum is :"<<sum3;
}
void altdiag(int a[][10],int m,int n)
{
int i,j,sum4=0;
for(i=0;i<m;i++)
{
for(j=n;j>=0;j--)
{
if(i==j)
sum4+=a[i][j];
}
}
cout<<"\nThe Alternate Diagonal Sum is :"<<sum4;
}
void main()
{
clrscr();
int m,n,a[10][10],i,j,p,ch;
char choice='y';
while(choice=='y')
{
cout<<"**********MATRIX-MENU**********";
cout<<"\n1)To Input Elements into a Matrix";
cout<<"\n2)To Display Elements of Matrix";
cout<<"\n3)To Display Sum of All Elements";
cout<<"\n4)To Display Row-Wise Sum of Matrix";
cout<<"\n5)To Display Column-Wise Sum of Matrix";
cout<<"\n6)To Create a Transpose of Matrix";
cout<<"\n7)To Display Main Diagonal Sum";

cout<<"\n8)To Display Alternate Diagonal Sum";


cout<<"\n9)exit";
cout<<"\nEnter Your Choice:";
cin>>ch;
cout<<"\nEnter the number of Rows:";
cin>>m;
cout<<"\nEnter the Number ofColumns:";
cin>>n;
if(ch==1)
in(a,m,n);
else if(ch==2)
{
in(a,m,n);
out(a,m,n);
}
else if(ch==3)
{
in(a,m,n);
sum(a,m,n);
}
else if(ch==4)
{
in(a,m,n);
rowsum(a,m,n);
}
else if(ch==5)
{
in(a,m,n);
columnsum(a,m,n);
}
else if(ch==6)
{
in(a,m,n);
transpose(a,m,n);
}
else if(ch==7)
{
if(m%2==0)
cout<<"\nMain Diagonal Sum Can't Be Calculated !!!!";
else if(m==n)
{in(a,m,n);
maindiag(a,m,n);}
else
cout<<"\nMain Diagonal Sum Can't Be Calculated !!!!";
}
else if(ch==8)
{
if(m%2==0)
cout<<"\nAlternate Diagonal Sum Can't Be Calculated !!!!";
else if(m==n)
{in(a,m,n);
altdiag(a,m,n);}
else
cout<<"\nMain Diagonal Sum Can't Be Calculated !!!!";
}

cout<<"\n Do You Wish To Continue(y/n)?";


cin>>choice;
}
getch();
}

Question 11: Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
#include<iostream.h>
#include<conio.h>
void accept(int a[],int s)
{
for(int i=0;i<s;i++)
{
cout<<"\nEnter The Element "<<i+1<<" : ";
cin>>a[i];
}
}
void display(int a[],int s)
{
cout<<"\nThe Elements Of The Array are : ";
for(int i=0;i<s;i++)
cout<<a[i]<<" ";
}
void isort(int a[],int s)
{
int i,j,temp;
for(i=1;i<s;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
}
void ssort(int a[],int s)
{
int i,j,temp,small;

for(i=0;i<s-1;i++)
{
small = i;
for(j=i+1;j<s;j++)
if(a[j]<a[small])
small=j;
if(small!=i)
{
temp a[i];
a[i]=a[small];
a[small]=temp;
}
}
}
void bsort(int a[],int s)
{
int i,j,temp;
for(i=0;i<s-1;i++)
{
for(j=0;j<s-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void main()
{
int a[100],n,choice;
cout<<"Enter The Size of the Array ";
cin>>n;
do
{
cout<<"\n\n**************MENU*****************";
cout<<"\n1)Accept Elements of Array";
cout<<"\n2)Display Elements of Array";
cout<<"\n3)Sort The Array Using Insertion Sort Method";
cout<<"\n4)Sort The Array Using Selection Sort Method";
cout<<"\n5)Sort The Array Using Bubble Sort Method";
cout<<"\n6)Exit";
cout<<"\n\nEnter Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:accept(a,n);
break;
case 2:display(a,n);
break;
case 3:isort(a,n);
break;

case 4:ssort(a,n);
break;
case 5:bsort(a,n);
break;
case 6:break;
default:cout<<"\n\nInvalid Choice ";
}
}while(choice!=6);
getch();
}
Question 12: Suppose a one-dimensional array A containing integers is arranged in ascending order.
Write a user-defined function in C++ to search for an integer from AR with the help of Binary search
method, returning an integer 0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters: (i) array AR (ii) the number to be searched
and (iii) the number of elements N in the array.
#include<iostream.h>
#include<conio.h>
int bsearch (int ar[],int n,int val)
{
int mid,l=0,u=n-1;
while(l<u)
{
mid=(l+u)/2;
if(val>ar[mid])
l=mid+1;
else if(val<ar[mid])
u=mid-1;
else
return 1;
}
return 0;
}
void main()
{
int ar[100],n,val;
int found;
cout<<"\nEnter Number Of Elements You Want To Insert : ";
cin>>n;
cout<<"\nEnter The Elements in Ascending Order\n";
for(int i=0;i<n;i++)
{
cout<<"\nEnter Element "<<i+1<<" : ";
cin>>ar[i];
}
cout<<"\nEnter The Element You Want To Search : ";
cin>>val;
found=bsearch(ar,n,val);
if(found==1)

{
cout<<"\nItem Found";
}
else
cout<<"\n Item Not Found ";
getch();
}

Question 13: WAP to input an array and display the following menu:
a. Sort the array
b. Insert an element in the array
c. Delete an element from the array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[100],i,n,choice;
char ch='y';
while(ch=='y')
{
clrscr();
cout<<"Enter The Size Of The Array : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter Element "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\n**************MENU***********";
cout<<"\n1)Sort the Array";
cout<<"\n2)Insert An Element To Array";
cout<<"\n3)Delete An Element from Array";
cout<<"\n\nEnter Your Choice : ";
cin>>choice;
if(choice==1)
{
int i1,j,temp;
for(i1=0;i1<n;i1++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}

}
cout<<"The Sorted Array is: ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
if(choice==2)
{
int ele,pos,i2;
cout<<"Enter The Element To Be inserted : ";
cin>>ele;
if(ele<a[0])
pos=0;
else if(ele>a[n-1])
pos=n;
else
{
for(i2=0;i2<n-1;i2++)
{
if(ele>=a[i2]&&ele<a[i2+1])
pos=i2+1;
}
}
cout<<endl;
for(int j=n;j>pos;j--)
{
a[j]=a[j-1];
a[pos]=ele;
n++;
}
cout<<"\nAfter Insertion The Array is : ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
}
if(choice==3)
{
int element,i3,pos=-1;
cout<<"Ente the Element To Be Deleted : ";
cin>>element;
for(i3=0;i3<n;i3++)
{
if(element==a[i3])
pos=i3;
}
if(pos>-1)
{
for(i3=pos;i3<n;i3++)
{
a[i3]=a[i3+1];
n--;
}
for(int k=0;k<n;k++)
cout<<a[k]<<" ";
}

else
cout<<"\nElement Not Found!!!";
}
cout<<"\nDo You Wish To Continue(y/n)? ";
cin>>ch;
}
getch();
}

Question 14: Write a program to display the following menu and do all the task using recursive
programming:
1. Producing Fibonacci series
2. Sum of n natural numbers
3. Factorial of a number
4. HCF of two numbers
#include<iostream.h>
#include<conio.h>
int fibonacci(int n)
{
if(n<=0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return (fibonacci(n-1)+fibonacci(n-2));
}
}
int add(int n)
{
if(n!=0)
return n+add(n-1);
}
int hcf(int n1,int n2)
{
if(n2!=0)
return hcf(n2,n1%n2);
else
return n1;}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}

void main()
{
int ch;
char choice='y';
clrscr();
while(choice=='y')
{
cout<<"\n*************MENU***************";
cout<<"\n1)Fibonaaci Series Through Recursion";
cout<<"\n2)Sum Of N Natural Numbers Through Recursion";
cout<<"\n3)Factorial Of a Number Through Recursion";
cout<<"\n4)HCF of Two Numbers Through Recursion";
cout<<"\n\nEnter Your Choice : ";
cin>>ch;
if(ch==1)
{
int m;
cout<<"\nEnter The Till Which Fibonacci Series Is To Be Printed : ";
cin>>m;
for(int x=0;x<m;x++)
cout<<fibonacci(x)<<" ";
cout<<endl;
}
if(ch==2)
{
int a;
cout<<"\nEnter The Number To Which Sum is To Be Calculated : ";
cin>>a;
cout<<"\nSum = "<<add(a);
}
if(ch==3)
{
int b;
cout<<"\nEnter The Number Whose Factorial is To Be Calculated : ";
cin>>b;
cout<<"\nFactorial of Given Number is : "<<factorial(b);
}
if(ch==4)
{
int n1,n2;
cout<<"\nEnter The Two Numbers : \n";
cin>>n1>>n2;
cout<<"\nH.C.F. of "<<n1<<" & "<<n2<<" is : "<<hcf(n1,n2);
}
cout<<"\nDo You Wish To Continue (y/n)?";
cin>>choice;
}
getch();
}

Question 15: Write a Menu Driven Program to print following patterns:


a.

**
***
****
*****
b.
*
***
*****
*******
*********
c.
1
222
33333
4444444
555555555
d.
*
*
*
*
*
************
e.

1
212
32123
4321234
543212345
#include<iostream.h>
#include<conio.h>
void main()
{
int ch;
cout<<"\n************ PATTERN-MENU **************";
cout<<"\n1) * ";
cout<<"\n * *";
cout<<"\n * * *";
cout<<"\n2)
* ";
cout<<"\n
* * * ";
cout<<"\n * * * *";
cout<<"\n3)
1 ";
cout<<"\n
222";
cout<<"\n
33333";
cout<<"\n
4444444";
cout<<"\n4)
*";
cout<<"\n
*
*";
cout<<"\n * * * * * *";
cout<<"\n5)
1";
cout<<"\n
212";
cout<<"\n
32123";
cout<<"\n
4321234";
cout<<"\n\nEnter Your Choice : ";

cin>>ch;
if(ch==1)
{
int i,j,n;
cout<<"Enter Number Of Rows : ";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
}
else if(ch==2)
{
int i,s,r,k=0;
cout<<"Enter The Number Of Rows : ";
cin>>r;
for(i=1;i<=r;i++)
{
for(s=1;s<=r-i;++s)
{
cout<<" ";
}
while(k!=2*i-1)
{
cout<<"* ";
++k;
}
k=0;
cout<<"\n";
}
}
else if(ch==3)
{
int i,s,r,k=0;
cout<<"Enter The Number Of Rows : ";
cin>>r;
int j=1;
for(i=1;i<=r;i++)
{
for(s=1;s<=r-i;++s)
{
cout<<" ";
}
while(k!=2*i-1)
{
cout<<j<<" ";
++k;
}
k=0;
j++;
cout<<"\n";
}

}
else if(ch==4)
{
int n,z=1;
cout<<"Enter the Number of Rows : ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=n;j>i;j--)
{
cout<<" ";
}
cout<<"*";
if(i!=0)
{
for(int k=1;k<=z;k++)
{
cout<<" ";
}
cout<<"*";
z+=2;
}
cout<<endl;
}
for(int i1=0;i1<=z+1;i1++)
{
cout<<"*";
}
}
if(ch==5)
{
int i,j,k,l,n;
cout<<"Enter The Number Of Rows : ";
cin>>n;
for(i=1;i<n;i++)
{
for(j=n;j>i;j--)
cout<<' ';
for(k=i;k>=1;k--)
cout<<k;
for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}
}
getch();
}

Vous aimerez peut-être aussi