### Archive

Posts Tagged ‘SWITCH CASE’

## C++ Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE – Q12

Q12. Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE:

Calculate the toll tax for cars passing by a toll bridge @ Rs 5 per car (use the concept static data members). Calculate and Print the following details:
– Total no of cars passed with paying the tax
– Total amount of tax paid
– Total no of cars passed without paying tax.

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class TollBridge{
private:
static int car_yes, car_no;
static int amt;
public:
static void GetCar();
static void PutCar();
};

int TollBridge::car_yes = 0;
int TollBridge::car_no = 0;
int TollBridge::amt = 0;

void TollBridge :: GetCar(){
char ch;
cout<<"\n Total Cars Passed: "<<car_yes+car_no;
cout<<"\n This Car:- ";
cout<<"\n Paid Tax (y/n) ?: ";
cin>>ch;
if(ch == 'y'){
car_yes++;
amt += 5;
}
else
car_no++;
}

void TollBridge :: PutCar(){
cout<<"\n Total Car Passed: "<<car_yes+car_no<<endl;
cout<<"\n\t Car Passed by giving Tax: "<<car_yes;
cout<<"\n\t\t Amount Recieved: "<<amt<<endl;
cout<<"\n Car Passed by not giving Tax: "<<car_no;
}

void main(){
int ch;
char choice;
while(1){
clrscr();
cout<<"\n TOLL TAX PLAZA";
cout<<"\n ~~~~~~~~~~~~~~";
cout<<"\n 1 -> Entry of CARS.";
cout<<"\n 2 -> Report of CARS.";
cout<<"\n 3 -> Exit.";
cin>>ch;
switch(ch){
case 1:
while(1){
TollBridge::GetCar();
cout<<"\n Do you want to continue
(y/n) ?: ";
cin>>choice;
if(choice =='n') break;
}
break;
case 2:
TollBridge::PutCar();
getch();
break;
default:
exit(1);
} // end of switch.
} // end of while.
} // end of main.
```

Output:

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.

Total Cars Passed: 0
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 1
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 2
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 3
This Car:-
Paid Tax (y/n) ?: n

Do you want to continue (y/n) ?: y

Total Cars Passed: 4
This Car:-
Paid Tax (y/n) ?: n

Do you want to continue (y/n) ?: y

Do you want to continue (y/n) ?: n

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.

Total Car Passed: 5
Car Passed by giving Tax: 3
Amount Recieved: 15
Car Passed by not giving Tax: 2

Categories: Cpp

## C++ Program for Matrix manipulation (menu based) by using Multi Dimensional Array and SWITCH CASE – Q6

Q6. Program Matrix manipulation (menu based):

for following Matrix manipulations:
ii) Multiply two matrices
iii) Find the difference
iv) Find the sum of Rows and Sum of Cols.

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void Init_Matrix (int [][3], int [][3]);
void Disp_Matrix (int [][3], int [][3]);
void Add_Matrix  (int [][3], int [][3]);
void Sub_Matrix  (int [][3], int [][3]);
void Mult_Matrix (int [][3], int [][3]);
void Sum_R_C     (int [][3], int [][3]);

void main(){
int mat1[3][3], mat2[3][3];
int ch;
clrscr();
cout<<"\n Matrix Operation of Order (3*3):- \n";
Init_Matrix(mat1, mat2);
while(1){
clrscr();
Disp_Matrix(mat1, mat2);
cout<<"\n Matrix Operation of Order (3*3):- \n";
cout<<"\n 1 -> Change Elements of Matrices.";
cout<<"\n 3 -> Subtract Matrices.";
cout<<"\n 4 -> Multiply Matrices.";
cout<<"\n 5 -> Sum of ROWs n COLs.";
cout<<"\n 6 -> Exit.";
cin>>ch;
switch(ch){
case 1:
Init_Matrix(mat1, mat2);
break;
case 2:
break;
case 3:
Sub_Matrix(mat1, mat2);
break;
case 4:
Mult_Matrix(mat1, mat2);
break;
case 5:
Sum_R_C(mat1, mat2);
break;
case 6:
exit(1);
default:
cout<<"\n Enter Correct Choice.";
} // end of switch.
getch();
} // end of while.
} // end of main.

void Init_Matrix(int mat1[][3], int mat2[][3]){
cout<<"\n Enter the Elments of First Matrix: ";
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
cin>>mat1[i][j];
cout<<"\n Enter the Elments of First Matrix: ";
for(i=0; i<3; i++)
for(j=0; j<3; j++)
cin>>mat2[i][j];
}

void Disp_Matrix(int mat1[][3], int mat2[][3]){
cout<<"\n First matrix is:- \n";
for(int i=0; i<3; i++){
for(int j=0; j<3; j++)
cout<<" "<<mat1[i][j];
cout<<endl;
}

cout<<"\n Second matrix is:- \n";
for(i=0; i<3; i++){
for(int j=0; j<3; j++)
cout<<" "<<mat2[i][j];
cout<<endl;
}
}

int tot[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
tot[i][j] = mat1[i][j] + mat2[i][j];

cout<<"\n Addition of 2 Matrices:- \n";
for(i=0; i<3; i++){
for(int j=0; j<3; j++)
cout<<" "<<tot[i][j];
cout<<endl;
}
}

void Sub_Matrix(int mat1[][3], int mat2[][3]){
int tot[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
tot[i][j] = mat1[i][j] - mat2[i][j];

cout<<"\n Subtraction of 2 Matrices:- \n";
for(i=0; i<3; i++){
for(int j=0; j<3; j++)
cout<<" "<<tot[i][j];
cout<<endl;
}
}

void Mult_Matrix(int mat1[][3], int mat2[][3]){
int tot[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++){
tot[i][j]=0;
for(int k=0; k<3; k++)
tot[i][j] = tot[i][j] +
(mat1[i][k]*mat2[k][j]);
}
cout<<"\n Multiplication of 2 Matrices:- \n";
for(i=0; i<3; i++){
for(int j=0; j<3; j++)
cout<<" "<<tot[i][j];
cout<<endl;
}
}

void Sum_R_C(int mat1[][3], int mat2[][3]){
int row=0, col=0;
cout<<"\n Sum of ROWs and COLs :- \n";
cout<<"\n First Matrix :- \n";
// Sum of row of mat1.
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
cout<<"  "<<mat1[i][j];
row = row + mat1[i][j];
}
cout<<" -  "<<row;
row = 0;
cout<<endl;
}
cout<<"\n  |  |  | \n";
// Sum of col of mat1.
for(i=0; i<3; i++){
for(int j=0; j<3; j++)
col = col + mat1[j][i];
cout<<"  "<<col;
col = 0;
}

cout<<"\n\n Second Matrix :- \n";
// Sum of row of mat2.
for(i=0; i<3; i++){
for(int j=0; j<3; j++){
cout<<"  "<<mat2[i][j];
row = row + mat2[i][j];
}
cout<<" -  "<<row;
cout<<endl;
row = 0;
}
cout<<"\n  |  |  | \n";
// Sum of col of mat2.
for(i=0; i<3; i++){
for(int j=0; j<3; j++)
col = col + mat2[j][i];
cout<<"  "<<col;
col = 0;
}
}
```

