Assignment CSE202

Date of allocation: 25/09/2012 Date of Submission: 19/09/2012 1. Write a program to enter values into a two 2-D arrays. Perform multiplication of the arrays and find and display the sum of both left and right diagonal of the resulting array. (5 Marks)

Ans: #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],b[10][10],c[10][10],m,n,o,p,i,j; cout<<"Enter number of rows of A: "; cin>>m; cout<<"Enter number of coloumns of A: "; cin>>n; cout<<endl<<"Enter elements of matrix A: "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<"Enter element a"<<i+1<<j+1<<": ";

cin>>a[i][j]; } } cout<<endl<<"Enter number of rows of B: "; cin>>o; cout<<"Enter number of coloumns of B: "; cin>>p; cout<<endl<<"Enter elements of matrix B: "<<endl; for(i=0;i<o;i++) { for(j=0;j<p;j++) { cout<<"Enter element b"<<i+1<<j+1<<": "; cin>>b[i][j]; } } cout<<endl<<"Displaying Matrix A: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<a[i][j]<<" "; } cout<<endl<<endl; } cout<<endl<<"Displaying Matrix B: "<<endl<<endl; for(i=0;i<o;i++)

{ for(j=0;j<p;j++) { cout<<b[i][j]<<" "; } cout<<endl<<endl; } if(n==o) { for(i=0;i<m;i++) { for(j=0;j<p;j++) { c[i][j]=0; for(int k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } cout<<endl<<"Matrix A * Matrix B = Matrix C: "<<endl<<endl; for(i=0;i<m;i++) { for(j=0;j<p;j++) { cout<<c[i][j]<<" "; }

cout<<endl<<endl; } } else cout<<"Multiplication not possible"; getch(); } 2. Define a class to represent a bank account. Include the following: Data Members a) Name of the depositor b) Account Number c) Type of account d) Balance amount in the account Member functions a) To assign initial values b) To deposit an amount c) To withdraw an amount after checking the balance. d) To display the name and balance. Write a main function to test the program for handling 10 customers Ans: #include<iostream.h> #include<conio.h> #include<stdlib.h> class bank { char name[22]; int acc_no; (5 Marks)

char type_acc[22]; int bal; public: void accept() { cout<<"\n\nEnter the account number: "; cin>>acc_no; cout<<"\nEnter the customer name: "; cin>>name; cout<<"\nEnter the type of account: "; cin>>type_acc; cout<<"\nEnter the balance in account: "; cin>>bal; } void disp() { cout<<"\n\n Customer Account Number : "<<acc_no; cout<<"\n Customer Name : "<<name; cout<<"\n Customer account type : "<<type_acc; cout<<"\n Customer account balance : "<<bal; }

void deposite(float ac_no) { if(acc_no==ac_no) { int x;

cout<<"\n Enter the amount to be deposited : "; cin>>x; bal=bal+x; } }

void withdraw(float ac_no) { if(acc_no==ac_no) { int x; cout<<"\n Enter the withdrawl amount : "; cin>>x; if(bal<x) cout<<"\nWithdrawl amount is greatere than the current balance "; else bal=bal-x; } } };

int main() { bank b[12]; int ch,n; clrscr();

do { cout<<"\n MAIN MENU "; cout<<"\n 1.ACCEPT CUSTOMER DETAILS"; cout<<"\n 2.DISPLAY CUSTOMER DETAILS "; cout<<"\n 3.DEPOSITE AMOUNT"; cout<<"\n 4.WITHDRAWL AMOUNT"; cout<<"\n 5.EXIT "; cout<<"\n ENTER YOUR CHOICE : "; cin>>ch; switch(ch) { case 1: cout<<"\n Entering record for 10 customers as asked in question : "; for(int i=0;i<10;i++) b[i].accept(); break; case 2: for(i=0;i<10;i++) b[i].disp(); break; case 3: int ac_no; cout<<"\n Enter the account no. in which you want to deposite money : "; cin>>ac_no; for(i=0;i<10;i++) b[i].deposite(ac_no); break;

case 4: cout<<"\n Enter the account no. from which you want to withdraw money : "; cin>>ac_no; for(i=0;i<10;i++) b[i].withdraw(ac_no); break; case 5: exit(0); } } while(ch!=5); getch(); return(0); }

3. Define a class string. Use overloaded == operator to compare two strings

(5 Marks)

Ans: #include<iostream.h> #include<conio.h> #include<string.h> class string { public: char st[35]; void operator ==(string s) { if( strcmp(st, cout<<"\nEntered strings are the same ";

else cout<<"\nEntered strings are not same "; } };

void main() { clrscr(); string s1,s2; cout<<"\nEnter first string : "; cin>>; cout<<"\nEnter second string : "; cin>>; s1==s2; getch(); } 4. Assume that a bank maintains two kinds of accounts for customers, one called as savings account and the other as current account. The savings account provides compound interest and withdrawal facilities but no check book facility. The current account provides check book facility but no interest. Current account holders should also maintain a minimum balance and if the balance falls below this level, a service charge is imposed. Create a class accounts that stores customer name, account number and type of account. From this derive the classes cur_acct and sav_acct to make them more specific to their requirements. Include necessary member functions in order to achieve the following tasks: (5 Marks) a) Accept deposit from a customer and update the balance b) Display the balance c) Compute and deposit interest d) Permit withdrawal and update the balance e) Check for the minimum balance, impose penalty, if necessary and update the balance.

