C++ Program to maintain a Cricket Team by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q9
Q9. Program to maintain a Cricket Team:
Make a Class Cricket_Team with following attributes:
– Team Name
– Player Name
– Batting Average
Read the details various players from different team in random.
Now print the team wise list of players in the sorted order of their batting averages.
Use functions:
– To read the player detail
– To arrange the players team wise
– To sort the batting average
– To Print the details
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>
class CCricket{
private:
char country[20], player[20];
int avg;
public:
void getTeams();
void compTeams(CCricket *, const int);
void dispTeams(const CCricket *, const int);
};
void CCricket :: getTeams(){
cout<<"\n Enter the Name of a Country: ";
cin>>country;
cout<<"\n Enter a Player Name: ";
cin>>player;
cout<<"\n Enter the Batting Average: ";
cin>>avg;
}
void CCricket :: compTeams(CCricket *Ock, const int t_pls){
int i, j;
CCricket Otemp;
// Sorting By Players Name.
for(i=0; i<=t_pls; i++){
for(j=i+1; j<=t_pls; j++){
if(Ock[i].avg < Ock[j].avg){
Otemp = Ock[i];
Ock[i] = Ock[j];
Ock[j] = Otemp;
}
}
}
// Sorting By Country Name.
for(i=0; i<=t_pls; i++){
for(j=i+1; j<=t_pls; j++){
if(strcmp(Ock[i].country, Ock[j].country) > 0){
Otemp = Ock[i];
Ock[i] = Ock[j];
Ock[j] = Otemp;
}
}
}
}
void CCricket :: dispTeams(const CCricket *Ock, const int t_pls){
int i, j;
char t_c_name[10];
// Display Players.
cout<<"\n\n Players Sorted According to their Country and
Average:- \n";
cout<<"\n COUNTRY \t TEAM \t AVERAGE"<<endl;
for(i=1; i<=t_pls; i++){
if(strcmp(t_c_name, Ock[i].country) != 0){
cout<<"\n "<<Ock[i].country;
strcpy(t_c_name, Ock[i].country);
}
cout<<"\n\t\t"<<Ock[i].player<<" - "
<<setw(5)<<Ock[i].avg<<endl;
}
}
void main(){
int i=0;
char ch;
CCricket Ock[30], Otemp;
while(1){
clrscr();
Ock[i].getTeams();
i++;
cout<<"\n Do you want to Enter next Entry (y/n) ? : ";
cin>>ch;
if(ch == 'n')
break;
} // End of while Loop.
cout<<"\n\n Total Players Entered: "<<i<<endl;
// Sort Teams.
Otemp.compTeams(Ock, i);
// Display Teams.
Otemp.dispTeams(Ock, i);
getch();
}
Output:
Enter the Name of a Country: India
Enter a Player Name: Sachin
Enter the Batting Average: 70
Do you want to Enter next Entry (y/n) ? : y
Enter the Name of a Country: Australia
Enter a Player Name: Ponting
Enter the Batting Average: 50
Do you want to Enter next Entry (y/n) ? : y
Enter the Name of a Country: Bang
Enter a Player Name: Zahid
Enter the Batting Average: 30
Do you want to Enter next Entry (y/n) ? : y
Enter the Name of a Country: India
Enter a Player Name: Rahul
Enter the Batting Average: 45
Do you want to Enter next Entry (y/n) ? : y
Enter the Name of a Country: Australia
Enter a Player Name: Gilcrist
Enter the Batting Average: 75
Do you want to Enter next Entry (y/n) ? : y
Enter the Name of a Country: India
Enter a Player Name: Harbhjan
Enter the Batting Average: 50
Do you want to Enter next Entry (y/n) ? : n
Total Players Entered: 6
Players Sorted According to their Country and Average:-
COUNTRY TEAM
Australia
Gilcrist – 75
Ponting – 50
Bang
Zahid – 30
India
Harbhjan – 50
Rahul – 45
Sachin – 70
C++ Program to implement Function Overloading – Q8
Q8. Program to implement Function Overloading:
Implement Function Overloading for following requirements
– Getting Sum of two number
– Concatenating two strings
– Adding two matrices
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
const int MAX = 3;
int Sum (int, int);
void Sum (char *, char *);
void Sum (int [][MAX], int [][MAX]);
void main(){
int a, b;
char x[10], y[10];
int mat1[MAX][MAX], mat2[MAX][MAX], tot[MAX][MAX];
clrscr();
cout<<"\n Integers:- "<<endl;
cout<<"\n Enter value of a: ";
cin>>a;
cout<<"\n Enter value of b: ";
cin>>b;
cout<<"\n Sum of Integers "<<a<<" & "<<b<<" is: "<<Sum(a, b)<<endl;
cout<<"\n Strings:- "<<endl;
cout<<"\n Enter value of x: ";
cin>>x;
cout<<"\n Enter value of y: ";
cin>>y;
cout<<"\n Sum of Strings "<<x<<" & "<<y<<" is: ";
Sum(x, y);
cout<<"\n Integer Matrices:- "<<endl;
cout<<"\n Enter values in Matrix mat1: ";
for(int i=0; i<MAX; i++)
for(int j=0; j<MAX; j++)
cin>>mat1[i][j];
cout<<"\n Enter value in Matrix mat2: ";
for(i=0; i<MAX; i++)
for(j=0; j<MAX; j++)
cin>>mat2[i][j];
cout<<"\n Sum of Matrices is:-"<<endl;
Sum(mat1, mat2);
getch();
}
int Sum(int a, int b){
return (a+b);
}
void Sum(char *a, char *b){
int i = 0, j = 0;
char c[20];
while(*(a+i) != '\0'){
*(c+i) = *(a+i);
i++;
}
while(*(b+j) != '\0'){
*(c+i) = *(b+j);
i++;
j++;
}
*(c+i) = '\0';
cout<<c;
}
void Sum(int m1[MAX][MAX], int m2[MAX][MAX]){
int tot[MAX][MAX];
for(int i=0; i<MAX; i++){
for(int j=0; j<MAX; j++){
tot[i][j] = m1[i][j] + m2[i][j];
cout<<" "<<tot[i][j];
}
cout<<endl;
}
}
Output:
Integers:-
Enter value of a: 45
Enter value of b: 12
Sum of Integers 45 & 12 is: 57
Strings:-
Enter value of x: Manoj
Enter value of y: Pandey
Sum of Strings Manoj & Pandey is: ManojPandey
Integer Matrices:-
Enter values in Matrix mat1: 1 2 3 4 5 6 7 8 9
Enter values in Matrix mat2: 9 8 7 6 5 4 3 2 1
Sum of Matrices is :-
10 10 10
10 10 10
10 10 10
C++ Program to maintain Student Result by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q7
Q7. Program Matrix manipulation (menu based):
Make a structure Student with following fields:
– Name
– Roll No.
– Result (Structure)
– – Total Marks Obtained
– – Max_Marks
– – Percentage
Read the list of students (At least 3) in a class.
Now compare the results of all the students and Rank top 3 students in the whole class.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
class CStud{
private:
char name[20], course[10];
char grade;
public:
int marks;
void getinfo();
void compute();
void display();
};
void CStud :: getinfo(){
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Course: ";
cin>>course;
cout<<"\n Enter Marks Obtained (out of 500): ";
cin>> marks;
}
void CStud :: compute(){
int per;
per = marks/5;
if(per >= 80)
grade = 'A';
else if( (per < 80) && (per >= 60) )
grade = 'B';
else if( (per < 60) && (per >= 40) )
grade = 'C';
else
grade = 'D';
}
void CStud :: display(){
cout<<"\n Name: "<<name;
cout<<"\t Course: "<<course;
cout<<"\t Marks: "<<marks;
cout<<"\t Grade: "<<grade;
}
void main(){
int n = 0;
char ch;
CStud Ostu[10]; // Creating Object.
clrscr();
while(ch != 'n'){ // Getting Information.
gotoxy(25, 1);
cout<<"-: STUDENT INFORMATION :- \n";
cout<<"\n Student No.: "<<n+1<<endl;
Ostu[n].getinfo();
Ostu[n].compute(); // Calculate Grade.
cout<<"\n Next Student (y/n): ";
cin>>ch;
n++;
clrscr();
}
// Sorting.
CStud Otemp;
for(int i=0; i<n; i++){
for(int j=i; j<n; j++){
if(Ostu[i].marks < Ostu[j].marks){
Otemp = Ostu[i];
Ostu[i] = Ostu[j];
Ostu[j] = Otemp;
}
}
}
clrscr();
gotoxy(25, 1);
cout<<"-: STUDENT'S REPORT :- "; // Display Results.
for(i=0; i<n; i++){
cout<<"\n\n Student No.: "<<i+1;
Ostu[i].display();
}
cout<<"\n\n Press Any Key to Exit ...";
getch();
}
Output:
-: STUDENT INFORMATION :-
Student No.: 1
Enter Name: Manoj
Enter Course: MCA
Enter Marks Obtained (out of 500): 350
Next Student (y/n): y
-: STUDENT INFORMATION :-
Student No.: 2
Enter Name: Bhanu
Enter Course: MBA
Enter Marks Obtained (out of 500): 400
Next Student (y/n): n
-: STUDENT’S REPORT :-
Student No.: 1
Name: Bhanu Course: MBA Marks: 400 Grade: A
Student No.: 2
Name: Manoj Course: MCA Marks: 350 Grade: B
Press Any Key to Exit …
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:
i) Add two matrices
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 2 -> Add Matrices.";
cout<<"\n 3 -> Subtract Matrices.";
cout<<"\n 4 -> Multiply Matrices.";
cout<<"\n 5 -> Sum of ROWs n COLs.";
cout<<"\n 6 -> Exit.";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch){
case 1:
Init_Matrix(mat1, mat2);
break;
case 2:
Add_Matrix(mat1, mat2);
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;
}
}
void Add_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 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.
2 -> Add Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.
Enter your choice: 2
Addition of 2 Matrices:-
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.
2 -> Add Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.
Enter your choice: 3
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.
2 -> Add Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.
Enter your choice: 4
Multiplication of 2 Matrices:-
35 31 14
104 97 44
99 94 68
1 -> Change Elements of Matrices.
2 -> Add Matrices.
3 -> Subtract Matrices.
4 -> Multiply Matrices.
5 -> Sum of ROWs n COLs.
6 -> Exit.
Enter your choice: 5
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
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.";
cout<<"\n\n Enter your choice: ";
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 your choice: 1
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 your choice: 2
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.
Enter your choice: 3
Enter 1st String: Manoj
Enter 2nd String: Pandey
Concate of Strings: Manoj & Pandey is: ManojPandey




