Archive
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
C++ Program to check Command Line Arguments – Q4
Q4. Program to check Command line arguments:
Accept an n digit number as command line argument & perform various tasks specified below:
i) Find the sum of digits of the number
ii) Find the reverse of a number
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <math.h>
void Sum_Num(char *);
void Rev_Num(char *);
void main(int argv, char *argc[]){
char patt[10];
clrscr();
if(argv < 1)
cout<<"\n Insuffecient Arguments. \n";
else{
strcpy(patt, argc[1]);
cout<<”\n Argument is: “<<patt;
Sum_Num(patt);
Rev_Num(patt);
}
getch();
}
void Sum_Num(char *pt){
int sum = 0, i = 0;
while(*(pt+i) != '\0'){
sum += int(*(pt+i)) - 48;
i++;
}
cout<<"\n Sum: "<<sum;
}
void Rev_Num(char *pt){
long r_num = 0;
int i = 0;
while(*(pt+i) != '\0'){
r_num += (int(*(pt+i)) - 48) * pow(10, i);
i++;
}
cout<<"\n Reverse: "<<r_num;
}
Output:
Argument is: 12345
Sum: 15
Reverse: 54321
C++ Program for Array Sorting, Searching and Deletion of element by using Pointers – Q3
Q3. Program for Array Sorting, Searching and Deletion of element:
Perform following task with an array of N numbers:
i) Sort the array in ascending order
ii) Search the given number in the array & notify its position in the list if present.
iii) Delete a given no from the array from a given position
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
void Sort_Array (int *, const int);
void Search_Elm (int *, const int);
void Delete_Elm (int *, int &);
void main(){
int *arr = new(int);
int n, ch;
clrscr();
cout<<"\n How many Number of Elements in an Array?: ";
cin>>n;
cout<<"\n Enter "<<n<<" Elements: ";
for(int i=0; i<n; i++)
cin>>*(arr+i);
while(1){
clrscr();
cout<<"\n Array is: ";
for(i=0; i<n; i++)
cout<<*(arr+i)<<" ";
cout<<"\n\n Main Menu. \n";
cout<<"\n 1 -> Sort Elements.";
cout<<"\n 2 -> Search a Given Element.";
cout<<"\n 3 -> Delete an Element.";
cout<<"\n 4 -> Exit.";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch){
case 1:
Sort_Array(arr, n);
break;
case 2:
Search_Elm(arr, n);
break;
case 3:
Delete_Elm(arr, n);
break;
case 4:
delete(arr);
exit(1);
default:
cout<<"\n Enter Correct Choice.";
} // end of switch.
getch();
} // end of while.
} // end of main.
void Sort_Array(int *arr, const int n){
int temp;
for(int i=0; i<n; i++){
for(int j=i; j<n; j++){
if( *(arr+i) > *(arr+j) ){
temp = *(arr+i);
*(arr+i) = *(arr+j);
*(arr+j) = temp;
}
}
}
cout<<"\n Array Sorted.";
}
void Search_Elm(int *arr, const int n){
int elm, found=0;
cout<<"\n Enter an Element to Search: ";
cin>>elm;
for(int i=0; i<n; i++){
if( elm == *(arr+i) ){
cout<<"\n Element "<<*(arr+i)<<" in position "
<<i+1;
found = 1;
}
}
if(!found)
cout<<"\n Element not Found.";
}
void Delete_Elm(int *arr, int &n){
int elm, found = 0;
cout<<"\n Enter an Element to Delete: ";
cin>>elm;
for(int i=0; i<n; i++){
if( elm == *(arr+i) ){
found = 1;
break;
}
if(i >= n)
found = 0;
}
for(int j=i; j<n; j++)
*(arr+j) = *(arr+j+1);
if(!found)
cout<<"\n Element not Found.";
else{
cout<<"\n Element Deleted.";
n--;
}
}
Output:
How many Number of Elements in an Array?: 5
Enter 5 Elements: 21
23
645
32
19
Array is: 19 21 23 32 645
Main Menu.
1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 1
Array Sorted.
Array is: 19 21 23 32 645
Main Menu.
1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 2
Enter an Element to Search: 33
Element not Found.
Array is: 19 21 23 32 645
Main Menu.
1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 2
Enter an Element to Search: 32
Element 32 in position 4
C++ Program to find Sum of Series (When n is Odd/Even) – Q2
Q2. Program to find Sum of Series:
i) x-x^3/3!-x^5/5!+x^7/7!-….. x^n/n! (When n is odd)
ii) x^2/2!-x^4/4!+x^6/6!-….. x^n/n! (When n is even)
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <math.h>
#include <conio.h>
long Fact(long);
float Odd_Series(int, int);
float Even_Series(int , int);
int main(){
int x, n;
float sum;
clrscr();
cout<<"\n Enter the Value of x: ";
cin>>x;
cout<<"\n Enter the Value of n: ";
cin>>n;
sum = Odd_Series(x, n);
cout<<"\n Sum of Odd Series: "<<sum;
sum = Even_Series(x, n);
cout<<"\n Sum of Even Series: "<<sum;
getch();
return 0;
}
long Fact(long f){
if(f<=0)
return 1;
else
return (f * Fact(f-1));
}
float Odd_Series(int x, int n){
int i, f;
float sum = 0;
for(i=1, f=0; i<=n; i=i+2, f++)
sum += pow(-1, f) * pow(x, i) / Fact(i);
return sum;
}
float Even_Series(int x, int n){
int i, f;
float sum = 0;
for(i=2, f=0; i<=n; i=i+2, f++)
sum += pow(-1, f) * pow(x, i) / Fact(i);
return sum;
}
Output:
Enter the Value of x: 4
Enter the Value of n: 5
Sum of Odd Series: 1.866667
Sum of Even Series: -2.666667