Ans: #include<iostream.h> #include<conio.h> #include<stdlib.h>

class account { public: char cust_name[10]; int accno; int typeofac;

void getaccount() { cout<<"enter customer name: "; cin>>cust_name; cout<<endl<<"enter account number"; cin>>accno; }

void displayaccount() { cout<<cust_name<<endl<<accno<<endl; if(typeofac==1) cout<<"savings"; else

cout<<"current"; } };

class savings: public account { public: int interest; int with_amt; int balance; int depo_amt;

void basicbalance() { cout<<endl<<"enter the balance: "; cin>>balance; int i; do { cout<<endl; cout<<endl<<"1.Deposite amt"; cout<<endl<<"2.Withdraw amt"; cout<<endl<<"3.Calculate interest"; cout<<endl<<"4.Show balance"; cout<<endl<<"5.Exit"; cin>>i; switch(i)

{ case 1: depositamt(); break; case 2: withdrawal(); break; case 3: calculateinterest(); break; case 4: showbalance(); break; case 5: exit(0); } }while(i!=5); } void depositamt() { cout<<endl<<"enter the amount to be deposited"; cin>>depo_amt; balance = balance + depo_amt; } void showbalance() { cout<<"the balance is"<<balance;

} void calculateinterest() { interest = balance * 0.10; cout<<endl<<"the interest is"<<interest; balance = balance+interest; } void withdrawal() { cout<<endl<<"Enter the withdrawal amount"; cin>>with_amt; balance=balance - with_amt; } }; class current : public account { public: int chequeno; char bankname[10]; int balance; int depo_amt; int with_amt;

void basicbalance() { cout<<endl<<"Enter the balance"; cin>>balance;

int i; do{ cout<<endl; cout<<endl<<"1. Depositamt"; cout<<endl<<"2. Withdrawamt"; cout<<endl<<"3. Show balance"; cout<<endl<<"4. Exit"; cin>>i; switch(i){ case 1: depositamt(); break; case 2: withdrawal(); break; case 3: showbalance(); break; case 4: exit(0); } } while(i!=4); } void depositamt()

{ cout<<endl<<"Enter the cheque no"; cin>>chequeno; cout<<endl<<"Enter the bank name"; cin>>bankname; cout<<endl<<"Enter the amount to be deposited"; cin>>depo_amt; balance = balance + depo_amt; } void showbalance() { cout<<"The balanace is "<<balance; } void withdrawal() { cout<<endl<<"Enter the withdrawalamount"; cin>>with_amt; balance = balance - with_amt; if (balance <= 500) { int charge; charge = (500-balance) * 0.11; cout<<endl<<"The service charge is"<<charge; cout<<endl<<"As balance is less than500"; balance = balance - charge; } }

}; void main() { clrscr(); savings s; s.getaccount(); cout<<endl<<"Enter the type of account"; cout<<"1. Savings"; cout<<"2. Current"; cin>>s.typeofac; s.displayaccount(); if (s.typeofac == 1)s.basicbalance(); else { current c; c.basicbalance(); } getch(); }

5. Write a program to accept records of different states using array of structures. The

structure should contain char State and number of int engineering colleges, int medical college, int management colleges. Calculate total colleges and display the state, which is having highest number of colleges. Ans: #include <iostream.h> #include <conio.h> (5 Marks)

struct data { char state[10]; int eng_col; int med_col; int mang_col; int total; }; void main() { struct data r[3]; int i,hc=0,x=0; clrscr(); for(i=0;i<2;i++) { cout<<"\nState : "; cin>>r[i].state; cout<<" Engineering Colleges : "; cin>>r[i].eng_col; cout<<" Medical Colleges : "; cin>>r[i].med_col; cout<<" Management Colleges : "; cin>>r[i].mamg_col; r[i].total=r[i].eng_col+r[i].med_col+r[i].mamg_col; cout<<"\n Total: "<<r[i].total; if (i>0) {

if (r[i].total>hc) { hc=r[i].total; x=i; } } else { hc=r[i].total; x=i; } } cout<<"\nState having maximum colleges "; cout<<"\nState: "<<r[x].state; cout<<"\nEngineering Colleges : "<<r[x].eng_col; cout<<"\nMedical Colleges : "<<r[x].med_col; cout<<"\nManagement Colleges : "<<r[x].mamg_col; cout<<"\n Total colleges : "<<r[x].total; getch(); }
6. Write a function power() to raise a number m to a power n. The function takes a double

value for m and int value for n and eturns the result correctly. Use a default value of 2 for n to make the function to calculate squares when this argument is omitted. Write a main that gets a value of m and n from the user to test the function. (5 Mar

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

#include<math.h> Power(double m, int n=2) { Return = power(m, n); } Void main() { Cout<<”enter value of m and n: ”; Cin>>m>>n; Power(m,n); }

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.