C++

52
REPORT FILE MAITREYI SINGH CLASS – XI/A ROLL NO. – 26

Transcript of C++

Page 1: C++

REPORT FILE

MAITREYI SINGH

CLASS – XI/A

ROLL NO. – 26

Page 2: C++

CERTIFICATE

This is to certify that the presentation and documentation of this report file is successfully completed by Maitreyi Singh of grade XI of Delhi Public School, Greater Noida in the session 2010-2011 under the guidance and supervision of undersigned.

Signature:

Mr. Narendra Singh

(Computer Teacher)

Delhi Pubic School, Greater Noida

2

Page 3: C++

CONTENTS

Topic Page no.

(a) Flow of Control:-(i) Display pattern 6(ii) Display odd nos. 6-7(iii) Prime 7-8(iv) Identification of character 8-9(v) Average 9-10(vi) Reversing a number 9-11(vii) Truth table 11-12(viii) Palindrome 12-13

(b) Array:-(i) Binary search 15(ii) Selection sort 16(iii) Bubble sort 16-17(iv) Insert sort 17-18(v) Arranging text values 18-19(vi) Character count 19-20(vii) Lucky number 20-21(ix) Displaying diagonal elements 21-23

3

Page 4: C++

(x) Difference of diagonals 23-25(xi) Prime number count in diagonals 25-27

(c) Functions:-(i) Creation, traversal, insertion,

deletion and sorting 29-33(ii) Sum of digits of number 33-34(iii) Swap values 34-35(iv) Convert into feets/inches 35-37(v) arithmetic operator 37-38

(d) Structures:-(i) Modifying time 40(ii) Print student result 41-42

(e) Teacher’s Remarks 43

4

Page 5: C++

5

Page 6: C++

Q1. Write a C++ program to display the following :11 21 2 31 2 3 41 2 3 4 5

Sol: #include<iostream.h> #include<conio.h> void main( )

{clrscr( );int i,j;for(i=1; i<=5; i++){

for(j=1; j<=i; j++)cout<<j<<’\t’;

cout<<endl;}getch( );

}

o/p – 11 2

1 2 3 1 2 3 4 1 2 3 4 5

6

Page 7: C++

Q2. Write a C++ program to display odd numbers.

Sol: #include<iostream.h> #include<conio.h>

void main( ){

clrscr( );int n,i,t;cout<<”enter number” <<endl;cin>>n;t=1;for(i=1; i<=n; i++){

cout<<t<<’\t’;t=t+2;

}getch( );

}

o/p – enter number121 3 5 7 9 11 13 15 17 19 21 23

Q3. Write a C++ program to check whether a number is prime or not.

Sol: #include<iostream.h>#include<conio.h>void main( ){

7

Page 8: C++

clrscr( );int num,mid,i;cout<<”enter a number”;cin>>num;mid=num/2;for(i=2; i<=mid; i++)

if(num%i==0)break;

if(i>mid)cout<<”prime”;

elsecout<<”not prime”;

getch( );}

o/p – enter a number 121 not prime

Q4. Write a C++ program to find whether the given character is a digit or a letter.

Sol:#include<iostream.h>#include<conio.h>void main( );{

clrscr( );char ch;cout<<”enter a symbol from keyboard : ”;cin>>ch;if((ch>=65 && ch<=90) || (ch>=97 && ch<=122))

8

Page 9: C++

cout<<ch<<”is a alphabet”;else

if(ch>=48 && ch<=57)cout<<ch<<”is a digit”;

elsecout<<ch<<”is neither a digit nor an alphabet”;

getch( );}

o/p – enter a symbol from keyboard : AA is an alphabet

Q5. Write a C++ program to find average of list of numbers entered through keyboard.

Sol:#include<iostream.h> #include<conio.h> void main ( ) {

clrscr( );int n,s,i,num;cout <<”the number of entries to be done ”;cin<<n;s = 0;for( i = 1; i< = n; i++){

cin>>num;s = s+num;

}cout <<”average of numbers entered is”

9

Page 10: C++

<<(float)s/n;getch( );

}

o/p – the numbers of enteries to be done 590941009281average of numbers entered is 91.4

Q6. Write a complete C++ program to do the following :

(1) Read an integer X(2) Form an integer Y by reversing the digits of X and integer S having

the sum of digits of X(3) Output Y and S.

Sol:#include <iostream.h>#include <conio.h>void main ( ){

clrscr ( );int X,Y,S;digit;cout <<”enter any number”;cin>>X;S = 0;Y = 0;

