EXERCISE 1
Write a distance function whose parameters are 4 doubles xa, ya and xb, yb which represent the coordinates of two points A and B and which returns the distance AB. Test this function.
The purpose of this exercise is to check the following technical points:
Simple function creation.
Parameter passing by value.
Using return.
Call a function.
Here is the source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std; #include <cmath> double distance (double xa, double ya, double xb, double yb) { double dx, dy; dx = xa-xb; dy = ya-yb; return sqrt (dx * dx + dy * dy); } int main () { double x1, y1, x2, y2, d; cout << "Enter the abscissa of A:"; cin >> x1; cout << "Enter the ordinate of A:"; cin >> y1; cout << "Enter the abscissa of B:" cin >> x2; cout << "Enter the ordinate of B:" cin >> y2; d = distance (x1, y1, x2, y2); cost << "The distance AB is:" << d << endl; return 0; } |
EXERCISE 2
Write a function f having as parameters a double x and a boolean ok and which returns a double by a return. The function returns with a return the square root of (x-1) * (2-x). The function returns the value true if the function is defined at x, otherwise false. Test this function.
The purpose of this exercise is to check the following technical points:
Simple function creation.
Passing parameters by value and by reference.
Using return.
Input and output parameters of a function.
Call a function.
Game of tests of a function.
Here is the source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; #include <cmath> double f (double x, bool & ok) { double r = 0; if (x> = 1 && x <= 2) {r = sqrt ((x-1) * (2-x)) ok = true; else ok = false; return r; } int main () { double x, y; bool ok; cout << "Type x:"; cin >> x; y = f (x, ok); if (ok) cout << "f (x) is:" << << endl; else cout << "x is not correct" << endl; return 0; } |
EXERCISE 3
Write a function f having an integer parameter and returning a Boolean return: true if the integer is first false otherwise. Test this function.
The purpose of this exercise is to check the following technical points:
Simple function creation.
Call a function.
Validation of data before calling a function.
Function returning a boolean.
Here is the source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> using namespace std; #include <cmath> bool f (int x) { bool r = true; int d = 2; while (r && d * d <= x) if (x% d == 0) r = false; else d ++; return r; } int main () { int x; first bool; do{ cout << "Type x:"; cin >> x; } While (x <= 0); first = f (x); if (first) cost << "x is prime" << endl; else cout << "x is not prime" << endl; return 0; } |
EXERCISE 4
Write a function f having as parameter an integer n and which returns the nth prime number: this function will use the function of 3). Test this function.
The purpose of this exercise is to check the following technical points:
Creation of simple functions.
Function call.
Function that calls another function.
Here is the source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <iostream> using namespace std; first bool (int x) { bool r = true; int d = 2; while (r && d * d <= x) if (x% d == 0) r = false; else d ++; return r; } int Npremier (int N) { int nb = 0; int i = 2; while (nb! = N) { if (first (i)) nb ++; i ++; } return i-1; } int main () { int N, p; cout << "Enter the value of N:"; cin >> N; p = Npremier (N); cout << "The N-th prime number is:" << p << endl; return 0; } |
EXERCISE 5
Write a swap function with parameters 2 integers a and b that exchange the contents of a and b. Test this function.
The purpose of this exercise is to check the following technical points:
Simple function creation.
Call a function.
Parameter passing by references.
Here is the source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> using namespace std; void swap (int & x, int & y) { int temp; temp = x; x = y; y = Temp; } int main () { int a, b; cout << "Type a:"; cin >> a; cout << "Type b:"; cin >> b; swap (a, b); cost << "a is:" << a << endl; cout << "b is:" << b << endl; return 0; } |