From 42137ce8c4397fb5e404db09e48861afe244c115 Mon Sep 17 00:00:00 2001 From: Tobzor Date: Mon, 1 Sep 2014 10:22:39 +0200 Subject: [PATCH 1/2] mostly done --- ANSWERS.md | 109 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 43 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 9e5261a..bceb197 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -19,7 +19,7 @@ Please use [markdown](https://help.github.com/articles/markdown-basics) formatin Make another directory inside the `unixstuff` directory called `backups` -**Answer:** *YOUR ANSWER HERE* +**Answer:** *mkdir unixstuff, mkdir backups* ###Exercise 1b @@ -27,31 +27,31 @@ Use the commands `cd`, `ls` and `pwd` to explore the file system. (Remember, if you get lost, type `cd` by itself to return to your home-directory) -**Answer:** *YOUR ANSWER HERE* +**Answer:** *pwd == print working directory. cd == change directory. ls == list* ###Exercise 2a Create a backup of your `science.txt` file by copying it to a file called `science.bak` -**Answer:** *YOUR ANSWER HERE* +**Answer:** *cp science.txt science.bak, mv science.bak unixstuff/backups/* ###Exercise 2b Create a directory called `tempstuff` using `mkdir`, then remove it using the `rmdir` command. -**Answer:** *YOUR ANSWER HERE* +**Answer:** *mkdir tempstuff, rmdir tempstuff* ###Exercise 3a Using the above method, create another file called `list2` containing the following fruit: orange, plum, mango, grapefruit. Read the contents of `list2`. -**Answer:** *YOUR ANSWER HERE* +**Answer:** *cat > list2* ###Exercise 3b Using pipes, display all lines of `list1` and `list2` containing the letter 'p', and sort the result. -**Answer:** *YOUR ANSWER HERE* +**Answer:** *cat list1 list2 | grep p | sort* ###Exercise 5a @@ -59,88 +59,111 @@ Try changing access permissions on the file `science.txt` and on the directory ` Use `ls -l` to check that the permissions have changed. -**Answer:** *YOUR ANSWER HERE* +**Answer:** *chmod a+rw science.txt, chmod go-rx backups* ##Shell questions 1. What option with the command `rm` is required to remove a directory? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *rmdir* 1. What is the command used to display the manual pages for any command? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *man command* 1. What command will show the first 5 lines of an input file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *head -5 filename* 1. What command can be used to rename a file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *mv filename newfilename* 1. What option can we given to `ls` to show the hidden files? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *ls -a (-a for all files)* 1. What will the command `cat -n file` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *output lines with the line numbers.* 1. What will the command `echo -n hello` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *display a line of text without a new line.* 1. What command will display s list of the users who currently logged in in the system? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *who* 1. How do you change password on your account? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *passwd* 1. How can you list a file in reverse order? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *tac ‘list’* 1. What does the `less` command do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *You can view the file(no need to load the entire file at once)with backward and forward moving possible.* 1. With `less` how do you navigate? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *enter for next line, space for new page, q for quit* 1. What command will display the running processes of the current user? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *ps* 1. What command can be used to find the process(es) consuming the most CPU? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *ps -aux | head -5* ##vi questions 1. How do we save a file in `vi` and continue working? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *:w filename* 1. What command/key is used to start entering text? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *i key* 1. What are the different modes the editor can be in? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *COMMAND and INSERT* 1. What command can be used to place the cursor at the beginning of line 4? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *:4* 1. What will `dd` command do (in command-mode)? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *dd deletes the whole line the cursor is on* 1. How do you undo the most recent changes? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *u* 1. How do you move back one word? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *b* ##The C Language and Make tool Questions 1. How do you use `gcc` to only produce the `.o` file? What is the difference between generating only the `.o` file, and building the `hello` executable done in the previous compilation above? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *gcc hello.c -c( -o hello.o), the first method doesn't link to system libraries.* 1. Give the command for compiling with `debug` enabled instead of normal compilation for the two examples shown in Listing 2 and Listing 3. Explain how to turn debugging on/off for the two cases. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *gcc -d DEBUG. The selectable (listing 3) must be turned on in the source code. * 1. Give a brief pros and cons discussion for the two methods to add debug code shown in Listing 2 and Listing 3. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Listing 2 will only compile the required debug code if the user has defined it while compiling. Listing 3 will have uneccessary code if it isn't used for debugging.* 1. Provide the command for generating the *map* file. Which of the `gcc` tools is responsible for producing a *map* file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *gcc hello.c -WL,-Map=hello.map hello.c. The linker produces the *map* file.* 1. What is the content of each of the sections in a *map* file. Explain briefly. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *.test contains the machine instructions for the program. .bss contains variables that is not initialized or is intilized as being null. + .data contains static read/write data that the codes create. + .rodata contains constant read only data. * 1. Rewrite `hello.c` to produce entries in the *map* file for `.data`, `.bss`, and `.rodata`. Hint: This can be done by adding one variable for each type to the file. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *int data = 30, int bss, static const int fin = 300* 1. Add the following function to `hello.c`: `double multiply(double x1, double x2)`, which returns `x1*x2`. Use `gcc` to generate an assembly code listing for the program, and examine the assembly code. What assembly instructions are used to do this? Repeat this task, but now replace `double` with `float`. Explain! - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *gcc -c -g -Wa,-a,-ad [other GCC options] foo.c > foo.lst (foo.lst C/assembly listing),With double the assembly instructions are: movsd/mulsd. With float the assembly instructions are: movss/mulss.* 1. How does `make` know if a file must be recompiled? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Using the timestamp of the source files.* 1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *make -f mymakefile* 1. How do you implement an *include guard*, and why is it needed? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *insert #ifndef … #define … #endif /* … */. It guards from compilation errors if a type is define twice.* ##Library Task Insert your code between the brackets `{}`: void main( int argc, char *argv[] ) - { +{ + int antall; + sscanf(argv[1], "%d", &antall); + double table[antall]; + int i; + for (i = 0; i < antall; i++) + { + table[i] = rand() % 100 + 1; + printf("%.2f\n", table[i]); } - - double tab_sort_sum( double *tab, int tab_size ) - { - } + double sum = tab_sort_sum(table, antall); + printf("Sum is %1f\n", sum); +} + +double tab_sort_sum( double *tab, int tab_size ) +{ + qsort(tab, tab_size, sizeof(double), comp); + double sum; + int i; + printf("sorted:\n"); + for (i = 0; i < tab_size; i++) + { + printf("%.2f\n", tab[i]); + sum += tab[i]; + } + return sum; +} From 1c63c4537ceee4bde0985ad3ac8a7e5bac874777 Mon Sep 17 00:00:00 2001 From: Tobzor Date: Mon, 1 Sep 2014 13:49:16 +0200 Subject: [PATCH 2/2] done --- ANSWERS.md | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index bceb197..058fe87 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -137,33 +137,26 @@ Use `ls -l` to check that the permissions have changed. Insert your code between the brackets `{}`: - void main( int argc, char *argv[] ) -{ - int antall; - sscanf(argv[1], "%d", &antall); - double table[antall]; - int i; - for (i = 0; i < antall; i++) - { - table[i] = rand() % 100 + 1; - printf("%.2f\n", table[i]); + void main( int argc, char *argv[] ){ + int i; + double tab[TAB_SIZE] = {4.3, 2.3, 6.4, 1.2, 1.3}; + double sum; + + sum = tab_sort_sum( tab, TAB_SIZE ); + + printf("sum=%f\n", sum); + for ( i=0; i