From 1b9022788cd9a7720af41d01e286d679c44d90bd Mon Sep 17 00:00:00 2001 From: Joakim Hagen Date: Tue, 26 Aug 2014 17:57:37 +0200 Subject: [PATCH 1/5] babbys furst commit --- ANSWERS.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 9e5261a..341dbee 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:** `~/unixstuff$ mkdir backups` ###Exercise 1b @@ -27,25 +27,30 @@ 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 backups` `pwd` `cd ..` `ls` ###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 -T sci[tab] 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 tem[tab]` ###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` ###Exercise 3b From 4c5fea985dbdd418899123ae3105c0de4648a474 Mon Sep 17 00:00:00 2001 From: Joakim Hagen <220474@uis.no> Date: Fri, 29 Aug 2014 01:13:52 +0200 Subject: [PATCH 2/5] almost there --- ANSWERS.md | 89 ++++++++++++++++++++++++++++++------------------------ dmult.c | 3 ++ dmult.s | 14 +++++++++ fmult.c | 3 ++ fmult.s | 14 +++++++++ 5 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 dmult.c create mode 100644 dmult.s create mode 100644 fmult.c create mode 100644 fmult.s diff --git a/ANSWERS.md b/ANSWERS.md index 341dbee..5487e35 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -45,18 +45,18 @@ 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` +**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:** `grep p list1 list2 | sort` ###Exercise 5a @@ -64,77 +64,86 @@ 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 go+rw science.txt` `chmod go-rwx backups` `ls -l` ##Shell questions 1. What option with the command `rm` is required to remove a directory? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `-r`, `-R` or `-recursive` 1. What is the command used to display the manual pages for any command? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `man ` or `help `, `whatis `, `info ` 1. What command will show the first 5 lines of an input file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `head -n 5 ` 1. What command can be used to rename a file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `mv ` or `rename ` 1. What option can we given to `ls` to show the hidden files? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `-a` 1. What will the command `cat -n file` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** Prints contents of file with enumerated lines. 1. What will the command `echo -n hello` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** Prints "hello" without automatically appending a newline char. 1. What command will display s list of the users who currently logged in in the system? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `who` or `users` (`users | sed 's/ /\n/g' | uniq`) 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 ` (linewise) 1. What does the `less` command do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** Lazily reads files and allows for backtracing. 1. With `less` how do you navigate? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** [space] for next page 1. What command will display the running processes of the current user? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `ps` `pstree` `top` `top??` 1. What command can be used to find the process(es) consuming the most CPU? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `top` ##vi questions 1. How do we save a file in `vi` and continue working? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `:w[Enter]` in normal-mode 1. What command/key is used to start entering text? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `i` 1. What are the different modes the editor can be in? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** normal / insert / visual / select / command-line / ex-mode 1. What command can be used to place the cursor at the beginning of line 4? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `4gg`, `4G` or `:4[Enter]` 1. What will `dd` command do (in command-mode)? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** dd is a linewise delete function 1. How do you undo the most recent changes? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** `u` `:undo[Enter]` 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 -c ` The object file is not linked 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:** Listing 2: `gcc -DDEBUG `, `-g` is similar but does not define anything. *(NDEBUG may however NOT be defined) + Listing 3: global var `debug` must be changed in source in order to toggle. 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 requires compile time config but is efficient. Listing 3 can potentially be toggled during runtime but may cause efficiency reduction in tight loops regardless of mode (if self modification is not allowed/implemented) 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:** `-Wl,-Map=mymap.map` (`-Wl,-Map,mymap.map` for non-GNU). Arguments are passed on as linker is not part of gcc 1. What is the content of each of the sections in a *map* file. Explain briefly. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** The map shows address resolutions, .data, .code, etc sections size and position, archive members included and allocated symbols and their assigned value. 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:** + const char debug_cmt[] = "Some smart debugcomment\n"; + const char hello[] = "Hello world!\n"; + const int retval = 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:** `gcc -O -S dmult.c` (took the liberty of optimizing) + Examining dmult.s & fmult.s: `mulsd %xmm1, %xmm0` scalar double as opposed to `mulss %xmm1, %xmm0` scalar single mult. instruction. Single (float) operates on half the bit-size of double. It is thus curious how gcc chooses to use the 128bit XMM registers on both accounts. 1. How does `make` know if a file must be recompiled? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** The source file is 'newer' than the currently compiled file, or the file does not exist. 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:** Include guards prevent common libraries and headers being imported several times. It looks like this: + #ifndef IVEBEENHEREBEFORE + #define IVEBEENHEREBEFORE + // do header stuff + #endif ##Library Task @@ -142,7 +151,7 @@ Insert your code between the brackets `{}`: void main( int argc, char *argv[] ) { - } + } double tab_sort_sum( double *tab, int tab_size ) { diff --git a/dmult.c b/dmult.c new file mode 100644 index 0000000..49746f4 --- /dev/null +++ b/dmult.c @@ -0,0 +1,3 @@ +double multiply( double x, double y ) { + return x * y; +} diff --git a/dmult.s b/dmult.s new file mode 100644 index 0000000..313834c --- /dev/null +++ b/dmult.s @@ -0,0 +1,14 @@ + .file "dmult.c" + .text +.globl multiply + .type multiply, @function +multiply: +.LFB0: + .cfi_startproc + mulsd %xmm1, %xmm0 + ret + .cfi_endproc +.LFE0: + .size multiply, .-multiply + .ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)" + .section .note.GNU-stack,"",@progbits diff --git a/fmult.c b/fmult.c new file mode 100644 index 0000000..ee56b1d --- /dev/null +++ b/fmult.c @@ -0,0 +1,3 @@ +float multiply( float x, float y ) { + return x * y; +} diff --git a/fmult.s b/fmult.s new file mode 100644 index 0000000..903044d --- /dev/null +++ b/fmult.s @@ -0,0 +1,14 @@ + .file "fmult.c" + .text +.globl multiply + .type multiply, @function +multiply: +.LFB0: + .cfi_startproc + mulss %xmm1, %xmm0 + ret + .cfi_endproc +.LFE0: + .size multiply, .-multiply + .ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)" + .section .note.GNU-stack,"",@progbits From 5b95e7ca312579431e6aa707d57df84e07a3b360 Mon Sep 17 00:00:00 2001 From: Joakim Hagen <220474@uis.no> Date: Fri, 29 Aug 2014 21:31:27 +0200 Subject: [PATCH 3/5] Working with C --- ANSWERS.md | 95 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 16 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 5487e35..7432949 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -45,12 +45,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` +**Answer:** + `cat > list2` + `orange` + `plum` + `mango` + `grapefruit` + `^D` + `cat list2` ###Exercise 3b @@ -128,9 +130,9 @@ Use `ls -l` to check that the permissions have changed. - **Answer:** The map shows address resolutions, .data, .code, etc sections size and position, archive members included and allocated symbols and their assigned value. 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:** - const char debug_cmt[] = "Some smart debugcomment\n"; - const char hello[] = "Hello world!\n"; - const int retval = 0; + const char debug_cmt[] = "Some smart debugcomment\n"; + const char hello[] = "Hello world!\n"; + const int retval = 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:** `gcc -O -S dmult.c` (took the liberty of optimizing) Examining dmult.s & fmult.s: `mulsd %xmm1, %xmm0` scalar double as opposed to `mulss %xmm1, %xmm0` scalar single mult. instruction. Single (float) operates on half the bit-size of double. It is thus curious how gcc chooses to use the 128bit XMM registers on both accounts. @@ -140,21 +142,82 @@ Use `ls -l` to check that the permissions have changed. - **Answer:** `make -f mymakefile` 1. How do you implement an *include guard*, and why is it needed? - **Answer:** Include guards prevent common libraries and headers being imported several times. It looks like this: - #ifndef IVEBEENHEREBEFORE - #define IVEBEENHEREBEFORE - // do header stuff - #endif + #ifndef IVEBEENHEREBEFORE + #define IVEBEENHEREBEFORE + // do header stuff + #endif ##Library Task Insert your code between the brackets `{}`: +**main.c:** + #include + #include + #include + #include + #include "l1.h" + void main( int argc, char *argv[] ) + { + int + size = 4, + min = 0, + max = 100, + range = max - min, + table[size][size]; + srand( time( NULL )); // seed random generator + for ( int i = size; i >= 0; i-- ) { + for ( int j = size; j >= 0; j-- ) + { + table[i][j] = rand_in_range( min, range ); + } } - - double tab_sort_sum( double *tab, int tab_size ) - { + + double sum = tab_sort_sum( ptable, size ); + + int cell_width = 10; // log_2( max ) + 1 + char *format[6]; + sprintf( format, '%%%dd', cell_width ); // e.g. '%10d' + char *line[size * cell_width + 1]; + + for ( i = 0; i < size; i++ ) // each row + { + for ( j = 0; j < size; i++ ) // each column - build line buffer + { + sprintf( line, format, table[i][j] ); + line += cell_width; // assuming char is 8bit! + } + printf( '%s\n' line ); + // No need to clean up the buffer. Rows have equal length } + printf( 'SUM: %20d\n', sum ); + } + + int rand_in_range( int min, int range ) + // Returns number within range with maintained random disribution. + // min + range must not exceed INT_MAX + { + unsigned long + intsize = (unsigned long) INT_MAX + 1, + groupsize = intsize / (unsigned long) range, + failmargin = intsize % groupsize; + long r; + while ( intsize - failmargin <= (unsigned long) (r = rand()) ); + return min + (int) (r / groupsize); + } + +**l1.c:** + double tab_sort_sum( double *tab, int tab_size ) + { + return 0.0; + } + +**makefile:** + main: main.c l1.h + gcc main.c -O -o main + l1.h: l1.c + gcc l1.c -c -O -o l1.h From c1d4eada196c9a758fe297a0f761888a0cee5039 Mon Sep 17 00:00:00 2001 From: Joakim Hagen <220474@uis.no> Date: Fri, 29 Aug 2014 21:45:30 +0200 Subject: [PATCH 4/5] fixing Markdown syntax --- ANSWERS.md | 159 ++++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 76 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 7432949..b615d8e 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -46,14 +46,15 @@ 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` - +``` +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. @@ -130,9 +131,11 @@ Use `ls -l` to check that the permissions have changed. - **Answer:** The map shows address resolutions, .data, .code, etc sections size and position, archive members included and allocated symbols and their assigned value. 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:** - const char debug_cmt[] = "Some smart debugcomment\n"; - const char hello[] = "Hello world!\n"; - const int retval = 0; +``` +const char debug_cmt[] = "Some smart debugcomment\n"; +const char hello[] = "Hello world!\n"; +const int retval = 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:** `gcc -O -S dmult.c` (took the liberty of optimizing) Examining dmult.s & fmult.s: `mulsd %xmm1, %xmm0` scalar double as opposed to `mulss %xmm1, %xmm0` scalar single mult. instruction. Single (float) operates on half the bit-size of double. It is thus curious how gcc chooses to use the 128bit XMM registers on both accounts. @@ -142,82 +145,86 @@ Use `ls -l` to check that the permissions have changed. - **Answer:** `make -f mymakefile` 1. How do you implement an *include guard*, and why is it needed? - **Answer:** Include guards prevent common libraries and headers being imported several times. It looks like this: - #ifndef IVEBEENHEREBEFORE - #define IVEBEENHEREBEFORE - // do header stuff - #endif - +``` +#ifndef IVEBEENHEREBEFORE +#define IVEBEENHEREBEFORE +// do header stuff +#endif +``` ##Library Task Insert your code between the brackets `{}`: **main.c:** - #include - #include - #include - #include - #include "l1.h" - - void main( int argc, char *argv[] ) +``` +#include +#include +#include +#include +#include "l1.h" + +void main( int argc, char *argv[] ) +{ + int + size = 4, + min = 0, + max = 100, + range = max - min, + table[size][size]; + srand( time( NULL )); // seed random generator + for ( int i = size; i >= 0; i-- ) { - int - size = 4, - min = 0, - max = 100, - range = max - min, - table[size][size]; - srand( time( NULL )); // seed random generator - for ( int i = size; i >= 0; i-- ) - { - for ( int j = size; j >= 0; j-- ) - { - table[i][j] = rand_in_range( min, range ); - } - } - - double sum = tab_sort_sum( ptable, size ); - - int cell_width = 10; // log_2( max ) + 1 - char *format[6]; - sprintf( format, '%%%dd', cell_width ); // e.g. '%10d' - char *line[size * cell_width + 1]; - - for ( i = 0; i < size; i++ ) // each row + for ( int j = size; j >= 0; j-- ) { - for ( j = 0; j < size; i++ ) // each column - build line buffer - { - sprintf( line, format, table[i][j] ); - line += cell_width; // assuming char is 8bit! - } - printf( '%s\n' line ); - // No need to clean up the buffer. Rows have equal length - } - printf( 'SUM: %20d\n', sum ); + table[i][j] = rand_in_range( min, range ); + } } - int rand_in_range( int min, int range ) - // Returns number within range with maintained random disribution. - // min + range must not exceed INT_MAX - { - unsigned long - intsize = (unsigned long) INT_MAX + 1, - groupsize = intsize / (unsigned long) range, - failmargin = intsize % groupsize; - long r; - while ( intsize - failmargin <= (unsigned long) (r = rand()) ); - return min + (int) (r / groupsize); - } + double sum = tab_sort_sum( ptable, size ); -**l1.c:** - double tab_sort_sum( double *tab, int tab_size ) + int cell_width = 10; // log_2( max ) + 1 + char *format[6]; + sprintf( format, '%%%dd', cell_width ); // e.g. '%10d' + char *line[size * cell_width + 1]; + + for ( i = 0; i < size; i++ ) // each row { - return 0.0; + for ( j = 0; j < size; i++ ) // each column - build line buffer + { + sprintf( line, format, table[i][j] ); + line += cell_width; // assuming char is 8bit! + } + printf( '%s\n' line ); + // No need to clean up the buffer. Rows have equal length } - + printf( 'SUM: %20d\n', sum ); +} + +int rand_in_range( int min, int range ) +// Returns number within range with maintained random disribution. +// min + range must not exceed INT_MAX +{ + unsigned long + intsize = (unsigned long) INT_MAX + 1, + groupsize = intsize / (unsigned long) range, + failmargin = intsize % groupsize; + long r; + while ( intsize - failmargin <= (unsigned long) (r = rand()) ); + return min + (int) (r / groupsize); +} +``` +**l1.c:** +``` +double tab_sort_sum( double *tab, int tab_size ) +{ + return 0.0; +} +``` **makefile:** - main: main.c l1.h - gcc main.c -O -o main - l1.h: l1.c - gcc l1.c -c -O -o l1.h - +``` +main: main.c l1.h + gcc main.c -O -o main +l1.h: l1.c + gcc l1.c -c -O -o l1.h +``` From fe63c5f2a94289ffe00b1704580bced0d3566c3b Mon Sep 17 00:00:00 2001 From: Joakim Hagen <220474@uis.no> Date: Mon, 1 Sep 2014 00:08:40 +0200 Subject: [PATCH 5/5] publish --- ANSWERS.md | 120 +++++++++++++++++++++++++++++++++-------------------- l1.c | 21 ++++++++++ main.c | 73 ++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 44 deletions(-) create mode 100644 l1.c create mode 100644 main.c diff --git a/ANSWERS.md b/ANSWERS.md index b615d8e..2653da5 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -158,73 +158,105 @@ Insert your code between the brackets `{}`: **main.c:** ``` #include +#include #include #include -#include -#include "l1.h" +// TODO: #include "l1.c" -void main( int argc, char *argv[] ) +int main( int argc, char *argv[] ) { - int - size = 4, - min = 0, - max = 100, - range = max - min, - table[size][size]; - srand( time( NULL )); // seed random generator - for ( int i = size; i >= 0; i-- ) + uint8_t debug = 0; + int size = 8; + double min = 0.0; + double range = 100.0; + int i; + char *p; + for ( i = 1; i < argc; i++ ) { - for ( int j = size; j >= 0; j-- ) + if ( !strcmp( argv[i], "-r" )) // range + { + range = strtod( argv[++i], &p ); + if ( !range ) + { + printf( "Argument to -r must be a positive number.\n" ); + exit(1); + } + } + else if ( !strcmp( argv[i], "-m" )) // minimum + { + min = strtod( argv[++i], &p ); + } + else if ( !strcmp( argv[i], "-s" )) // table size + { + size = strtod( argv[++i], &p ); + } + else if ( !strcmp( argv[i], "-d" )) // debug { - table[i][j] = rand_in_range( min, range ); + debug = -1; + for ( i = 1; i < argc; i++ ) + { + printf( "argv[%i]: \"%s\"\n", i, argv[i] ); + } } } + double div = RAND_MAX / range; + double table[size]; + + if ( debug ) + { + printf( "table size: %i\nminimum: %.2f\nrange: %.2f", size, min, range ); + } + + srand( time( NULL )); // seed random generator + for ( i = size; i >= 0; i-- ) + { + table[i] = min + ((double) rand()) / div; + } - double sum = tab_sort_sum( ptable, size ); + double sum = 0.0; //TODO: tab_sort_sum( &table, size ); - int cell_width = 10; // log_2( max ) + 1 - char *format[6]; - sprintf( format, '%%%dd', cell_width ); // e.g. '%10d' - char *line[size * cell_width + 1]; + /* + int cell_width = 4; // TODO: log_2( max ) + 3 + char *formatstr[6]; + sprintf( formatstr, '%%%dd\n', cell_width ); // e.g. '%10d' + const char *format[] = (const char *) formatstr; + */ + const char format[] = "%8.2f\n"; for ( i = 0; i < size; i++ ) // each row { - for ( j = 0; j < size; i++ ) // each column - build line buffer - { - sprintf( line, format, table[i][j] ); - line += cell_width; // assuming char is 8bit! - } - printf( '%s\n' line ); - // No need to clean up the buffer. Rows have equal length + printf( format, table[i] ); } - printf( 'SUM: %20d\n', sum ); -} - -int rand_in_range( int min, int range ) -// Returns number within range with maintained random disribution. -// min + range must not exceed INT_MAX -{ - unsigned long - intsize = (unsigned long) INT_MAX + 1, - groupsize = intsize / (unsigned long) range, - failmargin = intsize % groupsize; - long r; - while ( intsize - failmargin <= (unsigned long) (r = rand()) ); - return min + (int) (r / groupsize); + printf( "SUM: %f\n", sum ); + return 0; } ``` **l1.c:** +unfinished ``` +#include + +int *cmp( const double *a, const double *b ) +{ + if ( *a > *b ) return 1; + if ( *a < *b ) return -1; + return 0; +} + double tab_sort_sum( double *tab, int tab_size ) { - return 0.0; + qsort( tab, tab_size, sizeof(*tab), cmp ); + double sum = 0; + int i; + for ( i = 0; i >= 0; i-- ) + { + sum += tab[i]; + } + return sum; } ``` **makefile:** ``` -main: main.c l1.h +main: main.c l1.c gcc main.c -O -o main -l1.h: l1.c - gcc l1.c -c -O -o l1.h ``` - diff --git a/l1.c b/l1.c new file mode 100644 index 0000000..c8a6fd7 --- /dev/null +++ b/l1.c @@ -0,0 +1,21 @@ +#include + +int *cmp( const double *a, const double *b ) +{ + if ( *a > *b ) return 1; + if ( *a < *b ) return -1; + return 0; +} + +double tab_sort_sum( double *tab, int tab_size ) +{ + qsort( tab, tab_size, sizeof(*tab), cmp ); + double sum = 0; + int i; + for ( i = 0; i >= 0; i-- ) + { + sum += tab[i]; + } + return sum; +} + diff --git a/main.c b/main.c new file mode 100644 index 0000000..cb3a102 --- /dev/null +++ b/main.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +// TODO: #include "l1.c" + +int main( int argc, char *argv[] ) +{ + uint8_t debug = 0; + int size = 8; + double min = 0.0; + double range = 100.0; + int i; + char *p; + for ( i = 1; i < argc; i++ ) + { + if ( !strcmp( argv[i], "-r" )) // range + { + range = strtod( argv[++i], &p ); + if ( !range ) + { + printf( "Argument to -r must be a positive number.\n" ); + exit(1); + } + } + else if ( !strcmp( argv[i], "-m" )) // minimum + { + min = strtod( argv[++i], &p ); + } + else if ( !strcmp( argv[i], "-s" )) // table size + { + size = strtod( argv[++i], &p ); + } + else if ( !strcmp( argv[i], "-d" )) // debug + { + debug = -1; + for ( i = 1; i < argc; i++ ) + { + printf( "argv[%i]: \"%s\"\n", i, argv[i] ); + } + } + } + double div = RAND_MAX / range; + double table[size]; + + if ( debug ) + { + printf( "table size: %i\nminimum: %.2f\nrange: %.2f", size, min, range ); + } + + srand( time( NULL )); // seed random generator + for ( i = size; i >= 0; i-- ) + { + table[i] = min + ((double) rand()) / div; + } + + double sum = 0.0; //TODO: tab_sort_sum( &table, size ); + + /* + int cell_width = 4; // TODO: log_2( max ) + 3 + char *formatstr[6]; + sprintf( formatstr, '%%%dd\n', cell_width ); // e.g. '%10d' + const char *format[] = (const char *) formatstr; + */ + const char format[] = "%8.2f\n"; + + for ( i = 0; i < size; i++ ) // each row + { + printf( format, table[i] ); + } + printf( "SUM: %f\n", sum ); + return 0; +}