10

Page 11: C++

While (X>0){

digit = X%10;S = S+digit;Y = Y *10+digit;X = X/10;

}cout<<Y<<’/n’; cout<<S;getch ( );

}o/p – enter any number432 234

9

Q7. Write a C++ program to print the truth table for XY+Z.

Sol:#include<iostream.h>#include<conio.h>void main ( ){

clrscr ( );int x,y,z,f;cout <<”x\ty\tz\tf\n\n”;for (x = 0; x< = 1; x++)

for (y = 0; y< = 1; y++)for (z = 0; z< = 1; z++)

{f = x&&y||z;

11

Page 12: C++

cout<<x<<’\t\<<y<<’\t’<<x<<’\t’<<f<<endl;

}getch ( );

}

o/p - x y z f0 0 0 00 0 1 10 1 0 00 1 1 11 0 0 01 0 1 11 1 0 11 1 1 1

Q8. Write a C++ program to find those which are palindromes.

Sol:#include<iostream.h>#include<conio.h>void main ( ){

clscr ( );int num, rev, digit, original;cout<< “enter a number ”;cin>>num;original = num;rev = 0;while (num>0){

12

Page 13: C++

digit = num%10;rev = rev*10+digit;num = num/10;

}cout <<rev;if (rev = = original)

cout<< “the given number is palindrome”;else

cout<< “the given number is not a palindrome’;getch ( );

}

o/p – enter a number13311331the given number is palindrome.

13

Page 14: C++

14

Page 15: C++

Q1. Write a C++ program to search a number in an array using binary search.

Sol:#include<iostream.h>#include<conio.h>void main( ){

clrscr( );int num, beg, end, mid;beg=0;end=6;int A[ ]={2,4,14,400,800,850,9320};cout<< “enter number to be searched: ”;cin>>num;mid=(beg+end)/2;while(beg<=end && num!=A[mid]){

if(num>A[mid])beg=mid+1;

elseend=mid-1;

mid=(beg+mid)/2;}if(beg>mid)

cout<< “not present”;else

cout<< “present”;getch( );

15

Page 16: C++

}o/p – num800 present

Q2. Write a C++ program to sort an array in ascending order using selection sort.

Sol:#include<iostream.h>#include<conio.h>#include<iomanip.h>void main( ){

clrscr( );int A[ ]={5,10,-2,0,12,200,40}int i,j,t;for(i=0; i<(7-1); i++)

for(j=i+1; j<7; j++)if(A[i]>A[j]){

t=A[i];A[i]=A[j];A[j]=t;

}for(i=0; i<7; i++)

cout<<setw(5)<<A[i];cout<<endl;getch( );

}

o/p - -2 0 5 10 12 40 200

16

Page 17: C++

Q3. Write a C++ program to sort an array in ascending order using bubble sort.

Sol: #include<iostream.h>#include<conio.h>void main( ){

clrscr( );int i, j;int A[ ]={4,5,17,-2,0,12};for(i=0; i<(6-1); i++)

for(j=0; j<((6-1)-i); j++)if(A[j]>A[j+1]){

t=A[j];A[j]=A[j+1];A[j+1]=t;

}for(i=0; i<6; i++)

cout<<A[i]<<’\t’;getch( );

}

o/p - -2 0 4 5 12 17

Q4. Write a C++ program to sort an array in ascending order using insert sort.

17

Page 18: C++

Sol:#include<iostream.h>#include<conio.h>void main( ){

clrscr( );int i, x, temp;int A[ ]={12,5,0,7,200,28,30};for(i=1; i<7; i++){

temp=A[i];x=i-1;while(x>=0 && temp<A[x]){

A[x+1]=A[x];x=x-1;

}A[x+1]=temp;

}for(i=0; i<7; i++)

cout<<A[i]<<’\t’;getch( );

}

o/p – 0 5 7 12 28 30 200

Q5.Write a C++ program to arrange text values in A – Z order in an array.

Sol:#include<iostream.h>#include<conio.h>#include<stdio.h>

18

Page 19: C++

#include<string.h>void main( ){

clrscr ( );char names [10] [25]; char t [25];int i,j;for (i = 0; i<3; i++)

gets (names[ i ] );for (i = 0; i< 2; i++)

for (j = i + 1; j<3; j++)if (strcmp (names [ i ], name [ j ] )> 0 )

{strcpy ( t, name [ i ] );strcpy (name [ i ], name [ j ] );strcpy (name [ j ], t );

}for (i = 0, i<10; i++)

