List Of Experiments
Programs using C++
1. GENERATING THE ELECTRICITY BILL USING DEFAULT ARGUMENTS
2. GENERATING ‘N’ EMPLOYEES PAYROLL
- MATRIX MULTIPLICATION USING STATIC DATA MEMBERS & DEFAULT ARGUMENTS
- USING FRIEND FUNCTION
(i) SWAPPING & REMOVAL OF DUPLICATES IN AN ARRAY
- BANKING OPERATION (WITH ARRAY OF OBJECTS)
- INVENTORY OF BOOK USING DYNAMIC CONSTRUCTORS AND DESTRUCTORS
- OPERATOR OVERLOADING:
(i) STRING CONCATENATION BY OVERLOADING ‘+’ & STRING COMPARISON BY OVERLOADING ‘<=’ OPERATOR
(ii) MATRIX SUBTRACTION (-‘)
(iii) COMPLEX NUMBER ADDITION(‘+’) WITH TYPE CONVERSION
- EXCEPTION HANDLING
{i) IMPLEMENTATION OF STACK & QUEUE CONCEPT
- TEMPLATES
(i) DESIGN TEMPLATES FOR THE FOLLOWING – BUBBLE SORT,QUICK SORT,MERGE SORT,SELECTION SORT
- MERGING FILES
- Develop with suitable hierarchy, classes Design a simple test application to demonstrate dynamic polymorphism and RTTI
Ex No.1
Generating the electricity bill using Default Arguments
Aim:
An electricity board charges the following rates to domestic users to discourage large conceptions of energy.
100 units Rs 1.50 p/unit
200 units Rs 1.80 p/unit
Beyond 200 Rs 2.50 p/unit
All users are charged a minimum of Rs 50/-. If the total amount is more than 300 then an additional surcharge of 15% is added. Write a c++ program to read the names of users, number of units consumed and print out the charge using default.
Algorithm
Step 1: Initialise the total function with unit=83&&sur=0.15.
Step 2: Read the number of consumers.
Step 3: Read the name of the units of the entire customer.
Step 4: Call the function total(No.of units consumed as default arg).
Step 5: if the unit is less than or equal to 100.
Step 6: Calculate the amount as (unit * 1.50).
Step 7: else if the unit is greater than 100 and if the unit is less than 200.
Step 8: Calculate the amount as 150+((units-100)*1.80).
Step 9: else if the unit is greater than 200.
Step 10: Calculate the amount as 330+((units-200)*2.50)
Step 11: if the calculated amount is greater than Rs 300/-
Step 12: calculate amount as amount * sur.
Step 13: if the unit is less than 83.
Step 14: display the amount as 50.
Step 15: else display the calculated amount.
Ex No.2
Generating ‘N’ Employees Paybill
Aim:
Write a program to generate ‘n’ employees paybill
Algorithm
Step 1: Create an array of objects
Step 2: Read ‘n’ employee details using read() function in a for loop
Step 3: Calculate the gross & net sal for each employee using cal() fn
Step 4: Call the function display() in a for loop to display all details
EX.NO.3
MATRIX MULTIPLICATION USING STATIC DATA MEMBERS & DEFAULT ARGUMENTS
Aim:
To write a C++ program to perform matrix multiplication
Algorithm:
Step 1: Get the size of the two matrices(r1,c1,r2,c2) and pass it as
parameters .If not passed, take the default values assigned.
Step 2: Declare the resultant matrix as static data member so that all the
values in the matrix are initialized with the value 0.
Step 2: if(c1!=r2) print “matrix multiplication not possible” else go to
step 3
Step 3: Read two matrices using read() fn.
Step 4: Multiply two matrices using mul() fn
Step 5: Call display() fn to display the resultant matrix.
Ex. No: 4
SWAPPING & REMOVAL OF DUPLICATES IN AN ARRAY USING FRIEND FUNCTION
Aim:
Write a program to swap two array elements & removing duplicates from an array using friend functionb
Algorithm
Step 1: Define two classes that has an array & its size. Both classes share a friend function to swap() the array elements. One of the class has friend function duplicate() to remove duplicate array elements
Step 2: Create objects for both the classes
Step 3: Read array elements of both the classes using read()
Step 3: Call the friend function swap that swaps two array elements.(Don’t call it using
an object because it is friend fn)
Step 4: Call the friend function duplicate() to remove duplicates from an array
Step 5: Call display() from both the classes to print the array
Ex. No: 5 BANKING OPERATION USING CLASS AND OBJECTS
Aim
To Write a C++ program to represent a bank account using class and objects.
Algorithm
Step 1: Create an array of objects for the class bank
Step 2: Read ‘n’ customer records in a for loop using read()
Step 3: Call the member functions withdraw() & deposit() for transactions
Step 4: If the choice is deposit call the deposit member function using the function.
(a) Read the account number.
(b) Check whether this account number is already exiting in the list. If true
(c) Read the amount to be deposited.
(d) Update the balance by adding the amount deposited to the balance.
(e) Display the balance.
Step 5: If the choice is withdraw call the withdraw member function using the function.
(f) Read the account number.
(g) Check whether this account number is already exiting in the list. If true
(h) Read the amount to be withdrawn
(i) If the (balance-amt) <0 then deny withdraw operation else continue
(i) Update the balance by deducting the amount withdrawn from the balance.
(j) Display the balance.
Step 4: Using display() to print all details.
EX. NO:6
INVENTORY OF BOOK USING CONSTRUCTOR AND DESTRUCTOR
Aim
To write a C++ program for booking using constructor and destructor.
Algorithm
Step 1: Create an object for the class book.
Step 2: Declare the pointer variable for author, title, and publisher and the Variable for price and stock position.
Step 3: Read the number of records.
Step 4:Display a menu with the following choice create ,buybook, transaction and display.
Step 5:Using switch case execute the statements corresponding to the choice.
Step 6:If the choice is create, read the title, author, publishes, price and stock position and pass it to the constructor of the book class.
Step 7;Find the string length of each of the pointer variables dynamically.
Step 8:If the choice is buy books, read the title, author, stock from the user and check these with the array already created.
Step 9:If the author name and title matches then display the message”:Available” and read the number of copies.
Step 10:Decrement the stock position by 1 and display the mount to be paid. Increment successful transaction by 1. Else display “NOT success” and increment the unsuccessful transaction by 1.
Step 11:If the transaction; display the variables, successful transaction and unsuccessful transaction.
Step 12:If the choice is display, then display all the details such as title, author, price, publishes and stock position.
EX.NO:7 (a)
STRING CONCATENATION BY OVERLOADING ‘+’ & STRING COMPARISON BY OVERLOADING ‘<=’ OPERATOR
Aim:
Write a program to concatenate two strings and comparing it by overloading ‘+’ & ‘<=’ operators respectively
Algorithm:
Step1: Read two strings and pass it as parameters in the constructor. The
constructor allocates memory dynamically for the strings
Step2: Call operator fn + to concatenate two strings and operator fn <= two
compare two strings.
Step3: Call display() to display the concatenated string
EX.NO:7(b) MATRIX SUBTRACTION BY OVERLOADING ‘-‘
Aim:
To write a C++ program to perform matrix subtraction using operator overloading
Algorithm:
Step 1: Get the size of the two matrices(r1,c1,r2,c2) and pass it as
parameters .If they are equal, allocate memory dynamically for the matrices else print “subtraction not possible”
Step 2: Read two matrices using read() fn.
Step 4: Subtract two matrices using operator fn ‘-‘
Step 5: Call display() fn to display the resultant matrix.
EX.NO:7(c)
COMPLEX NUMBER ADDITION BY OVERLOADING ‘+‘ AND PERFORM TYPE CONVERSION
Aim:
To write a C++ program to perform complex addition using operator overloading and also perform type conversions such as int to complex, float to complex & vice versa
Algorithm:
Step 1: Get the two complex numbers and pass it as
Parameters in the constructor.
Step 2: Using constructors convert int type to complex type, float type to
complex
Step 4: Using casting operator convert complex to float type
Step 5: Call operator fn ‘+’ to add two complex numbers
Step 5: Call display() fn to display the resultant complex number
Ex No 8(a) STACK USING ARRAYS (EXCEPTION HANDLING)
Aim :
To implement PUSH and POP in stack using arrays. Handle the exceptions like stack underflow & overflow.
Algorithm :
Step1:Define a class stack. It has as an array & its size as data members.TOP
To implement PUSH and POP in stack using arrays. Handle the exceptions like stack underflow & overflow.
Algorithm :
Step1:Define a class stack. It has as an array & its size as data members.TOP
pointer points to last item in the stack
Step 2: The operations on the stack are
Step 2: The operations on the stack are
a)PUSH data into the stack
b)POP data out of stack
b)POP data out of stack
Step 3: PUSH DATA INTO STACK
3a.Enter the data to be inserted into stack.
3b.If TOP is NULL
the input data is the first item in stack.
TOP points to that item.
3c.If TOP is NOT NULL
the link of TOP points to the new item to be inserted .
3a.Enter the data to be inserted into stack.
3b.If TOP is NULL
the input data is the first item in stack.
TOP points to that item.
3c.If TOP is NOT NULL
the link of TOP points to the new item to be inserted .
Else if TOP==n
Throw an exception “Stack overflows” and handle it
Step 4: POP DATA FROM STACK
4a.If TOP is NULL
the stack is empty
Step 4: POP DATA FROM STACK
4a.If TOP is NULL
the stack is empty
Throw the exception “stack underflows”
4b.If TOP is NOT NULL
the link of TOP is the current TOP.
the pervious TOP is popped from stack.
Step 5. The stack is traversed to display its content.
4b.If TOP is NOT NULL
the link of TOP is the current TOP.
the pervious TOP is popped from stack.
Step 5. The stack is traversed to display its content.
Ex No 8(b) QUEUE USING ARRAYS (EXCEPTION HANDLING)
Aim :
To implement ENQUEUE and DEQUEUE in queue using arrays. Handle the exceptions like full & empty
Algorithm :
Step1:Define a class queue. It has as an array & its size as data members.REAR
To implement ENQUEUE and DEQUEUE in queue using arrays. Handle the exceptions like full & empty
Algorithm :
Step1:Define a class queue. It has as an array & its size as data members.REAR
pointer points to last item in the stack & FRONT points to the first item in
the queue.
Step 2: The operations on the queue are
a)ENQUEUE inserts data into the queue
b)DEQUEUE removes data out of queue
b)DEQUEUE removes data out of queue
Step 3: ENQUEUE
3a.Enter the data to be inserted into queue.
3b.If REAR=FRONT = NULL
the input data is the first item in queue.
BOTH REAR & FRONT points to that item.
3c.If REAR IS NOT NULL
the new item is inserted at REAR end
3b.If REAR=FRONT = NULL
the input data is the first item in queue.
BOTH REAR & FRONT points to that item.
3c.If REAR IS NOT NULL
the new item is inserted at REAR end
if REAR==n
Throw an exception “QUEUE FULL” and handle it
Step 4: POP DATA FROM STACK
4a.If FRONT is NULLthe queue is empty.Throw the exception “queue empty”
4b.If FRONT is NOT NULL
Remove that item from the queue in FIFO order and change the FRONT
4a.If FRONT is NULLthe queue is empty.Throw the exception “queue empty”
4b.If FRONT is NOT NULL
Remove that item from the queue in FIFO order and change the FRONT
Step 5. The queue is traversed to display its content.
Ex No 9 SORTING ALGORITHMS
Aim:
Develop templates of standard sorting algorithms such as bubble sort, insertion sort, merge sort, and quick sort
Algorithm :
Bubble Sort:
Step 1: Read the unsorted array elements
Step 2: for I = L to U
Step 3: for J = L to (U-1) –I
Step 4: if a[J] > a[J+1} then
Temp = a[J]
a[J]=a[J+1]
a[J]=temp
Insertion Sort:
Step 1: Read the unsorted array elements
Step 2: A[0]=minimum integer-value
Step 3: Repeat step 4 through n for k= 1,2,3,… N-1
Step 4: temp=A[k]
Step 5: ptr=k-1
Step 6: Repeat steps 7 to 8 while temp < A[ptr]
Step 7: ptr=ptr-1
Step 8: A[ptr+1]= A[ptr] // moves element forward
Merge sort:
Step 1: ctrA = L1;ctrB=L2;ctrC=L3
Step 2: while ctrA <= U1 and ctrB <= U2 perform steps 3 through 10
Step 3: if A[ctrA] < B[ctrB] then
Step 4: C[ctrC]=A[ctrA]
Step 5: ctrC=ctrC+1
Step 6: ctrA=ctrA+1
Step 7: Else
Step 8: C[ctrC]=B[ctrB]
Step 9: ctrC=ctrC+1
Step 10: ctrB=ctrB+1
Step 11: if ctrA > m-1 then
Step 12: while ctrB >= 0 perform steps 13 through 15
Step 13: C[crtC]=B[ctrB]
Step 14: ctrC= ctrC - 1
Step 15: ctrB=ctrB – 1
Step 16
EX NO:10 MERGING FILES
Aim:
Write a program to merge two files
Algorithm:
Step2: Open files f1 & f2 in “r” mode
Step3: Read the contents of f1 and write it to f3.Then read the contents of f2 and write it to f3.
Step4: Close all the files
EX NO 11: STUDENT EVALUTION USING INHERITANCE
Aim
To write a C++ program for students evaluation concept using inheritance.
Algorithm
Step 1: create a class studentpersonal declare roll no,age,name,sex in protected mode.
Step 2: Using a parameterized constructor initialize the values for all the data members.
Step 3: Create another class studentmark that is inherited from the base class and having the data members mark1,mark2,mark3,mark4 Using a parameterized constructor initialize the value for mark1,mark2,mark3,mark4.
Step 4: Create another class called studentsports and declare score as protected mode.
Step 5: Create a class studentresult and public inherited form studentmarks and studentsports having the data members total,avg,grade..
Step 6: Calculate the total and avg and display the result.
For more information contact us via Studentstrainer@gmail.com. Caring is sharing, show your caring to us by sharing our post in social sites and keep like us,,,
{ 0 comments... read them below or add one }
Post a Comment
Enter your comments here