Professional Documents
Culture Documents
Master ENG/ENV/TECH
2017
General instruction for all lab
Your work consists of sources answering exercises given below. They must be provided as indi-
vidual text files following the naming convention of your subject. For each exercise a table, called
requested information table hereafter, sets precisely what is expected in terms of naming conven-
tion, precises order of input data for the programs you create and output format of these programs
results (see chapter 8.4 for complete explanations). You are expected to upload an archive con-
taining all your work in resource Assignments of course Labs section of Pedagogic server (no
individual file will be allowed).
An auto evaluation tool called auto eval.bash (see chapter 8.1, 8.2, and 8.3 for details) given
with this document, has to be used to generate this archive. It can also be used to auto evaluate
your archive before submission and check partially the correctness of your work.
The same auto evaluation tool will be used by the teachers to evaluate your work.
Failure to return the sources archive on time on the server will result in a grade
of zero for this homework.
Uploading an archive with a wrong structure or name (i.e. archive not readable
or not named correctly) will result in a grade of zero for this homework.
Hexadecimal representation
In some exercises we will use hexadecimal representation of integers. Here is a table to recall you
the correspondence between decimal and hexadecimal notations:
decimal hexadecimal
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 A
11 B
12 C
13 D
14 E
15 F
1
To avoid confusion with decimal representation a hexadecimal number is always prepended by
a 0x or 0X.
Here are some little examples of arithmetic operations with hexadecimal notation:
0x32FF+0x0021=0x3320
0x329F+4=0x32A3
0x000A*2=0x0014
If you want you can translate in decimal and do the arithmetic operation and then translate
back to hexadecimal notation: 0x32FF+0x0021=13055+33=13088=0x3320
In this lab we will use 0x notation only and all letter will be in upper case. All
hexadecimal numbers will use 4 digits by prepending 0 if necessary.
Open a terminal (browse menu to find this program). Then click inside this window. Thats
it, you are now interacting with Linux command interpreter!
In your home directory (where you arrive if you just type cd ) type:
> mkdir -p ComputerLab/lab1
then get into this new folder:
> cd ComputerLab/lab1
then type:
> gedit hello.cc (or any other editor than gedit that you want)
and pass to next training.
Using the console create a directory named ComputerLab in your home directory. Inside this
directory create a directory named lab1. Edit a file (use for instance gedit) named test1.txt, put
whatever you want inside it. Using the console, try to change its name. Try to copy it in your
home directory. Modify this file on the home directory, copy it with the name test2.txt in the
ComputerLab/lab1.
Look at the rights you have on the file. Change your rights on the file to read only, try to
modify the file using an editor. You can use your one-sheet linux survival guide to help you do
that. Remember that you can get a complete description of each command using the command
man, or a short help by adding --help or -h at the end of the command.
There is much more that can be done within the command line. Even complicated scripts can
be written to help you do repetitive tasks. The command language usually used on Linux is the
bash (born again shell).
When you have time, you can learn more tricks with the console. For example, for shell script,
you can have a look here.
2
Training - First program.
Write a hello word program (a program that prints hello world on the screen), something similar
to the first C++ program that we have seen in class. Make sure you understand all the commands.
Compile it, correct it if needed. Execute it. Try to add errors in the program. Compile it. Try
to understand the error messages.
As it is just training, no source are requested for evaluation.
3
1 A simple program.
1.1 First basic Input/Output
Write a program basic io that asks for your age and then prints it on the screen.
You'll need to look at course C1 for basic i/o and possibly take a look on the Web to complete
it.
Modify your code so that it also asks for your height in meters, and then prints it out.
Output
A message M telling if T is degenerate or not.
The barycentric coordinate of point P if T not degenerated.
Arbitrary fake barycentric coordinate 0. 0. 0. if T is degenerated
Having input information the program first checks that T is not degenerate, saying that its
points are not collinear nor at the same location. Dont start doing your own set of tests for that.
The way barycentric coordinates have to be computed following the mathematical definition given
below induces a natural degenerate triangle test. So if T is degenerate program must give message
M:
TRIANGLE : degenerated
4
and :
TRIANGLE : correct
otherwise.
Then the program computes and outputs P barycentric coordinates in given triangle system
according to the output rule given above.
Requested information
Files
bary triangle.cc
Input
x1 , y1 , x2 , y2 , x3 , y3 , x then y
Output
lambda1(1 ), lambda2(2 ), lambda3(3 ) and TRIANGLE(M)
Scaling : 4/19
Write two functions, named maximum and minimum. maximum must return the maximum of
the two doubles given as inputs of the function. minimum must return the minimum of the two
doubles given as inputs of the function.
The two functions must be declared in a header file named mathfunction.h and implemented
in a separate mathfunction.cc file.
5
To test your maximum and minimum functions, you need to create a main program test mathfunction
that calls them with two doubles a and b asked to the user and outputs the result of their compu-
tation on the terminal (following requirements below). You will write it in a test mathfunction.cc
file. When it's done you can compile it:
> g++ -c test mathfunction.cc
This will produce the file test mathfunction.o.
You can now produce an executable by linking test mathfunction.o and mathfunction.o:
> g++ mathfunction.o test mathfunction.o -o test mathfunction.exe
Requested information
Files
mathfunction.h,mathfunction.cc,test mathfunction.cc
Input
a then b
Output
MAXIMUM(max(a, b)) and MINIMUM(min(a, b))
Scaling : 1/19
Here test mathfunction.o may be of great interest only in large projects where you are compiling
not one, but hundreds of programs, or if compilation of the main itself is very very long. Then you
are happy when you do a modification in a shared library not to compile again all the programs but
just link their associated .o files with the new library. This saves compilation time.
In our case we may shorten the compilation step by using the following commands as test mathfunction.cc
is obviously very quick to compile:
> g++ -c mathfunction.cc
> g++ mathfunction.o test mathfunction.cc -o test mathfunction.exe
And again as in this case mathfunction.cc is also obviously very quick to compile, we may even
be shorter:
> g++ mathfunction.cc test mathfunction.cc -o test mathfunction.exe
But this alleviates all the advantages of the library concept which is here among many things
to avoid as much as possible re compilation of unchanged sources. In the following labs you will
quickly adopt this concept to avoid losing time waiting for the compilation to finish.
4 First loops
Your program in both cases must use one of the loop structures available in C++ and use double
type for real arithmetic.
6
Requested information
Files
mean 1.cc
Input
x1 , x2 ,x3 ,x4 ,x5 then x6
Output
Mean(m)
Scaling : 1/19
Requested information
Files
mean 2.cc
Input
xi
Output
INPUT(N ), MEAN(m)
Scaling : 1/19
7
Declare mySort in a header file mySort.h, implement it in a mySort.cc file and test it with the
main program given by file test mySort.cc. Whatever implementation you chose for this function,
this program should give the appropriate results without modification of test mySort.cc source file.
Requested information
Files
mySort.cc, mySort.h
Input
Output
Scaling : 1/19
Note: This is the first example of a requested information table with empty Input/Output
directives. This is normal since you have to use the given teacher test .cc file (and you are not
expected to change anything in it) which does input/output. Auto evaluation tool is tuned to work
with this file and your evaluation will be done here only on the results that your functions give and
not also on the way you present results (as in previous exercises). In all the labs you will have
many exercises that work like that.
Requested information
Files
refMySort.cc, refMySort.h
Input
Output
Scaling : 1/19
1 i n t a [ ] = {1 , 2 , 3 , 4 , 5};
2 i n t pa = &a [ 0 ] ;
3 c o u t << pa <<e n d l ;
4 c o u t << (++pa ) << e n d l ;
5 c o u t << ( pa+2) << e n d l ;
On line 2, the pointer pa points to the first element of array a. On line 3 pa is derefer-
enced and the value it points to is printed on screen. It therefore prints the value 1.
8
On line 4, pa is incremented using ++ (as pres operator, recall course 1 on operator and prece-
dence), and then dereferenced. When ++ is applied, pa points to the next integer in memory,
which is the same as the one stored in slot a [1] , which is 2. Line 4 therefore prints 2 on the screen.
Note: On line 2, &a[0] is the address of the first element of a. Line 2 could have been
replaced by the following: int pa =a; which has the exact same meaning: the identifier of an
array variable is equivalent to a non-modifiable pointer that contains the address of the first element
of the array. By nature it then can be assigned to a pointer, and in this case pa is set to the
address of the first element of the a array.
9
37 cout<<R16 : <<temp<<e n d l ;
38 ptr [1]=27;
39 p t r=b l o c k s + 3 ;
40 cout<<R17 : <<ptr<<e n d l ;
41 temp=ptrb l o c k s ;
42 cout<<R18 : <<temp<<e n d l ;
43 temp=( p t r +2) ;
44 cout<<R19 : <<temp<<e n d l ;
45 ptr =3;
46 cout<<R20 : <<ptr<<e n d l ;
47 return 0;
48 }
For the order of evaluation of the operator, you can refer to the table of course 1.
Requested information
Files
pointer array.txt
Input
Output
R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15,
R16, R17, R18, R19 and R20 following code above
Scaling : 4/19
Output A pointer to the first even number in the array after sorting (or a pointer
to the location in memory past the end of the array a if no even number
is entered). a array is sorted fulfilling requirement above.
The aim of this exercise is to pack odd and even number into two sets in the array a.
Depending on your algorithm many values order are possible for both sets so we need to have a
common order to ease the correction of your work. Sorting those sets is then mandatory but it
is far behind the scope of this course. To simplify this sorting task you will use the qsort of the
standard library. Here an example on how to use it:
1 #i n c l u d e <i o s t r e a m >
2 // D e c l a r a t i o n o f q s o r t i s :
3 // v o i d q s o r t ( v o i d base , s i z e t nmemb, s i z e t s i z e ,
4 // i n t ( compar ) ( c o n s t v o i d , c o n s t v o i d ) ) ;
5 // i t i s g i v e n i n t h i s h e a d e r
6 #i n c l u d e <c s t d l i b >
10
7 u s i n g namespace s t d ;
8
10 // H e l p i n g Function f o r q s o r t ( a s c e n d i n g o r d e r )
11 i n t intCompare ( c o n s t v o i d i , c o n s t v o i d j )
12 {
13 i f ( ( ( i n t ) i ) < ( ( i n t ) j ) )
14 r e t u r n 1;
15 e l s e i f ( ( ( i n t ) i ) > ( ( i n t ) j ) )
16 return 1;
17 else
18 return 0;
19 }
20
21
22 // main
23 i n t main ( )
24 {
25 // an a r r a y t o s o r t
26 const int n = 7;
27 int some int [ n ] = {42 ,2 ,6 ,8 ,99 ,23 ,2};
28
29 // s o r t i n g a r r a y with q s o r t
30 q s o r t ( s o m e i n t , n , s i z e o f ( i n t ) , intCompare ) ;
31
32 // output
33 f o r ( i n t i = 0 ; i < n ; ++i ) cout<< <<s o m e i n t [ i ] ;
34 cout<<e n d l ;
35
36 return 0;
37 }
Use exactly the same way this qsort function (with intCompare function) to sort each set (odd and
even) in your sortOdd function (two calls to qsort must be present in the implementation
of your sortOdd function, no more, no less). You must not implement your own version of
sorting algorithm or worse implementing your own version of sorting and packing together algo-
rithm (if you are interested by the subject, and have time... Donald Knuth in The Art of Computer
Programming III dedicated more then 350 pages to it... ). Last but not least it is prohibited to
use any other function than intCompare with qsort. One may be lazy and change intCompare
so that it considers also an even number greater than an odd one... For those who are interested
it may be an exercise, apart from labs, to modify intCompare so that in one qsort call you get the
result expected by this subject.
Implement sortOdd function in an sortOdd.cc file and test it with the main program given by
file test sortOdd.cc which includes directly this file (your work is included in the test sortOdd.cc
source just as if you had typed this function in this file). This inclusion controls that you will
use the intCompare implemented in test sortOdd.cc and not yours. Whatever implementation you
chose for this function, this program should give the appropriate results without modification of
test sortOdd.cc source file. Here is an example:
11
Requested information
Files
sortOdd.cc
Input
Output
Scaling : 5/19
8 Appendix
8.1 Auto eval.bash limits
This tool is just a help to verify mainly that:
Your program is located in correct source files having correct names.
No file is missing. If so you will be able to pass through all steps but your points for the
missing file will be lost.
All programs, at least, compiles and runs correctly (to obtain the 2 points see 8.2).
With the student parameter setting, it gives correct results (to obtain the 6 points see 8.2).
The archive given to teacher has the correct structure and name.
What this tool doesnt verify is that:
Your programs are correct in absolute. Be careful auto eval.bash is tuned to have exercises
running with a set of parameters but a different setting will be used by the teacher to
correct your work. This means that your code may work with auto eval.bash and the student
parameter setting but not with the teacher parameter setting. Its your responsibility to
ensure that your program is correct and works with any setting.
Your programs are correct if you just generate an archive and didnt pass through evaluation
step.
You give your correct group identification number. If you generated an archive following a
wrong group ID and upload it as it is then you will have a grade of zero for this homework.
12
8.3 Getting and using auto eval.bash
auto eval.bash like this pdf files comes from self extracting file labX.bash that you download from
resource Lx subject in https://hippocampus.ec-nantes.fr Labs section of M1 M-ENG PROGR
course, where X corresponds to current lab number.
Place auto eval.bash in the folder where you have all exercises sources that have to be given to
teachers. Then in this folder just type:
> ./auto eval.bash
If you launch the script for the first time, you have to create an archive of your work. Select
Prepare an archive of your work.
Create archive
A .gz archive file is created, and ready to be uploaded on the pedagogic server. If you want, at
this point you can stop the program (Type q). Note that theses two steps at least are mandatory
to create the archive of your work that you will upload on the server. You can create this archive
by yourself (for instance using the tar command), but if there is any kind of problem when we
try to uncompress it, your grade may be affected with no protestation possible (the most stupid
13
problem would be that your archive doesnt have the correct name which leads to ... zero for the
whole lab evaluation) .
Otherwise, you can use this tool to get an auto-evaluation of your work. Now that your archive
is created, you can notice that a new command appeared, evaluate archive already generated.
Type b.
Some information are displayed on the screen, saying that the auto-evaluation worked through
well (or not). The most interesting information for you are the ones on the next screen-shot.
14
This way you can have an estimation of your grade according to the rules detailed in section
8.2. However keep in mind that the grade estimated using this tool may not be your final grade
(see section 8.1). Note that you can use this command only if you have generated an archive first.
You could see that in the previous example, one of the results given by program ex1 is
incorrect. To display the difference between your results and the expected one, select the new
command display difference from solution of last generated archive by typing c.
15
Stop the auto-evaluation program
16
blablabla
please enter nu :
0.28
blabla
please enter E :
2500.
LAMBDA : 1242.8977
blabla
MU : 976.5625
blabla
Notice in this example that is asked before E as mandatory by Input directive. And and
appear in undefined order but with mandatory format (i.e token LAMBDA and MU).
Very important. You should avoid at any cost the use of token for anything else than the
result it corresponds to. Consequences may result in a zero grade just because you messed up the
tools with false information.
In example above if in any blabla you write MU or LAMBDA you will get a zero for associated
results.
Last but not least when you enter information and output first a question message
you must always add a new line to your question if it precedes a result. Lets take the
example of Lame above which outputs token LAMBDA right after asking to input E. If you
dont follow this last recommendation you may ask for E without inserting a new line after the
question as you can see in the example below:
Your executable is Lame and on terminal you type:
> ./Lame
And obtain the following bunch of outputs:
blabla
please enter E : 2500.
LAMBDA : 1242.8977
blabla
This works well on the terminal by typing on the keyboard 2500. and enter. But auto eval.bash
works differently. It uses what is called redirection mechanisms. And in fact this leads
auto eval.bash to miss recognize your results.
In this example you must add a end of line after the question to obtain:
> ./Lame
blabla
please enter E :
2500.
LAMBDA : 1242.8977
blabla
8.5 Useful
During Labs you will have questions where testing program asks for many inputs. During debugging
stage you may then be obliged to re enter those inputs manually many times. That may be error
prone and time consuming. To simplify your work use redirection.
For example lets take a prg program which runs the following:
> prg
Enter number :
23
17
Enter number :
13
Enter number :
-1
Mean value :
18
Here for illustration we just limit inputs to three entries. Put all those inputs in a file, with one
input per line. Lets call this file ip.txt. Its contents for this example will be:
23
13
-1
Now if you type the following command you get the same execution of prg without typing
anything:
> prg < ip.txt
Enter number :
Enter number :
Enter number :
Mean value :
18
Note that inputs just vanish from terminal display.
18