puts (names [ i ] );getch ( );

}o/p – apple qwerty lion

applelionqwerty

Q6. Write a C++ program to count the number of words in a string as well as the number of characters other than a space.

Sol:#include<iostream.h>

19

Page 20: C++

#include<conio.h>#include<stdio.h>void main ( ){

clrscr ( );int i = 0, len, now, c, noc ;char st[ 50 ];gets (st);while (st [ i ] ! = ‘\0’ ){

if (st [ i ] = = ‘ ‘)c = c + 1;

i = i + 1;}

len = i;now = c + 1;noc = i – c;cout << “number of words = ”<<now<< endl;cout << “number of characters”<<noc;

getch ( );}

o/p – my name is abcd312

Q7. Write a C++ program to display the lucky number for the one entering his name.

Sol:#include <iostream.h>#include <conio.h>

20

Page 21: C++

#include <stdio.h>#include <string.h>void main ( ){

clrscr ( );char name [25];int S, d, C, num;cout << “enter your name”;gets (name);C = 0; S = 0;While (name [ c ] ! = ‘\0’ ){

S = S + name[ C ];C++;

}while (S>9){

num = S;S = 0;While (num>0){

d = num%10;S = S + d;Num = num/10;

}}cout <<name<< “your lucky number is”<<s<< ‘\n’;getch ( );

}

o/p – Deepali4

21

Page 22: C++

Q8. Write a C++ program to display diagonal elements in a 2-D array.

Sol: #include<iostream.h>#include<conio.h>void main(){

clrscr();int a[4][4];int r,c;for(r=0;r<4;r++)

for(c=0;c<4;c++)cin>>a[r][c];

for(r=0;r<=3;r++){ for(c=0;c<=3;c++)

{cout<<a[r][c]<<'\t';

}cout<<endl;

}cout<<endl;for(r=0;r<4;r++)

cout<<a[r][r]<<'\t';r=0;cout<<endl;for(c=3;c>=0;c--)

cout<<a[r++][c]<<'\t';getch();

}

22

Page 23: C++

o/p – 123456789101112131415161 2 3 45 6 7 89 10 11 1213 14 15 16

1 6 11 164 7 10 13

Q9. Write a C++ program to calculate difference of diagonal elements in a 2-D array.

Sol: #include<iostream.h>#include<conio.h>void main(){

int a[4][4];

23

Page 24: C++

int r,c,sum1=0,sum2=0;for(r=0;r<4;r++)

for(c=0;c<4;c++)cin>>a[r][c];

for(r=0;r<=3;r++){ for(c=0;c<=3;c++)

{ cout<<a[r][c]<<'\t';}cout<<endl;

}cout<<endl;for(r=0;r<4;r++)

sum1=sum1+a[r][r];r=0;cout<<endl;for(c=3;c>=0;c--)

sum2=sum2+a[r++][c];if(sum1>sum2)

cout<<sum1-sum2;else

cout<<sum2-sum1;getch();

}

o/p – 12345678

24

Page 25: C++

9101112131415161 2 3 45 6 7 89 10 11 1213 14 15 16

0 //difference of elements of two diagonals

Q10. Write a C++ program to find the count of prime numbers on both the diagonals of a 2-D array.

Sol: #include<iostream.h>#include<conio.h>int is_prime(int num){

int mid=0;mid=num/2;for(int i=2;i<=mid;i++)

if(num%i==0)break;

if(i>mid)return(1);

25

Page 26: C++

elsereturn(0);

}void main(){

clrscr();int a[4][4];int r,c,f=0;for(r=0;r<4;r++)

for(c=0;c<4;c++)cin>>a[r][c];

for(r=0;r<=3;r++){ for(c=0;c<=3;c++)

{ cout<<a[r][c]<<'\t';}cout<<endl;

}cout<<endl;for(r=0;r<4;r++){

if(is_prime(a[r][r])==0)f++;

}r=0;f--;cout<<endl;for(c=3;c>=0;c--){

if(is_prime(a[r++][c])==0)f++;

}cout<<endl<<endl<<"frequency->"<<f<<endl<<endl;getch();

26

Page 27: C++

}

o/p – 12345678910111213141516

1 2 3 45 6 7 89 10 11 1213 14 15 16

frequency->3

27

Page 28: C++

28

Page 29: C++

Q1. Write a C++ program which includes creation, traversal, insertion, deletion and sorting of an array (using functions).