Output:

Matrix Operation of Order (3*3):-

Enter the Elments of First Matrix: 2 1 3 5 4 9 8 6 7

Enter the Elments of First Matrix: 3 1 4 2 5 6 9 8 0

First matrix is:-
2 1 3
5 4 9
8 6 7

Second matrix is:-
3 1 4
2 5 6
9 8 0

Matrix Operation of Order (3*3):-
1 -> Change Elements of Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.

5 2 7
7 9 15
17 14 7

First matrix is:-
2 1 3
5 4 9
8 6 7

Second matrix is:-
3 1 4
2 5 6
9 8 0

Matrix Operation of Order (3*3):-
1 -> Change Elements of Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.
Subtraction of 2 Matrices:-
-1 0 -1
3 -1 3
-1 -2 7

First matrix is:-
2 1 3
5 4 9
8 6 7

Second matrix is:-
3 1 4
2 5 6
9 8 0

Matrix Operation of Order (3*3):-
1 -> Change Elements of Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.

Multiplication of 2 Matrices:-
35 31 14
104 97 44
99 94 68

1 -> Change Elements of Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.

Sum of ROWs and COLs :-

First Matrix :-
2 1 3 – 6
5 4 9 – 18
8 6 7 – 21
| | |
15 11 19

Second Matrix :-
3 1 4 – 8
2 5 6 – 13
9 8 0 – 17
| | |
14 14 10

Categories: Cpp

## C++ Program for String Manipulation (menu based) by using SWITCH CASE – Q5

Q5. Program for String Manipulation (menu based):

Perform following string manipulation –
i) To find the length of the given string.
ii) To concatenate two strings.
iii) To reverse a string.
iv) To search a sub string in the given string.

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

int  Str_Length  (char *);
void Str_Reverse (char *, char *);
void Str_Concat  (char *, const char *);
void Str_Copy    (char *, char *);
int  Str_SubStr  (const char *, char *);
int  Str_Comp    (char *, char *);

