object oriented pprograming polymorsphism, Exercises of Computer Science

exercise questions for home work

Typology: Exercises

2017/2018

Uploaded on 08/30/2018

mohsanshah
mohsanshah 🇵🇰

2 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab Week 14
Q no 1
Create a function called swaps () that interchanges the values of the two
arguments sent to it. (You will probably want to pass these arguments by
reference.) Make the function into a template, so it can be used with all numerical
data types (char, int, float, and soon). Write a main () program to exercise the
function with several types.
Q No 2:
Create a function that accept and swap the values of the array using template
function.
Q no 3:
Write program that create a Stack with exception handlings methods.
Q no 4: Write program that create a Stack with multiple catch blocks one for full
Stack and second for empty Stack exception handlings methods
Write a function calculateAverage() which takes four int arguments which
are marks for four courses in the semester and returns their average as a
oat.
The calculateAverage() function should take only valid range for marks
which is between 0 - 100. If the marks are out of range throw an
OutOfRangeException - dene this exception as a class
Solved examples for exception handling
#include <iostream>
using namespace std;
class OutOfRangeException {};
/*
* Pre conditions: all the four marks must be between
* 0 -100 otherwise the function throws a OutOfRangeException
* Post condition: returns the average of the four marks
* Invariants: avg >= 0 && avg <=100
*/
float calculateAverage(int m1, int m2, int m3, int m4) {
if ((m1 < 0 || m1 > 100))
throw OutOfRangeException();
if (m2 < 0 || m2 > 100)
throw OutOfRangeException();
if (m3 < 0 || m3 > 100)
throw OutOfRangeException();
if (m4 < 0 || m4 > 100)
throw OutOfRangeException();
return (m1 + m2 + m3 +m4 ) / 4.0;
}
pf2

Partial preview of the text

Download object oriented pprograming polymorsphism and more Exercises Computer Science in PDF only on Docsity!

Lab Week 14

Q no 1

Create a function called swaps () that interchanges the values of the two arguments sent to it. (You will probably want to pass these arguments by

reference.) Make the function into a template, so it can be used with all numerical data types (char, int, float, and soon). Write a main () program to exercise the function with several types. Q No 2: Create a function that accept and swap the values of the array using template function. Q no 3: Write program that create a Stack with exception handlings methods. Q no 4: Write program that create a Stack with multiple catch blocks one for full Stack and second for empty Stack exception handlings methods

Write a function calculateAverage() which takes four int arguments which are marks for four courses in the semester and returns their average as a float. The calculateAverage() function should take only valid range for marks which is between 0 - 100. If the marks are out of range throw an OutOfRangeException - define this exception as a class Solved examples for exception handling #include using namespace std;

class OutOfRangeException {};

/*

  • Pre conditions: all the four marks must be between
  • 0 -100 otherwise the function throws a OutOfRangeException
  • Post condition: returns the average of the four marks
  • Invariants: avg >= 0 && avg <= */ float calculateAverage(int m1, int m2, int m3, int m4) { if ((m1 < 0 || m1 > 100)) throw OutOfRangeException(); if (m2 < 0 || m2 > 100) throw OutOfRangeException(); if (m3 < 0 || m3 > 100) throw OutOfRangeException(); if (m4 < 0 || m4 > 100) throw OutOfRangeException();

return (m1 + m2 + m3 +m4 ) / 4.0; }

int main () { try { float avg = calculateAverage(79,90,100,90); cout << avg; }catch (OutOfRangeException ) { cerr << "marks out of range" << endl; } system("pause"); }