Sol: #include<iostream.h>#include<conio.h>#include<iomanip.h>void menu( );void creation(int[ ],int n);void traversal(int [ ],int n);void insertion(int [ ],int &,int, int);void deletion(int [ ],int &,int);void sorting(int [ ],int);

void menu( ){

clrscr( );cout<<"OPERATION\n";cout<<"1. CREATION\n";cout<<"2. TRAVERSAL\n";cout<<"3. INSERTION\n";cout<<"4' DELETION\n";cout<<"5. SORTING\n";cout<<"6. EXIT\n";cout<<"enter your option->";

}

void creation(int a[ ],int n){

int i;for(i=0;i<n;i++)

cin>>a[i];29

Page 30: C++

}

void traversal(int a[ ],int n){

int i;for(i=0;i<n;i++)

cout<<setw(5)<<a[i];}

void insertion(int a[ ],int & n,int num,int p){

(n>=10)cout<<"sorry, underflow situation\n";

else{

for(int i=(n-1);i>=p;i--)a[i+1]=a[i];

a[p]=num;n=n+1;

}}

void deletion(int a[ ], int & n, int p){

if(n==0)cout<<"sorry,underflow situation\n";

else{

for(int i=p;i<(n-1);i++)a[i]=a[i+1];

n=n-1;}

30

Page 31: C++

}

void sorting(int a[ ],int n){

int i,x,t;for(i=1;i<n;i++){ t=a[i];

x=i-1;while(x>=0 && t<a[x]){ a[x+1]=a[x];

x=x-1;}a[x+1]=t;

}}//end of function definitionvoid main( ){

int opt, n, a[10], num, p;clrscr( );ABC:menu( );cin>>opt;switch(opt){ case 1:cout<<"enter value for n->"; cin>>n; creation(a,n); break;

case 2:traversal(a,n); getch( ); break;

case 3:cout<<"enter value and position->"; cin>>num>>p; insertion(a,n,num,p);

31

Page 32: C++

break;case 4:cout<<"enter position->";

cin>>p; deletion(a,n,p); break;

case 5:sorting(a,n);}if(opt<6)

goto ABC;}

o/p – enter your option->1 creationenter value for n->61 2 3 4 5

enter your option->2 traversal1 2 3 4 5

enter your option->3 insertionenter value and position->24 2

enter your option->21 2 24 3 4 5

enter your option->4 deletionenter position->5

enter your option->21 2 24 3 4

enter your option->5 sorting

enter your option->2

32

Page 33: C++

1 2 3 4 24

Q2. Write a C++ program that uses following functions:

(a) sqlarge( ) where two arguments are passed by reference and then it sets the larger of the two numbers to its square.

(b) sum( ) where an int argument is passed by value and then it returns the sum of the individual digits of the passed number.

(c) main( ) that exercises above two functions by getting two integers from the user and by printing the sum of the individual digits of the square of the large number.

Sol:#include<iostream.h>#include<conio.h>int sqlarge(int x, int y){

if(x > y)return(x*x)

elsereturn(y*y)

}int sum(int num){

int t=0;while(num>0){

t=t+(num%10);

33

Page 34: C++

num=num/10;}return(t);

}void main( ){

clrscr( );int n1,n2;cout<< “enter any two numbers: ”<<endl;cin>>n1>>n2;cout<<sum(sqlarge(n1,n2));getch( );

}

o/p – enter any two numbers: 25 1413

Q3. Write a program to swap two values using functions.

Sol:#include<iostream.h>#include<conio.h>void main( ){

clrscr( );void swap(int &,int &);int a,b;a=7;b=4;

34

Page 35: C++

cout<< “\nthe original values are : \n”;cout<< “a=”<<a<< “,b=”<<b<<’\n’;swap(a,b);cout<< “\nthe values after swap( ) are :\n”;cout<< “a=”<<a<< “,b=”<<b<<’\n’;getch( );

}void swap(int &x,int &y){

int temp;temp = x;x = y;y = temp;cout<< “\nthe swapped values are : \n”;cout<< “a=”<<x<< “,b=”<<y<<’\n’;

}

o/p – the original values are:a=7,b=4

the swapped values are:a=4,b=7

the values after swap( ) are:a=4,b=7

Q4. Write a C++ program to convert distance in feet or inches using a call by reference method.

Sol:#include<iostream.h>#include<conio.h>#include<process.h>void main( );

35

Page 36: C++