int main(){
int ch, num;
char *str1, *str2;
str1 = new(char);
str2 = new(char);
while(1){
clrscr();
cout<<"\n STRING MANIPULATION PROGRAMME";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n";
cout<<"\n 1 -> String Length.";
cout<<"\n 2 -> String Reverse.";
cout<<"\n 3 -> String Concatenation.";
cout<<"\n 4 -> String Copy.";
cout<<"\n 5 -> Sub String.";
cout<<"\n 6 -> String Comparision.";
cout<<"\n 7 -> Exit.";
cin>>ch;
switch(ch){
case 1:
cout<<"\n Enter a String: ";
cin>>str1;
cout<<"\n Length of String "<<str1<<" is: "
<<Str_Length(str1);
break;
case 2:
cout<<"\n Enter a String: ";
cin>>str1;
cout<<"\n Reverse String "<<str1<<" is: ";
Str_Reverse(str1, str2);
cout<<str2;
break;

case 3:
cout<<"\n Enter 1st String: ";
cin>>str1;
cout<<"\n Enter 2nd String: ";
cin>>str2;
cout<<"\n Concate of Strings: "<<str1<<" &
"<<str2<<" is: ";
Str_Concat(str1, str2);
cout<<str1;
break;
case 4:
cout<<"\n Enter a String: ";
cin>>str1;
Str_Copy(str1, str2);
cout<<"\n String str2 is: "<<str2;
break;
case 5:
cout<<"\n Enter a String: ";
cin>>str1;
cout<<"\n Now enter an Another String: ";
cin>>str2;
num = Str_SubStr(str1, str2);
if(num == 1)
cout<<"\n The Sub String: "<<str2<<"
is Present in the String: "
<<str1;
else
cout<<"\n The Sub String: "<<str2<<"
is NOT Present in String: "
<<str1;
break;
case 6:
cout<<"\n Enter 1st String: ";
cin>>str1;
cout<<"\n Enter 2nd String: ";
cin>>str2;
num = Str_Comp(str1, str2);
if(num == 0)
cout<<"\n String str1: "<<str1
<<" & String str2: "<<str2<<"
are Equal";
else if(num < 0)
cout<<"\n String str1: "<<str1
<<" is Smaller than String str2: "
<<str2;
else
cout<<"\n String str1: "<<str1
<<" is Greater than String str2: "
<<str2;
break;
default:
delete(str1);
delete(str2);
exit(1);
} // end of switch.
getch();
} // end of while.
} // end of main.

int Str_Length(char *st){
int len = 0;
while(*(st+len) != '\0')
len++;
return len;
}

void Str_Reverse(char *st, char *temp){
int len, i=0;
len = Str_Length(st);
while(len > 0){
len--;
*(temp+i) = *(st+len);
i++;
}
*(temp+i) = '\0';
}

void Str_Concat(char *st1, const char *st2){
int i = 0, j = 0;
while(*(st1+i) != '\0')
i++;
while(*(st2+j) != '\0'){
*(st1+i) = *(st2+j);
i++;
j++;
}
*(st1+i) = '\0';
}

void Str_Copy(char *st1, char *st2){
int i=0, len;
len = Str_Length(st1);
for(i=0; i<len; i++)
*(st2+i) = *(st1+i);
*(st2+i)='\0';
}

int Str_SubStr(const char *st1, char *st2){
int i=0, j=0;
int flag=0;
while( (*(st1+i) != '\0') && (*(st2+j) != '\0') ){
if( *(st1+i) == *(st2+j) )
j++, flag=1;
else
flag=0, j=0;
i++;
}
if (flag == 1)
return 1;
else
return 0;
}

int Str_Comp(char *st1, char *st2){
int n1, n2, x;
n1 = Str_Length(st1);
n2 = Str_Length(st2);
if(n1 == n2)
x = 0;
else if(n1 < n2)
x = -1;
else
x = 1;
return x;
}
```

Output:

STRING MANIPULATION PROGRAMME

1 -> String Length.
2 -> String Reverse.
3 -> String Concatenation.
4 -> String Copy.
5 -> Sub String.
6 -> String Comparision.
7 -> Exit.

Enter a String: ManojPandey

Length of String ManojPandey is: 11

STRING MANIPULATION PROGRAMME

1 -> String Length.
2 -> String Reverse.
3 -> String Concatenation.
4 -> String Copy.
5 -> Sub String.
6 -> String Comparision.
7 -> Exit.

Enter a String: Pandey

Reverse of String Pandey is: yednaP

STRING MANIPULATION PROGRAMME

1 -> String Length.
2 -> String Reverse.
3 -> String Concatenation.
4 -> String Copy.
5 -> Sub String.
6 -> String Comparision.
7 -> Exit.