BCSL-032 ASSIGNMENT SOLUTION (2018-19)

In this ASSIGNMENT all the below PROGRAMMINGS are written and run in Turbo C++ 3.0

We have removed clrscr( ) and getch( ) functions from each programming because of different version of C++ compiler.

Question 1 (a) :

Write a C++ program for followings :

• Find sum of 1+3+5+…. up to n terms
• Generate Fibonacci series

 // Find sum of 1+3+5+…. up to n terms #include class series { public: void calculation(int n) { int i, a=1, sum=0; for(i=1;i<=n;i++) { sum=sum+a; a=a+2; } cout << "\n Answer = " << sum; } }; void main() { int num; cout << "\n Enter the value of n : "; cin >> num; series ob1; ob1.calculation(num); } Output Enter the value of n : 5 Answer = 25

 // Generate Fibonacci series #include class Fibonacci { private: int a, b, c, n; public: void Input() { cout << "\n Enter the first number of the series : "; cin >> a; cout << "\n Enter the second number of the series : "; cin >> b; cout << "\n Enter the number of terms you want to generate : "; cin >> n; } void SeriesGenerate() { int i; for(i=1;i<=n;i++) { c=a+b; cout << "\t" << c; a=b; b=c; } } }; void main() { Fibonacci ob1; ob1.Input(); ob1.SeriesGenerate(); } Output Enter the first number of the series : 2 Enter the second number of the series : 5 Enter the number of terms you want to generate : 5 712193150

Question 1 (b) :

Write a C++ program to create class named Account. Derive Current_Account and Saving_Account classes from it. Define method display-balance() in both the classes. Make necessary assumptions wherever required.

#include<iostream.h>
#include<conio.h>
class Account
{
protected:
long account_no;
double balance;
Account()
{
balance=0.0;
}
};

class Current_Account : private Account
{
public:
Current_Account(long accno, double b)
{
account_no=accno;
balance=b;
}
void display_balance()
{
cout << "\n Account Type \t Account No. \t Balance";
cout << "\n ------------ \t ----------- \t -------";
cout << "\n Current \t " << account_no << " \t " << balance;
}
};

class Saving_Account : private Account
{
public:
Saving_Account(long accno, double b)
{
account_no=accno;
balance=b;
}
void display_balance()
{
cout << "\n Account Type \t Account No. \t Balance";
cout << "\n ------------ \t ----------- \t -------";
cout << "\n Saving \t " << account_no << " \t " << balance;
}
};

void main()
{
clrscr();
Current_Account ob1(1055080, 1540372.25);
Saving_Account ob2(2468105, 50815.75);
ob1.display_balance();
cout << "\n\n\n";
ob2.display_balance();
getch();
}

Output

 Account Type Account No. Balance ------------------ ---------------- ------------ Current 1055080 1540372.25 Account Type Account No. Balance ------------------ ---------------- ------------ Saving 2468105 50815.75

Question 2 (a) :

Write a C++ program to demonstrate exception handling by using example of multiplication of two matrices.

 #include void main() { int i, j, k, a, b, c; // Input Matrix A cout << "\n Enter the element of matrix A"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout << "\n Enter the value of A[" << i << "][" << j <<"] : "; cin >> a[i][j]; } } // Input Matrix B cout << "\n Enter the element of matrix B"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout << "\n Enter the value of B[" << i << "][" << j <<"] : "; cin >> b[i][j]; // Matrix C initialization c[i][j] = 0; } } // try block try { // Multiply matrix A and B for(i=0;i<3;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { c[i][j] = c[i][j] + (a[i][k] * b[k][j]); } } } } // catch block catch(char* message) { cerr << message; } // Print the matrix C for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout <<"\t" << c[i][j]; } cout << endl; } } Output Enter the Element of First Matrix Enter the value of A : 1 Enter the value of A : 4 Enter the value of A : 2 Enter the value of A : 3 Enter the value of A : 2 Enter the value of A : 5 Enter the value of A : 4 Enter the value of A : 6 Enter the value of A : 3 Enter the Element of Second Matrix Enter the value of B : 2 Enter the value of B : 6 Enter the value of B : 3 Enter the value of B : 8 Enter the value of B : 1 Enter the value of B : 5 Enter the value of B : 6 Enter the value of B : 2 Enter the value of B : 4 461431 523039 743654

Question 2 (b) :

 #include class MatrixAddition { private: int matrix, value, i, j; public: MatrixAddition(int m, int n) { for(i=0;i> z; // Input Matrix A cout << "\n Enter the element of matrix A"; for(x=0;x> a[x][y]; } } // Input Matrix B cout << "\n Enter the element of matrix B"; for(x=0;x> b[x][y]; } } MatrixAddition ob1(a,z), ob2(b,z); ob1 + ob2; } Output Enter the number of Row / Column present in the Matrix : 2 Enter the Element of First Matrix Enter the value of A : 5 Enter the value of A : 8 Enter the value of A : 3 Enter the value of A : 4 Enter the Element of Second Matrix Enter the value of B : 7 Enter the value of B : 6 Enter the value of B : 7 Enter the value of B : 9 1214 1013

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.

MORE TOPIC

WHAT WE DO

 Follow Us 