From aa62f7fe24545f99eae9941825365f72881cef20 Mon Sep 17 00:00:00 2001 From: Tor Morten Finnesand Date: Tue, 26 Aug 2014 12:52:21 +0200 Subject: [PATCH 01/13] Lab1 WIP --- ANSWERS.md | 57 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 9e5261a..783e725 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 backups"* ###Exercise 1b @@ -33,25 +33,32 @@ Use the commands `cd`, `ls` and `pwd` to explore the file system. 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* ###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 +orange +plum +mango +grapefruit +^D + +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 @@ -64,54 +71,54 @@ Use `ls -l` to check that the permissions have changed. ##Shell questions 1. What option with the command `rm` is required to remove a directory? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *"rm -r [directory_name]" will remove a directory* 1. What is the command used to display the manual pages for any command? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *the command "man [progam]" will show the manual page for your desired program* 1. What command will show the first 5 lines of an input file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *"head -n 5 [filename]" will show the first 5 lines of a specified file* 1. What command can be used to rename a file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *You may use the command "mv [file] [new_name]" to rename a file* 1. What option can we given to `ls` to show the hidden files? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *"ls -a"* 1. What will the command `cat -n file` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *The command will number the lines that are printed* 1. What will the command `echo -n hello` do? - **Answer:** *YOUR ANSWER HERE* 1. What command will display s list of the users who currently logged in in the system? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *the command "w" will list all users logged in to the system* 1. How do you change password on your account? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *You can change the password using the command "passwd [username]"* 1. How can you list a file in reverse order? - **Answer:** *YOUR ANSWER HERE* 1. What does the `less` command do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *the command "less" prints a file one page at a time* 1. With `less` how do you navigate? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *You can navigate "less" using the pageUp and pageDown keys. Also pressing space will take you to the next page.* 1. What command will display the running processes of the current user? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *the command "top -u [username]" will display the processes for the user* 1. What command can be used to find the process(es) consuming the most CPU? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Running the program "top" with no parameters will print the running processes with the most intensive on the top of the list* ##vi questions 1. How do we save a file in `vi` and continue working? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *You can save a file and continue working by entering :w [filename] in the command mode* 1. What command/key is used to start entering text? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Pressing the "i" button * 1. What are the different modes the editor can be in? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *The editor includes two modes. "Command" and "Input mode"* 1. What command can be used to place the cursor at the beginning of line 4? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Typing :4 in command mode will take your cursor to line 4* 1. What will `dd` command do (in command-mode)? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Typing "dd" in command mode will delete the line your cursor hovers over* 1. How do you undo the most recent changes? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *You can undo your recent by typing "u" in Command Mode* 1. How do you move back one word? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Pressing "b" in Command Mode will take you back one word at a time* ##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:** *By typing "gcc -c [source.c]" you will get only the ".o" file. This way gcc will only compile your source and not link it to create an executable.* 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* 1. Give a brief pros and cons discussion for the two methods to add debug code shown in Listing 2 and Listing 3. From cdca8b572067b90a3e399c467d81a0658264fdaf Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Wed, 27 Aug 2014 15:40:42 +0200 Subject: [PATCH 02/13] Work in progress --- ANSWERS.md | 78 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 783e725..ffe1c07 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:** *"mkdir backups"* +**Answer:** *`mkdir backups`* ###Exercise 1b @@ -27,38 +27,39 @@ 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:** *`cd` changes directory `ls` lists the directory structure `pwd` show you current working directory* ###Exercise 2a Create a backup of your `science.txt` file by copying it to a file called `science.bak` -**Answer:** *cp science.txt science.bak* +**Answer:** *`cp science.txt science.bak`* ###Exercise 2b Create a directory called `tempstuff` using `mkdir`, then remove it using the `rmdir` command. -**Answer:** *mkdir tempstuff. rmdir tempstuff/* +**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:** *cat > list2 +**Answer:** * +`cat > list2 orange plum mango grapefruit ^D -cat list2* +cat list2`* ###Exercise 3b Using pipes, display all lines of `list1` and `list2` containing the letter 'p', and sort the result. -**Answer:** *cat list1 list2 | grep p | sort* +**Answer:** *`cat list1 list2 | grep p | sort`* ###Exercise 5a @@ -66,65 +67,65 @@ 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 -w science.txt` remove write access.`chmod +w science.txt` enabled write access. `chmod -rw backups/` made the Os print out a warning when deleting the directory.* + ##Shell questions 1. What option with the command `rm` is required to remove a directory? - - **Answer:** *"rm -r [directory_name]" will remove a directory* + - **Answer:** *`rm -r [directory_name]` will remove a directory* 1. What is the command used to display the manual pages for any command? - - **Answer:** *the command "man [progam]" will show the manual page for your desired program* + - **Answer:** *the command `man [progam]` will show the manual page for your desired program* 1. What command will show the first 5 lines of an input file? - - **Answer:** *"head -n 5 [filename]" will show the first 5 lines of a specified file* + - **Answer:** *`head -n 5 [filename]` will show the first 5 lines of a specified file* 1. What command can be used to rename a file? - - **Answer:** *You may use the command "mv [file] [new_name]" to rename a file* + - **Answer:** *You may use the command `mv [file] [new_name]` to rename a file* 1. What option can we given to `ls` to show the hidden files? - - **Answer:** *"ls -a"* + - **Answer:** *`ls -a`* 1. What will the command `cat -n file` do? - **Answer:** *The command will number the lines that are printed* 1. What will the command `echo -n hello` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *`echo -n hello` will print hello to the screen but make the cursor stay on the same line.* 1. What command will display s list of the users who currently logged in in the system? - - **Answer:** *the command "w" will list all users logged in to the system* + - **Answer:** *the command `who` will list all users logged in to the system* 1. How do you change password on your account? - - **Answer:** *You can change the password using the command "passwd [username]"* + - **Answer:** *You can change the password using the command `passwd [username]`* 1. How can you list a file in reverse order? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *To list a file in reverse order you may use the `tac` command which is also `cat` spelled backwards* 1. What does the `less` command do? - - **Answer:** *the command "less" prints a file one page at a time* + - **Answer:** *the command `less` prints a file one page at a time* 1. With `less` how do you navigate? - - **Answer:** *You can navigate "less" using the pageUp and pageDown keys. Also pressing space will take you to the next page.* + - **Answer:** *You can navigate `less` using the pageUp and pageDown keys. Also pressing space will take you to the next page.* 1. What command will display the running processes of the current user? - - **Answer:** *the command "top -u [username]" will display the processes for the user* + - **Answer:** *the command `top -u [username]` will display the processes for the user* 1. What command can be used to find the process(es) consuming the most CPU? - - **Answer:** *Running the program "top" with no parameters will print the running processes with the most intensive on the top of the list* + - **Answer:** *Running the program `top` with no parameters will print the running processes with the most intensive on the top of the list* ##vi questions 1. How do we save a file in `vi` and continue working? - - **Answer:** *You can save a file and continue working by entering :w [filename] in the command mode* + - **Answer:** *You can save a file and continue working by entering `:w [filename]` in the command mode* 1. What command/key is used to start entering text? - - **Answer:** *Pressing the "i" button * + - **Answer:** *Pressing the `i` button * 1. What are the different modes the editor can be in? - - **Answer:** *The editor includes two modes. "Command" and "Input mode"* + - **Answer:** *The editor includes two modes. `Command` and `Input Mode`* 1. What command can be used to place the cursor at the beginning of line 4? - - **Answer:** *Typing :4 in command mode will take your cursor to line 4* + - **Answer:** *Typing `:4` in command mode will take your cursor to line 4* 1. What will `dd` command do (in command-mode)? - - **Answer:** *Typing "dd" in command mode will delete the line your cursor hovers over* + - **Answer:** *Typing `dd` in command mode will delete the line your cursor hovers over* 1. How do you undo the most recent changes? - - **Answer:** *You can undo your recent by typing "u" in Command Mode* + - **Answer:** *You can undo your recent by typing `u` in Command Mode* 1. How do you move back one word? - - **Answer:** *Pressing "b" in Command Mode will take you back one word at a time* + - **Answer:** *Pressing `b` in Command Mode will take you back one word at a time* ##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:** *By typing "gcc -c [source.c]" you will get only the ".o" file. This way gcc will only compile your source and not link it to create an executable.* + - **Answer:** *By typing `gcc -c [source.c]` you will get only the `.o` file. This way gcc will only compile your source and not link it to create an executable.* 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:** *To compile you program with debug enabled in case 1: use gcc with the flag `- DDEBUG`. In case two: * 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* 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:** *In order to generate a map file use the command `gcc -o [executable] -Wl,-Map,[desired.map] [source.c]` `ld` the linker in gcc is responsible for producing the map file* 1. What is the content of each of the sections in a *map* file. Explain briefly. - **Answer:** *YOUR ANSWER HERE* 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. @@ -132,11 +133,18 @@ Use `ls -l` to check that the permissions have changed. 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* 1. How does `make` know if a file must be recompiled? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *Make checks the timestamp of the executable. If it is older than the source file required to make it, one can asume it is outdated and needs to be recompiled in order to update the executable.* 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:** *And include guard is needed so that headers are not included more than once. If they are you will end up defining functions multiple times and you program may not compile. An include header is added by using the preprocessor: + `#ifndef header.h + #define header.h + + ..code contents.. + + #endif + `* ##Library Task From 43bee091d00fbd055224258a77599fc6c91cdce4 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Thu, 28 Aug 2014 12:13:36 +0200 Subject: [PATCH 03/13] Added libtask files --- src/l1.c | 43 ++++++++++++++++++++++++++++++++++++++ src/l1.h | 6 ++++++ src/main.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/l1.c create mode 100644 src/l1.h create mode 100644 src/main.c diff --git a/src/l1.c b/src/l1.c new file mode 100644 index 0000000..3c1b02b --- /dev/null +++ b/src/l1.c @@ -0,0 +1,43 @@ +#include +#include "l1.h" +#include + +double tab_sort_sum(double *tab, int tab_size){ + // A version of bubble sort + + int swapped = 1; + int idx; + int big, small; + double *table = tab; + double sum; + while(swapped == 0){ + for(idx = 1;idx < tab_size;idx ++){ + if(table[idx -1] > table[idx]){ + big = table[idx - 1]; + small = table[idx]; + table[idx] = big; + table[idx-1] = small; + swapped = 1; + }else{ + swapped = 0; + } + } +} + + for(idx = 0; idx < tab_size; idx ++){ + sum += table[idx]; + } + for(idx = 0; idx < tab_size; idx ++){ + printf("%f ", table[idx]); + } + + return sum; +} + + + + + + + + diff --git a/src/l1.h b/src/l1.h new file mode 100644 index 0000000..2d98f5b --- /dev/null +++ b/src/l1.h @@ -0,0 +1,6 @@ +#ifndef L1_H_ +#define L1_H_ + +double tab_sort_sum(double *tab, int tab_size); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..91b6f70 --- /dev/null +++ b/src/main.c @@ -0,0 +1,60 @@ +//Make a table of size give by an argument on the command line. +//Fill the table with random numbers between MIN and MAX +//MIN = 0.0 if not specified with another value on command line, i.e. optional argument +//MAX = 100.0 if not specified with another value on command line, i.e. optional argument +//Call tab_sort_sum() in lib1 +//Print out table and sum + +#include +#include +#include +#include "l1.h" + +double randFloat(double min, double max); + +double randFloat(double min, double max){ + //srand(time(NULL)); + double range = (max - min); + double div = (RAND_MAX / range); + return min + (rand() / div); +} +int main(int argc, char *argv[]) +{ + int tableSize; + + double min = 0.0; //Default Min value + double max = 100.0; //Default Max value + + if(atoi(argv[1])){ + tableSize = atoi(argv[1]); + }else{ + printf("Error: You need to specify size yo\n"); + return -1; + } + double table[tableSize]; + if(atof(argv[2])){ + min = atof(argv[2]); + } + if(atof(argv[3])){ + max = atof(argv[3]); + } + srand(time(NULL)); + int i; + for(i = 0; i < tableSize;i++){ + //table[i] = min + rand() % max; + table[i] = randFloat(min,max); + } + printf("Here is our table:\n"); + for(i = 0; i < sizeof(table)/sizeof(double);i++){ + printf("%f, ",table[i]); + } + + printf("Sorted table: \n \n"); + + printf("Sum of table: %f\n",tab_sort_sum(table, sizeof(table)/sizeof(double))); + + + return 0; + +} + From 9d1f92dc3f612dd792128643e24bd8727c0cabd5 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Thu, 28 Aug 2014 14:16:42 +0200 Subject: [PATCH 04/13] Program now works --- src/l1.c | 59 +++++++++++++++++++++------------------ src/main.c | 81 ++++++++++++++++++++++++++++++------------------------ 2 files changed, 77 insertions(+), 63 deletions(-) diff --git a/src/l1.c b/src/l1.c index 3c1b02b..212953b 100644 --- a/src/l1.c +++ b/src/l1.c @@ -1,37 +1,42 @@ #include -#include "l1.h" #include double tab_sort_sum(double *tab, int tab_size){ - // A version of bubble sort - - int swapped = 1; - int idx; - int big, small; - double *table = tab; - double sum; - while(swapped == 0){ - for(idx = 1;idx < tab_size;idx ++){ - if(table[idx -1] > table[idx]){ - big = table[idx - 1]; - small = table[idx]; - table[idx] = big; - table[idx-1] = small; - swapped = 1; - }else{ - swapped = 0; - } - } -} - for(idx = 0; idx < tab_size; idx ++){ - sum += table[idx]; - } - for(idx = 0; idx < tab_size; idx ++){ - printf("%f ", table[idx]); +/* Bubble sort code */ + + int i, j; + double sum, swap; + double array[tab_size]; + for(i = 0; i < tab_size; i++){ + array[i] = tab[i]; + } + + int array_size = tab_size; + + for (i = 0 ; i < ( (array_size - 1 )); i++) + { + for (j = 0 ; j < array_size - i - 1; j++) + { + if (array[j] > array[j+1]) /* For decreasing order use < */ + { + swap = array[j]; + array[j] = array[j+1]; + array[j+1] = swap; + } } + } + + //Create the sum + for(i = 0; i < tab_size; i++){ + sum += array[i]; + } + // Print the Table + for(i = 0; i < tab_size;i++){ + printf("%f, ", array[i]); + } +return sum; - return sum; } diff --git a/src/main.c b/src/main.c index 91b6f70..95c6788 100644 --- a/src/main.c +++ b/src/main.c @@ -10,51 +10,60 @@ #include #include "l1.h" -double randFloat(double min, double max); +void sclk(){ + srand(time(NULL)); +} -double randFloat(double min, double max){ - //srand(time(NULL)); +double randDouble(double min, double max){ double range = (max - min); double div = (RAND_MAX / range); return min + (rand() / div); } + int main(int argc, char *argv[]) { - int tableSize; - - double min = 0.0; //Default Min value - double max = 100.0; //Default Max value - - if(atoi(argv[1])){ - tableSize = atoi(argv[1]); - }else{ - printf("Error: You need to specify size yo\n"); - return -1; - } - double table[tableSize]; - if(atof(argv[2])){ - min = atof(argv[2]); - } - if(atof(argv[3])){ - max = atof(argv[3]); - } - srand(time(NULL)); - int i; - for(i = 0; i < tableSize;i++){ - //table[i] = min + rand() % max; - table[i] = randFloat(min,max); - } - printf("Here is our table:\n"); - for(i = 0; i < sizeof(table)/sizeof(double);i++){ - printf("%f, ",table[i]); - } - - printf("Sorted table: \n \n"); - - printf("Sum of table: %f\n",tab_sort_sum(table, sizeof(table)/sizeof(double))); + int tableSize; + + //Default Values + double min = 0; + double max = 100; + if(atoi(argv[1])){ + tableSize = atoi(argv[1]); + }else{ + printf("Error: You need to specify size yo\n"); + return -1; + } + + double table[tableSize]; + + if(argv[2] != NULL){ + min = atof(argv[2]); + } + if(argv[3] != NULL){ + max = atof(argv[3]); + } + + + sclk(); // Seeds the timer + int i; + for(i = 0; i < tableSize;i++){ + //table[i] = min + rand() % max; + table[i] = randDouble(min,max); + } + + printf("\nHere is our table:\n"); + for(i = 0; i < sizeof(table)/sizeof(double);i++){ + printf("%f, ",table[i]); + } + + printf("\n\nSorted table:\n"); + + double sum = tab_sort_sum(table,tableSize); - return 0; + printf("\nSum of table: %f\n\n", sum); + +return 0; } From 9e65aee2eed47ad2a5cff5d5301b69b0b8b6f755 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 12:39:15 +0200 Subject: [PATCH 05/13] Final Code --- src/l1.c | 2 +- src/main.c | 31 ++++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/l1.c b/src/l1.c index 212953b..c3322cb 100644 --- a/src/l1.c +++ b/src/l1.c @@ -33,7 +33,7 @@ double tab_sort_sum(double *tab, int tab_size){ } // Print the Table for(i = 0; i < tab_size;i++){ - printf("%f, ", array[i]); + printf("%.2f, ", array[i]); } return sum; diff --git a/src/main.c b/src/main.c index 95c6788..5586691 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,3 @@ -//Make a table of size give by an argument on the command line. -//Fill the table with random numbers between MIN and MAX -//MIN = 0.0 if not specified with another value on command line, i.e. optional argument -//MAX = 100.0 if not specified with another value on command line, i.e. optional argument -//Call tab_sort_sum() in lib1 -//Print out table and sum - #include #include #include @@ -23,25 +16,29 @@ double randDouble(double min, double max){ int main(int argc, char *argv[]) { int tableSize; - //Default Values double min = 0; double max = 100; - if(atoi(argv[1])){ - tableSize = atoi(argv[1]); - }else{ - printf("Error: You need to specify size yo\n"); + if(argc == 1){ + printf("Error: You need to specify Array Size\n"); + printf("Usage: main [size] [min] [max]\n"); return -1; + }else{ + tableSize = atoi(argv[1]); } double table[tableSize]; - - if(argv[2] != NULL){ + if(argc >= 3){ min = atof(argv[2]); + printf("HELLOOOAOODOWDOAOWDE921823912316212616926912639"); + }else{ + min = 0.0; } - if(argv[3] != NULL){ + if(argc >= 4){ max = atof(argv[3]); + }else{ + max = 100.0; } @@ -54,14 +51,14 @@ int main(int argc, char *argv[]) printf("\nHere is our table:\n"); for(i = 0; i < sizeof(table)/sizeof(double);i++){ - printf("%f, ",table[i]); + printf("%.2f, ",table[i]); } printf("\n\nSorted table:\n"); double sum = tab_sort_sum(table,tableSize); - printf("\nSum of table: %f\n\n", sum); + printf("\n\n**Sum of table: %.2f**\n\n", sum); return 0; From c8d83a3b87e873eef13d4e57c2b826b464268eaf Mon Sep 17 00:00:00 2001 From: Tor Morten Finnesand Date: Fri, 29 Aug 2014 13:18:42 +0200 Subject: [PATCH 06/13] Added code to answer --- ANSWERS.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/l1.c | 2 -- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index ffe1c07..6e2690f 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -152,10 +152,90 @@ Insert your code between the brackets `{}`: void main( int argc, char *argv[] ) { + int tableSize; + + double min; + double max; + + if(argc == 1){ + printf("Error: You need to specify Array Size\n"); + printf("Usage: main [size] [min] [max]\n"); + return -1; + }else{ + tableSize = atoi(argv[1]); + } + + double table[tableSize]; + + if(argc >= 3){ + min = atof(argv[2]); + }else{ + min = 0.0; + } + if(argc >= 4){ + max = atof(argv[3]); + }else{ + max = 100.0; + } + + + sclk(); // Seeds the timer + int i; + for(i = 0; i < tableSize;i++){ + //table[i] = min + rand() % max; + table[i] = randDouble(min,max); + } + + printf("\nHere is our table:\n"); + for(i = 0; i < sizeof(table)/sizeof(double);i++){ + printf("%.2f, ",table[i]); + } + + printf("\n\nSorted table:\n"); + + double sum = tab_sort_sum(table,tableSize); + + printf("\n\n**Sum of table: %.2f**\n\n", sum); + + return 0; + } double tab_sort_sum( double *tab, int tab_size ) { - } + int i, j; + double sum, swap; + double array[tab_size]; + + for(i = 0; i < tab_size; i++){ + array[i] = tab[i]; + } + + int array_size = tab_size; + + for (i = 0 ; i < ( (array_size - 1 )); i++) + { + for (j = 0 ; j < array_size - i - 1; j++) + { + if (array[j] > array[j+1]) /* For decreasing order use < */ + { + swap = array[j]; + array[j] = array[j+1]; + array[j+1] = swap; + } + } + } + + //Create the sum + for(i = 0; i < tab_size; i++){ + sum += array[i]; + } + // Print the Table + for(i = 0; i < tab_size;i++){ + printf("%.2f, ", array[i]); + } + return sum; + + } diff --git a/src/l1.c b/src/l1.c index c3322cb..1393454 100644 --- a/src/l1.c +++ b/src/l1.c @@ -3,8 +3,6 @@ double tab_sort_sum(double *tab, int tab_size){ -/* Bubble sort code */ - int i, j; double sum, swap; double array[tab_size]; From d8ce86b0db2e36737afb98d3eb583b7357cd4ce7 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 14:58:22 +0200 Subject: [PATCH 07/13] Updated ANSWERS.md and created hello.c --- ANSWERS.md | 75 +++++++++++++++++++++++++++++++++++++++++------------ src/hello.c | 19 ++++++++++++++ src/main.c | 7 +++-- 3 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 src/hello.c diff --git a/ANSWERS.md b/ANSWERS.md index 6e2690f..484ccfe 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -46,14 +46,14 @@ Create a directory called `tempstuff` using `mkdir`, then remove it using the `r Using the above method, create another file called `list2` containing the following fruit: orange, plum, mango, grapefruit. Read the contents of `list2`. **Answer:** * -`cat > list2 -orange -plum -mango -grapefruit -^D + `cat > list2 + orange + plum + mango + grapefruit + ^D -cat list2`* + cat list2`* ###Exercise 3b @@ -123,28 +123,71 @@ Use `ls -l` to check that the permissions have changed. 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:** *To compile you program with debug enabled in case 1: use gcc with the flag `- DDEBUG`. In case two: * 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:**In Listing 2 debugging is activated by compiling with the macro `DEBUG`. In my opinion it seems like a simple and convenient method to activate debugging, mainly because it does not require you to modify the source code. In Listing 3 debugging is enabled by setting a variable. In this case you will have to modify the c file(s) to enable debugging which may become difficult as you have to trace the appropriate variables. In my opinion it also makes the code harder to read because on first sight it may seem like a "normal" part of the code.** 1. Provide the command for generating the *map* file. Which of the `gcc` tools is responsible for producing a *map* file? - **Answer:** *In order to generate a map file use the command `gcc -o [executable] -Wl,-Map,[desired.map] [source.c]` `ld` the linker in gcc is responsible for producing the map file* 1. What is the content of each of the sections in a *map* file. Explain briefly. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** *The .map file include a list over program entrypoints and which libraries/object files the linker uses.* 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:** * + ' + #include + + float multiply(float x1, float x2){ + return x1*x2; + } + + int main(void){ + int integer; + char character; + short shortvar; + float floating; + + printf("Hello, World!\n"); + + printf("%.3f\n", multiply(3.2,4.0)); + + return 0; + + } + ' + * 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:** * + multiply: + .LFB0: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + movsd %xmm0, -8(%rbp) + movsd %xmm1, -16(%rbp) + movsd -8(%rbp), %xmm0 + mulsd -16(%rbp), %xmm0 + popq %rbp + .cfi_def_cfa 7, 8 + ret + .cfi_endproc + + The `mulsd` Instruction performing the multiplication. When using the float type, it uses the `mulss` insturction. + + * 1. How does `make` know if a file must be recompiled? - **Answer:** *Make checks the timestamp of the executable. If it is older than the source file required to make it, one can asume it is outdated and needs to be recompiled in order to update the executable.* 1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`. - **Answer:** *`make -f mymakefile`* 1. How do you implement an *include guard*, and why is it needed? - **Answer:** *And include guard is needed so that headers are not included more than once. If they are you will end up defining functions multiple times and you program may not compile. An include header is added by using the preprocessor: - `#ifndef header.h - #define header.h + ` + #ifndef header.h + #define header.h - ..code contents.. + ..code contents.. - #endif - `* + #endif + `* ##Library Task diff --git a/src/hello.c b/src/hello.c new file mode 100644 index 0000000..cf71b76 --- /dev/null +++ b/src/hello.c @@ -0,0 +1,19 @@ +#include + +float multiply(float x1, float x2){ + return x1*x2; +} + +int main(void){ + int integer; + char character; + short shortvar; + float floating; + + printf("Hello, World!\n"); + + printf("%.3f\n", multiply(3.2,4.0)); + +return 0; + +} diff --git a/src/main.c b/src/main.c index 5586691..ac59566 100644 --- a/src/main.c +++ b/src/main.c @@ -17,8 +17,8 @@ int main(int argc, char *argv[]) { int tableSize; //Default Values - double min = 0; - double max = 100; + double min; + double max; if(argc == 1){ printf("Error: You need to specify Array Size\n"); @@ -31,7 +31,7 @@ int main(int argc, char *argv[]) double table[tableSize]; if(argc >= 3){ min = atof(argv[2]); - printf("HELLOOOAOODOWDOAOWDE921823912316212616926912639"); + }else{ min = 0.0; } @@ -45,7 +45,6 @@ int main(int argc, char *argv[]) sclk(); // Seeds the timer int i; for(i = 0; i < tableSize;i++){ - //table[i] = min + rand() % max; table[i] = randDouble(min,max); } From 086f9820463d55683718e15fbe475e7bb0b68b92 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 15:05:03 +0200 Subject: [PATCH 08/13] Updated ANSERS.md --- ANSWERS.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 484ccfe..aed4623 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -129,8 +129,8 @@ Use `ls -l` to check that the permissions have changed. 1. What is the content of each of the sections in a *map* file. Explain briefly. - **Answer:** *The .map file include a list over program entrypoints and which libraries/object files the linker uses.* 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:** * - ' + - **Answer:** + #include float multiply(float x1, float x2){ @@ -150,10 +150,11 @@ Use `ls -l` to check that the permissions have changed. return 0; } - ' - * + + 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:** * + multiply: .LFB0: .cfi_startproc From 97ea56ee90e6358289dfc9b88dcfcbf01658ad19 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 15:08:23 +0200 Subject: [PATCH 09/13] Trying to get paring right --- ANSWERS.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index aed4623..b141e63 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -45,7 +45,7 @@ Create a directory called `tempstuff` using `mkdir`, then remove it using the `r Using the above method, create another file called `list2` containing the following fruit: orange, plum, mango, grapefruit. Read the contents of `list2`. -**Answer:** * +**Answer:** ** `cat > list2 orange plum @@ -53,7 +53,7 @@ Using the above method, create another file called `list2` containing the follow grapefruit ^D - cat list2`* + cat list2` ###Exercise 3b @@ -129,7 +129,7 @@ Use `ls -l` to check that the permissions have changed. 1. What is the content of each of the sections in a *map* file. Explain briefly. - **Answer:** *The .map file include a list over program entrypoints and which libraries/object files the linker uses.* 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:** + - **Answer:** ** #include @@ -151,7 +151,6 @@ Use `ls -l` to check that the permissions have changed. } - 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:** * From 262fcae295295700cf264ade3c7766bc3ddc19f7 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 15:23:36 +0200 Subject: [PATCH 10/13] Update --- ANSWERS.md | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index b141e63..140d979 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -129,27 +129,26 @@ Use `ls -l` to check that the permissions have changed. 1. What is the content of each of the sections in a *map* file. Explain briefly. - **Answer:** *The .map file include a list over program entrypoints and which libraries/object files the linker uses.* 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:** ** + - **Answer:** * - #include - - float multiply(float x1, float x2){ - return x1*x2; - } + float multiply(float x1, float x2){ + return x1*x2; + } - int main(void){ - int integer; - char character; - short shortvar; - float floating; + int main(void){ + int integer; + char character; + short shortvar; + float floating; - printf("Hello, World!\n"); + printf("Hello, World!\n"); - printf("%.3f\n", multiply(3.2,4.0)); + printf("%.3f\n", multiply(3.2,4.0)); - return 0; + return 0; - } + } +* 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:** * From 7882266c74bdd86bb114044e013c1ced47b9d4d1 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 15:32:31 +0200 Subject: [PATCH 11/13] More parsing --- ANSWERS.md | 65 +++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 140d979..c6f3ba0 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:** *`mkdir backups`* +**Answer:** *`cd unixstuff` `mkdir backups`* ###Exercise 1b @@ -45,16 +45,17 @@ Create a directory called `tempstuff` using `mkdir`, then remove it using the `r Using the above method, create another file called `list2` containing the following fruit: orange, plum, mango, grapefruit. Read the contents of `list2`. -**Answer:** ** - `cat > list2 - orange - plum - mango - grapefruit - ^D - - cat list2` +**Answer:** * + + `cat > list2 + orange + plum + mango + grapefruit + ^D + cat list2` +* ###Exercise 3b Using pipes, display all lines of `list1` and `list2` containing the letter 'p', and sort the result. @@ -132,13 +133,17 @@ Use `ls -l` to check that the permissions have changed. - **Answer:** * float multiply(float x1, float x2){ - return x1*x2; + + return x1*x2; + } int main(void){ - int integer; + + int integer; char character; short shortvar; + float floating; printf("Hello, World!\n"); @@ -153,26 +158,26 @@ Use `ls -l` to check that the permissions have changed. 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:** * - multiply: - .LFB0: - .cfi_startproc - pushq %rbp - .cfi_def_cfa_offset 16 - .cfi_offset 6, -16 - movq %rsp, %rbp - .cfi_def_cfa_register 6 - movsd %xmm0, -8(%rbp) - movsd %xmm1, -16(%rbp) - movsd -8(%rbp), %xmm0 - mulsd -16(%rbp), %xmm0 - popq %rbp - .cfi_def_cfa 7, 8 - ret - .cfi_endproc + multiply: + .LFB0: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + movsd %xmm0, -8(%rbp) + movsd %xmm1, -16(%rbp) + movsd -8(%rbp), %xmm0 + mulsd -16(%rbp), %xmm0 + popq %rbp + .cfi_def_cfa 7, 8 + ret + .cfi_endproc - The `mulsd` Instruction performing the multiplication. When using the float type, it uses the `mulss` insturction. +The `mulsd` Instruction performing the multiplication. When using the float type, it uses the `mulss` insturction. - * +* 1. How does `make` know if a file must be recompiled? - **Answer:** *Make checks the timestamp of the executable. If it is older than the source file required to make it, one can asume it is outdated and needs to be recompiled in order to update the executable.* 1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`. From b0167c3e574e13053727ac98fffd67ead72128c6 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 15:39:04 +0200 Subject: [PATCH 12/13] More parsing of ANSWERS.md --- ANSWERS.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index c6f3ba0..ec08187 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -157,7 +157,7 @@ Use `ls -l` to check that the permissions have changed. 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:** * - + ``` multiply: .LFB0: .cfi_startproc @@ -174,24 +174,26 @@ Use `ls -l` to check that the permissions have changed. .cfi_def_cfa 7, 8 ret .cfi_endproc - + ``` The `mulsd` Instruction performing the multiplication. When using the float type, it uses the `mulss` insturction. * + 1. How does `make` know if a file must be recompiled? - **Answer:** *Make checks the timestamp of the executable. If it is older than the source file required to make it, one can asume it is outdated and needs to be recompiled in order to update the executable.* 1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`. - **Answer:** *`make -f mymakefile`* 1. How do you implement an *include guard*, and why is it needed? - **Answer:** *And include guard is needed so that headers are not included more than once. If they are you will end up defining functions multiple times and you program may not compile. An include header is added by using the preprocessor: - ` + ``` #ifndef header.h #define header.h ..code contents.. #endif - `* + ``` +* ##Library Task From 6d16443c4d2036ca67fc15d79446de6a4a8a61c7 Mon Sep 17 00:00:00 2001 From: TorMFinn Date: Fri, 29 Aug 2014 22:23:48 +0200 Subject: [PATCH 13/13] Added Makefile --- src/Makefile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/Makefile diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..32dcbf3 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,20 @@ +#Makefile +#v 1.0 + +all: main hello + +hello: hello.o + gcc hello.o -o hello +hello.o: hello.c + gcc -c hello.c +main: main.o l1.o + gcc main.o l1.o -o main +main.o: main.c + gcc -c main.c +l1.o: l1.c + gcc -c l1.c +clean: + rm -rf *o main hello +test: main hello main.o l1.o hello.c + ./main 10 1 10 + ./hello