Test 3 Solutions

Solutions for Test on DBC and Exceptions

2. No, correct software is not always robust because it may be not able to handle the cases and situations which are not in the specification however it may still work correctly as per the specification.

4. Some differences between assertions and exceptions:
In C++ for assertions you need to use the assert() function from the assert library whereas for exceptions you can define your own exception as these are simply classes

When assertions are used the program is terminated if the condition is false whereas using exceptions you can handle errors without terminating (such as letting the user try again) properly using try and catch.

Assertions are used to check pre conditions and post conditions so that errors can be prevented where exceptions are used for handling errors such as user errors or system errors.

Note that exceptions can be still be used for check pre and post conditions as shown in the solution for Q7 below.

5. and 6. See full working C++ code or Run the code online

    /*
     * Pre conditions: intArray is not NULL
     *                 arrsize is more than zero 
     * Post conditions: returns the index of the number
     *                  if the number is found in the array
     *                   else returns -1
     * Invariants: returned index will be between 0 - arrsize
     */
     int search(int *intArray, int arrsize, int number) {
             //check pre conditions using assertions
            assert(intArray != NULL);
            assert(arrsize != 0);
            //.....
     }

7.

    /*
     * Pre conditions: intArray is not NULL
     *                 arrsize is more than zero 
     * Post conditions: returns the index of the number
     *                  if the number is found in the array
     *                   else returns -1
     * Invariants: returned index will be between 0 - arrsize
     */
     int search(int *intArray, int arrsize, int number) {
             //check pre conditions using exceptions
            if (intArray == NULL)
                    throw InvalidArrayException();
            if (arrsize == 0)
                    throw InvalidArrayException();
 
     }

8 a) and b) See full working code or Run the code online

    class InvalidMarksListException {}
    class InvalidMarksException {}
 
    float calcAverageMarks(int marks[], int size) {
      //check if size of marks array is 4 
      if (size != 4)
            throw InvalidMarksListException();
      //check if all the four marks in the array are in range
      if (marks[0] < 0 || marks[0] > 100)
            throw InvalidMarksException();
      if (marks[1] < 0 || marks[1] > 100)
            throw InvalidMarksException();
      if (marks[2] < 0 || marks[2] > 100)
            throw InvalidMarksException();
      if (marks[3] < 0 || marks[3] > 100)
            throw InvalidMarksException();
 
      float avg = (marks[0] + mark[1] + marks[2] + marks[3]) / 4.0;
      return avg;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License