CPP
CPP
CPP
Question 1 Give the output of the following program #include<iostream.h> struct Pixel { int C,R; }; void Display(Pixel P) { cout<<"Col<<P.C<<"Row"<<P.R<<endl; } void main() { Pixel X={40,50},Y,Z; Z=X; X.C+=10; Y=Z; Y.C+=10; Y.R+=20; Z.C-=15; Display(X); Display(Y); Display(Z); } Question 2 Find the output of the following program: #include <iostream.h> struct PLAY { int Score, Bonus; }; void Calculate(PLAY &P, int N=10) { P.Score++; P.Bonus+=N; } void main() { PLAY PL={10,15}; Calculate(PL,5); cout<<PL.Score<<:<<PL.Bonus<<endl; Calculate(PL); cout<<PL.Score<<:<<PL.Bonus<<endl; Calculate(PL,15); cout<<PL.Score<<:<<PL.Bonus<<endl; } Question 3 Find the output of the following program: #include<iostream.h> struct MyBox { int Length, Breadth, Height; }; void Dimension (MyBox M)
{ cout<<M.Length<<"x"<<M.Breadth<<"x"; cout<<M.Height<<endl; } void main () { MyBox B1={10,15,5}, B2, B3; ++B1.Height; Dimension(B1); B3 = B1; ++B3.Length; B3.Breadth++; Dimension(B3); B2 = B3; B2.Height+=5; B2.Length--; Dimension(B2); } Question 4 Rewrite the following program after removing the syntactical errors (if any). Underline each correction. #include <iostream.h> struct Pixels { int Color, Style; } void ShowPoint(Pixels P) { cout<<P.Color,P.Style<<endl; } void main() { Pixels Point1=(5,3); ShowPoint(Point1); Pixels Point2=Point1; Color.Point1+=2; ShowPoint(Point2); } Question 5 Declare a structure to represent a complex number (a number having a real part and imaginary part). Write C++ functions to add, subtract, multiply and divide two complex numbers. Question 6 An array stores details of 25 students (rollno, name, marks in three subject). Write a program to create such an array and print out a list of students who have failed in more than one subject.
#include<iostream.h> #include<conio.h>
#include<iostream.h> #include<conio.h> int main() { cout<<"subject " <<"\tmarks"<<"\nmathematic\t" <<90<<"\ncomputer\t"<<77<<"\nchemistry\t"<<69; getch(); return 0; }
Write a program which accept two numbers and print their sum
#include<iostream.h> #include<conio.h> int main() { int a,b,c; cout<< "\nEnter first number : "; cin>>a; cout<<"\nEnter second number : "; cin>>b; c=a+b; cout<<"\nThe Sum is : "<<c; getch(); return 0; }
#include<iostream.h> #include<conio.h>
int main() { float F,C; cout<< "\nEnter temperature in Farenheit : "; cin>>F; C=5*(F-32)/9; cout<<"Temperature in celcius is : "<<C; getch(); return 0; }
Write a program which accept principle, rate and time from user and print the simple interest.
#include<iostream.h> #include<conio.h> int main() { int p,r,t,i; cout<<"Enter Principle : "; cin>>p; cout<<"Enter Rate : "; cin>>r; cout<<"Enter Time : "; cin>>t; i=(p*r*t)/100; cout<<"Simple interest is : "<<i; getch(); return 0; }
Write a program which accepts a character and display its ASCII value
#include<iostream.h> #include<conio.h> int main() { char ch; cout<< "\nEnter any character : "; cin>>ch; cout<<"ASCII equivalent is : "<<(int)ch; getch(); return 0; }
#include<iostream.h> #include<conio.h> int main() { int a,b,temp; cout<<"\nEnter two numbers : "; cin>>a>>b; temp=a; a=b; b=temp; cout<<"\nAfter swapping numbers are : "; cout<<a<<" "<<b; getch(); return 0;
}
Write a program to check whether the given number is positive or negative (using ? : ternary operator )
#include<iostream.h> #include<conio.h> int main() { int a; cout<<"Enter any non-zero Number : "; cin>>a; (a>0)?cout<<"Number is positive":cout<<"Number is negative"; getch(); return 0;
}
What is the output of following program? int x = 10; cout<<x<<x++; int x = 10,y; y = x + x++; cout<<y; Question 9 Question 10 int x = 10; cout<<++x<<x++<<x; int x = 10,y; y = ++x + x++ + x; cout<<y; What is the output of following program? cout << setw(10)<< 77; What is the output of following program? float net=5689.2356; cout <<"Employee's net pay is: "; cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << net << endl; int x = 10; cout<<x++<<x<<++x; int x = 10,y; y = x++ + x + ++x; cout<<y;
Write a program which accepts amount as integer and display total number of Notes of Rs. 500, 100, 50, 20, 10, 5 and 1. For example, when user enter a number, 575, the results would be like this... 500: 1 100: 0 50: 1 20: 1 10: 0 5: 1 1: 0
#include<iostream.h> #include<conio.h> int main() { int amt,R500,R100,R50,R20,R10,R5,R1; cout<<"Enter amount : "; cin>>amt; R500=amt/500; amt=amt%500; R100=amt/100; amt=amt%100; R50=amt/50; amt=amt%50; R20=amt/20; amt=amt%20; R10=amt/10; amt=amt%10; R5=amt/5; amt=amt%5; R1=amt; cout<<"Rs.500 : "<<R500<<"\nRs.100 : "<<R100<<"\nRs. 50 : "<<R50<< "\nRs. 20 : "<<R20<<"\nRs. 10 : "<<R10<<"\nRs. 5 : "<<R5<<"\nRe. 1 : "<<R1; getch(); return 0; }
Write a program which accepts a character and display its next character
#include<iostream.h> #include<conio.h> int main() { char ch; cout<< "\nEnter any character : ";
#include<iostream.h> #include<conio.h> int main() { int days,y,m,d; cout<<"Enter no. of days : "; cin>>days; y=days/365; days=days%365; m=days/30; d=days%30; cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d; getch(); return 0;
}
Write a program that takes length as input in feet and inches. The program should then convert the lengths in centimeters and display it on screen. Assume that the given lengths in feet and inches are integers. Based on the problem, you need to design an algorithm as follows: 1. Get the length in feet and inches. 2. Convert the length into total inches. 3. Convert total inches into centimeters. 4. Output centimeters. To calculate the equivalent length in centimeters, you need to multiply the total inches by 2.54. Instead of using the value 2.54 directly in the program, you will declare this value as a named constant. Similarly, to find the total inches, you need to multiply the feet by 12 and add the inches. Instead of using 12 directly in the program, you will also declare this value as a named constant. Using a named constant makes it easier to modify the program later. To write the complete length conversion program, follow these steps: 1. Begin the program with comments for documentation. 2. Include header files, if any are used in the program. 3. Declare named constants, if any. 4. Write the definition of the function main.
int main () { int feet, inches; int totalInches; double centimeter; cout << "Enter two integers, one for feet and one for inches: "; cin >> feet >> inches; cout << endl; cout << "The numbers you entered are " << feet << " for feet and " << inches << " for inches. " << endl; totalInches = INCHES_PER_FOOT * feet + inches; cout << "The total number of inches = " << totalInches << endl; centimeter = CENTIMETERS_PER_INCH * totalInches; cout << "The number of centimeters = " << centimeter << endl; return 0; }
Array
Write a C++ program to find the sum and average of one dimensional integer array#include<iostream.h>
#include<conio.h> int main() { int Arr[100],n,sum=0; cout<<"Enter number of elements you want to insert "; cin>>n; for(int i=0;i<n;i++) { cout<<"Enter element "<<i+1<<":"; cin>>Arr[i]; } for(i=0;i<n;i++) sum+=Arr[i]; cout<<"\nThe sum of Array is :"<<sum; cout<<"\nThe average of Array is :"<<sum/i; getch(); return 0; }
Write a C++ program to swap first and last element of an integer 1-d array.
#include<iostream.h> #include<conio.h> int main() { int Arr[100],n,temp; cout<<"Enter number of elements you want to insert "; cin>>n; for(int i=0;i<n;i++) { cout<<"Enter element "<<i+1<<":"; cin>>Arr[i]; } temp=Arr[0]; Arr[0]=Arr[n-1]; Arr[n-1]=temp; cout<<"\nArray after swapping"<<endl; for(i=0;i<n;i++) cout<<Arr[i]<<" "; getch(); return 0; }
#include<iostream.h> #include<conio.h> int main() { int Arr[100],n,temp,i,j; cout<<"Enter number of elements you want to insert "; cin>>n; for(i=0;i<n;i++) { cout<<"Enter element "<<i+1<<":"; cin>>Arr[i]; } for(i=0,j=n-1;i<n/2;i++,j--) { temp=Arr[i]; Arr[i]=Arr[j]; Arr[j]=temp; }
Write a C++ program to find the largest and smallest element of an array.
#include<iostream.h> #include<conio.h> int main() { int Arr[100],n,small,large; cout<<"Enter number of elements you want to insert "; cin>>n; for(int i=0;i<n;i++) { cout<<"Enter element "<<i+1<<":"; cin>>Arr[i]; } small=Arr[0]; large=Arr[0]; for(i=1;i<n;i++) { if(Arr[i]<small) small=Arr[i]; if(Arr[i]>large) large=Arr[i]; } cout<<"\nLargest element is :"<<large; cout<<"\nSmallest element is :"<<small; getch(); return 0;
}
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 Write C++ functions for all options. The functions should have two parameters name of the array and number of elements in the array
#include<iostream.h> #include<conio.h> void void void void void accept(int Arr[], int s); display(int Arr[], int s); isort(int Arr[], int s); ssort(int Arr[], int s); bsort(int Arr[],int s);
int main() { int Arr[100],n,choice; cout<<"Enter Size of Array "; cin>>n; do { cout<<"\nMENU"; 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 1-6 :"; cin>>choice; switch(choice) { case 1: accept(Arr,n); break; case 2: display(Arr,n); break; case 3: isort(Arr,n); break; case 4: ssort(Arr,n); break; case 5: bsort(Arr,n); break; case 6: break; default:cout<<"\nInvalid choice"; } }while(choice!=6); return 0; } void accept(int Arr[], int s) { for(int I=0;I<s;I++)
{ cout<<"Enter element "<<I+1<<":"; cin>>Arr[I]; } } void display(int Arr[], int s) { cout<<"The elements of the array are:\n"; for(int I=0;I<s;I++) cout<<Arr[I]<<" "; } void isort(int Arr[], int s) { int I,J,Temp; for(I=1;I<s;I++) { Temp=Arr[I]; J=I-1; while((Temp<Arr[J]) && (J>=0)) { Arr[J+1]=Arr[J]; J--; } Arr[J+1]=Temp; } } void ssort(int Arr[], int s) { int I,J,Temp,Small; for(I=0;I<s-1;I++) { Small=I; for(J=I+1;J<s;J++) //finding the smallest element if(Arr[J]<Arr[Small]) Small=J; if(Small!=I) { Temp=Arr[I]; //Swapping Arr[I]=Arr[Small]; Arr[Small]=Temp; } } } void bsort(int Arr[],int s) { int I,J,Temp; for(I=0;I<s-1;I++) //sorting { for(J=0;J<(s-1-I);J++)
#include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: int admno; char sname[20]; float eng,math,science; float total; float ctotal() { return eng+math+science; } public: void Takedata() { cout<<"Enter admission number "; cin>> admno; cout<<"Enter student name " ; gets(sname); cout<< "Enter marks in english, math, science "; cin>>eng>>math>>science; total=ctotal(); }
void Showdata() { cout<<"Admission number "<<admno<<"\nStudent name "<<sname<<"\nEnglish " <<eng<<"\nMath "<<math<<"\nScience "<<science<<"\nTotal "<<total; } }; int main () { clrscr(); student obj ; obj.Takedata(); obj.Showdata(); getch(); return 0; }
Define a class batsman Private members: bcode bname innings, notout, runs batavg calcavg() Public members: readdata() function displaydata() with the following specifications: 4 digits code number 20 characters integer type it is calculated according to the formula batavg =runs/(innings-notout) Function to compute batavg Function to accept value from bcode, name, innings, notout and invoke the calcavg() Function to display the data members on the screen
#include<iostream.h> #include<conio.h> #include<stdio.h> class batsman { int bcode; char bname[20]; int innings,notout,runs; int batavg; void calcavg() { batavg=runs/(innings-notout); } public : void readdata (); void displaydata(); }; void batsman::readdata () {
cout<<"Enter batsman code "; cin>> bcode; cout<<"Enter batsman name "; gets(bname); cout<<"enter innings,notout and runs "; cin>>innings>>notout>>runs; calcavg(); } void batsman::displaydata() { cout<<"Batsman code "<<bcode<<"\nBatsman name "<<bname<<"\nInnings "<<innings <<"\nNot out "<<notout<<"\nRuns "<<runs<<"\nBatting Average "<<batavg; } int main() { batsman obj; obj.readdata(); obj.displaydata(); getch(); return 0; }
Define a class TEST in C++ with following description: Private Members TestCode of type integer Description of type string NoCandidate of type integer CenterReqd (number of centers required) of type integer A member function CALCNTR() to calculate and return the number of centers as (NoCandidates/100+1) Public Members - A function SCHEDULE() to allow user to enter values for TestCode, Description, NoCandidate & call function CALCNTR() to calculate the number of Centres - A function DISPTEST() to allow user to view the content of all the data members
#include<iostream.h> #include<conio.h> #include<stdio.h> class TEST { private: int TestCode; char Description[30]; int NoCandidate; int CenterReqd; int CALCNTR() { return NoCandidate/100+1;
} public: void SCHDULE(); void DISPTEST(); }; void TEST::SCHDULE() { cout<<"Enter Test code "; cin>> TestCode; cout<<"Enter description "; gets(Description); cout<< "Enter no of candidates "; cin>>NoCandidate; CenterReqd=CALCNTR(); } void TEST :: DISPTEST() { cout<<"Test code "<<TestCode<<"\nDescripton "<<Description<<"\nNo of candidate " <<NoCandidate<<"\nCenter required "<<CenterReqd; } int main () { TEST obj; obj.SCHDULE(); obj.DISPTEST(); getch(); return 0; }
Define a class in C++ with following description: Private Members A data member Flight number of type integer A data member Destination of type string A data member Distance of type float A data member Fuel of type float A member function CALFUEL() to calculate the value of Fuel as per the following criteria Distance Fuel <=1000 500 more than 1000 and <=2000 1100 more than 2000 2200 Public Members A function FEEDINFO() to allow user to enter values for Flight Number, Destination, Distance & call function CALFUEL() to calculate the quantity of Fuel A function SHOWINFO() to allow user to view the content of all the data members
class TEST { private: int TestCode; char Description[30]; int NoCandidate; int CenterReqd; int CALCNTR() { return NoCandidate/100+1; } public: void SCHDULE(); void DISPTEST(); }; void TEST::SCHDULE() { cout<<"Enter Test code "; cin>> TestCode; cout<<"Enter description "; gets(Description); cout<< "Enter no of candidates "; cin>>NoCandidate; CenterReqd=CALCNTR(); } void TEST :: DISPTEST() { cout<<"Test code "<<TestCode<<"\nDescripton "<<Description<<"\nNo of candidate " <<NoCandidate<<"\nCenter required "<<CenterReqd; } int main () { TEST obj; obj.SCHDULE(); obj.DISPTEST(); getch(); return 0; }
Define a class BOOK with the following specifications : Private members of the class BOOK are BOOK NO integer type BOOKTITLE 20 characters PRICE float (price per copy) TOTAL_COST() A function to calculate the total cost for N number of copies where N is passed to the function as argument. Public members of the class BOOK are INPUT() function to read BOOK_NO. BOOKTITLE, PRICE
PURCHASE()
function to ask the user to input the number of copies to be purchased. It invokes TOTAL_COST() and prints the total cost to be paid by the user. Note : You are also required to give detailed function definitions.
#include<iostream.h> #include<stdio.h> #include<conio.h> class BOOK { int BOOKNO; char BOOKTITLE[20]; float PRICE; void TOTAL_COST(int N) { float tcost; tcost=PRICE*N; cout<<tcost; } public: void INPUT() { cout<<"Enter Book Number "; cin>>BOOKNO; cout<<"Enter Book Title "; gets(BOOKTITLE); cout<<"Enter price per copy "; cin>>PRICE; } void PURCHASE() { int n; cout<<"Enter number of copies to purchase "; cin>>n; cout<<"Total cost is "; TOTAL_COST(n); } }; int main() { BOOK obj; obj.INPUT(); obj.PURCHASE(); getch(); return 0; }
Inheritance
#include<iostream.h> #include<stdio.h> #include<dos.h> class student { int roll; char name[25]; char add [25]; char *city; public: student() { cout<<"welcome in the student information system"<<endl; } void getdata() { cout<<"\n enter the student roll no."; cin>>roll; cout<<"\n enter the student name"; cin>>name; cout<<\n enter ther student address"; cin>>add; cout<<"\n enter the student city"; cin>>city; } void putdata() { cout<,"\n the student roll no:"<<roll; cout<<"\n the student name:"<<name; cout<<"\n the student coty:"<<city; } }; class mrks: public student { int sub1; int sub2; int sub3; int per; public: void input() { getdata(); cout<<"\n enter the marks1:" cin>>sub1: cout<<"\n enter the marks2:"; cin>>sub2; cout<<\n enter the marks3:"; cin>>sub3; } void output() { putdata(); cout<<"\n marks1:"<<sub1; cout<<"\n marks2:"<<sub2; cout<<"\n marks3:"<<sub3; } void calculate () { per= (sub1+sub2+sub3)/3; cout<<"\n tottal percentage"<<per; } };
void main() { marks m1[25]; int ch; int count=0; do { cout<<\n1.input data"; cout<<\n2.output data"; cout<<\n3. Calculate percentage"; cout<<\n4.exit"; cout<<\n enter the choice"; cin>>ch; switch (ch) { case 1: m1.input(); count++; break; case2: m1.output(); break; case3: m1.calculate(); break; } } while (ch!=4); }