C Labs-2
C Labs-2
Preliminaries
Create and add to your project two files called Labs2.h and Labs2.cpp.
(in code::blocks, you can add "virtual folders" to separate headers and cpp files...)
Labs2.h file is your first personal header file and is going to contain all the signatures of the
procedures and functions that will be implemented in the Labs2.cpp file. It may also contain
variable definitions. For instance:
MyFunction2(float x) {
if (x == 0) cout <<"x is null"<<endl;
else cout <<"x is not null"<<endl;
}
Now, from the main.cpp file, you can call any function declared in Labs2.h and implemented in
Labs2.cpp, without taking care of the order of implementation.
Exercice 1 - Beginning
Within the main function: declare two integers a nd b, ask the user to enter these two variables, and
display them on the console screen.
Implement two functions min and max which take 2 parameters and return the minimum and
maximum values, respectively. Call both functions from the main function using values stored in
variables a and b, and display the results.
The keyword return can only return one value. You may sometimes need to return several values,
sometimes an error code and several results as well. To do so, pass arguments by references to
"return" multiple results.
As an exercice, declare and implement a function SQ(...) that returns simultaneously the square
and the square root of a number, and an error code if the square root can not be computed. Include
<cmath> for the sqrt() function.
Implement 3 overloaded functions named Sums(...) that return the sum of all the parameters. The
first will use 2 integers, the second 2 floats, and the third 3 floats.
A recursive functions is a function that calls itself during its execution. Declare and implement
function called RecursiveFactorial(...) to compute the factorial of a number defined as :
p
p != p∗ p−1∗...∗2∗1=∏i =1 i
Declare an array called Tab1 of 10 integers, where each element of index i contains the value i!
Display the complete array.
Declare a bidimensional array Tab2 that contains 8x8 integers. Initialize the first value (Tab2[0]
[0]) to 1, and all the other to 0.
In the Pascal triangle, sne can remark that the following relations hold:
Tab[i][0] = 1 and Tab[i][j] = Tab[i -1][j] + Tab[i -1][j -1] if j>0
Completely fill the array using the previous relationship, then display it. You should get the
following result:
1 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 2 1 0 0 0 0 0
1 3 3 1 0 0 0 0
1 4 6 4 1 0 0 0
1 5 10 10 5 1 0 0
1 6 15 20 15 6 1 0
1 7 21 35 35 21 7 1