{clrscr( );void convert(float &,char &,char);float dist;char opt, type = ‘F’;cout<<”\nenter dist. in feet : ”;cin>>dist;cout<< “\nyou want dist. in feet or inches? (F/I) : \n”;cin>>opt;switch(opt){

case ‘F’ : convert(dist, type, ‘F’); break;

case ‘I’ : conert(dist, type, ‘I’) break;

default : cout<< “\nyou have entered a wrong choice!!!”; exit(0);

}cout<< “\ndist. = ”<<dist<< “ ”<<type<<’\n’;getch( );

}void convert(float &d, char &t, char ch){

switch(ch){

case ‘F’ : if (t==’I’){

d=d/2;t=’F’;

}break;

case ‘I’ : if(t==’F’)36

Page 37: C++

{d=d*12;t=’I’;

}break;

}return;

}

o/p – enter dist. in feet : 15you want dist. in feets or inches? (F/I) :

Idist. = 180 I

Q5. Write a C++ program that invokes a function calc( ) which intakes two integers and an arithmetic operator and prints the corresponding result.

Sol:#include<iostream.h>#include<conio.h>#include<process.h>void main( ){

clrscr( );void calc(int, int, char)int a,b;char ch;cout<< “\nenter arithmetic operator (+, -, *, /, %) : \n”;cin>>ch;

37

Page 38: C++

calc(a, b, ch);getch( );

}void calc(int x, int y, char ch){

switch(ch){

case ‘+’ :cout<< “\nsum of”<<x<< “ and ”<<y<< “is”<<(x+y);break;

case ‘-’ :cout<< “\ndifference of” <<x<< “and”<<y<< “is”<<(x-y);

break;case ‘*’ :cout<< “\nproduct of”<<x<< “and”<<y<< “is”<<(x*y);

break;case ‘/’ :cout<< “\nquotient of”<<x<< “and”<<y<< “is”<<(x/y);

break;case ‘%’ : cout<< “\nremainder of”<<x<< “and”<<y<<

“is”<<(x%y);break;

}return;

}

o/p – enter two integers : 12 5enter arithmetic operator (+, -, *, /, %) : %remainder of 12 and 5 is 2.

38

Page 39: C++

39

Page 40: C++

Q1. Write a C++ program that declares a structure time having two int variables hrs and mins and declares a variable of data type time. Also write statements to change time in AM or PM according to the trivial system of 24 hours.

Sol:#include<iostream.h>#include<conio.h>struct time{

int hrs;char mins;

}void main( ){

clrscr( );time time1;cin>>time1.hrs>>time1.mins;if(time1.hrs<12)

cout<<time1.hrs<<":"<<time1.mins<<"A.M.";else

if(time1.hrs>12)cout<<time1.hrs<<":"<<time1.mins<<"P.M.";

elseif(time1.hrs==12 && time1.mins==0)

cout<<time1.hrs<<":"<<time1.mins<<"noon";else

if(time1.hrs==12 && time1.mins>0)

cout<<time1.hrs<<":"<<time1.mins<<"P.M.";getch();

}40

Page 41: C++

o/p – 12 3412:34 P.M.

Q2.Write a C++ program to accept and print a student’s result using a structure having array inside it.Sol:#include<iostream.h>

#include<conio.h>#include<stdio.h>struct student{

int rollno;char name[21];float marks[5];char grade;

};student learner;void main( );{

cout<< “\n”<< “enter roll no. :”;cin>>learner.roll.no;cout<< “\n”<< “enter name : ”;gets(learner.name);cout<< “\n”<< “enter marks in 5 suvjects : ”<< “\n”;for(int i=0; i<5;i++){

cout<<’\n’<< “subject”<<i+1<< “:”;cin>>learner.marks[i];

}float avg,total;total=(learner.marks[0]+learner.marks[1]+learner.marks[2]+learner.marks[3]+learner.marks[4]);avg=total/5;

41

Page 42: C++

if(avg<50)learner.grade = ‘F’;

else if(avg<60)

learner.grade = ‘C’;else

if (avg<80)learner.grade = ‘B’;

elselearner.grade = ‘A’;

cout<< “\n”<<”student result :”<< “\n”;cout<< “rollno :”<<learner.roll<< “\t”;cout<<”name :”;cout.write(learner.name,21);cout<< “\n”<< “total marks :”<<total;cout<< “\t”<< “grade :”<<learner.grade<<endl;getch( );

}o/p –

enter roll no : 21enter name : Navyaenter marks in five subjects :subject 1: 98subject 2: 89subject 3: 99subject 4: 82subject 5: 79

student resultroll no : 21 name : Navyatotal marks : 457 grade : A

42

Page 43: C++

Teacher’s Signature:

Mr. Narendra Singh

43