Programming in C ++
Lab 6: The preprocessor, testing - Homework
Important! Please submit the solution as a single text file containing answers and program code. Use a common text format (docx,pdf,etc.). Tasks are submitted via the form on the module's website. Solutions will not be accepted via email. If you have any questions, please contact the internship supervisor. There are 14 days to solve the task.
Deadline 30.04.2023 23:59:59
Task 1 - Constants (2 point)
Change this program so that PI is defied as a constant using the preprocessor:
#include <stdio.h> int main() { float radius, pi, area; pi = 3.14159; radius = 12; area = pi * radius * radius; printf("Area = %f", area); return 0; }
Explain what is the difference compared to making the variable pi a const (short explanation enough).
Task 2 - Macros (3 point)
Write a program which calculates the minimum or the maximum of two numbers, similar to what we did in lab 1. The program should accept three command line paramters, two float numbers and a string. If the string is min, the minimum of the numbers should be printed, for max the maxium, else and error message should be printed. Do this by using a macro which prints either the number or the error message. Notice that the macro can do the print directly. The wording implied that a value should be returned. This is possible, but not as straighforward as printing directly.
Task 3 - Equivalence classes and boundary value analysis (3 point)
We are writing an application for ordering pizza online. One of the values is the number of pizzas of one type the customer wants to order. We have decided to accept integer values from 1 to 99 as valid input here. The input comes from a web form, where the value is entered in a text field, therefore it can be anything. For this situation, suggest a separation of the input into equivalence classes. Your separation should be useful for the problem. Then, for each equivalence class, give the values you suggest to use for testing, using boundary value analysis.
Task 4 - Using doctest (2 point)
Write a short program which contains a function accepting a string as input and returning the input as an integer if it is a number from 1 to 99, else throws an exception of type string saying "invalid input". Then add tests for all values you have found as test values in the previous exercise. Check for an exception if the input is invalid.