9511_workbook

solved exercises from algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README
commit 9e864f7eb82b6ff4d5afeb1281bbcf148524c526
parent d0c19ae0630f78d68f1fc304804a7ef5a336ca7d
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Tue, 22 Jun 2021 02:31:11 -0300

Updated: added guia07 exercises

Diffstat:
Mguia04/ex09.c | 8+++++---
Mguia07/ex05.c | 20++++++++++----------
Mguia07/ex06.c | 4----
Aguia07/ex07.c | 42++++++++++++++++++++++++++++++++++++++++++
Aguia07/ex08.c | 116+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 173 insertions(+), 17 deletions(-)
diff --git a/guia04/ex09.c b/guia04/ex09.c
@@ -1,9 +1,11 @@
+/*    Populates the direction in memory pointed by every element of an array
+ *    of 10 pointers to float    with 10 numbers entered via stdin.    */
+ 
 #include <stdio.h>
 #include <stdlib.h>
 #include <float.h>
 
 #define BUFFER_LEN 10
-#define MAX_IN_LEN 6
 
 int main (void)
 {
@@ -29,9 +31,9 @@ int main (void)
     }
 
     /*    Populates the array with user input converted to float, computes the sume of all
-     *    the converted numbers and then frees the memmory since it's not longer needed    */
+     *    the converted numbers and then frees the memmory since it's no longer needed    */
     for(i = *sum = 0; i < 10 ; i++) {
-        if(!fgets(buffer, MAX_IN_LEN, stdin)) return 2;
+        if(!fgets(buffer, BUFFER_LEN, stdin)) return 2;
         *sum += (*pf[i] = strtof(buffer, NULL));
 
         printf("%p: %5f\n", pf[i], *pf[i]);
diff --git a/guia07/ex05.c b/guia07/ex05.c
@@ -5,27 +5,27 @@
 
 int main(void) {
 
-//    0x10 30 25 F4
+    /* 0x10 30 25 F4 */
     unsigned int n = 271590900;
 
-//    Arreglo de 4 bytes;
-    unsigned char v[4]; 
+    /* Arreglo de 4 bytes; */
+    unsigned char x[4]; 
 
     size_t i;
 /*
-    v[0] = (n >> 24) & 0xFF;
-    v[1] = (n >> 16) & 0xFF;
-    v[2] = (n >> 8) & 0xFF;
-    v[3] = (n >> 0) & 0xFF;
+    x[0] = (n >> 24) & 0xFF;
+    x[1] = (n >> 16) & 0xFF;
+    x[2] = (n >> 8) & 0xFF;
+    x[3] = (n >> 0) & 0xFF;
 */
     for(i = 0; i < sizeof(unsigned int); i++)
-        v[i] = (n >> ( (sizeof(unsigned int) - 1 - i) * 8  )) & 0xFF;
+        x[i] = (n >> ( (sizeof(unsigned int) - 1 - i) * 8  )) & MASK_BYTE;
 
     printf("%d\n", n);
-    printf("%x\n", n);
+    printf("%X\n", n);
 
     for(i = 0; i < sizeof(unsigned int); i++)
-        printf("v[%lu] = %x\n", i, v[i]);
+        printf("x[%lu] = %X\n", i, x[i]);
 
     return EXIT_SUCCESS;
 }
diff --git a/guia07/ex06.c b/guia07/ex06.c
@@ -1,9 +1,5 @@
 #include <stdio.h>
 
-
-
-
-
 int main (void)
 {
     unsigned int n;
diff --git a/guia07/ex07.c b/guia07/ex07.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+
+#define MASK_POWER        0x80
+
+#define SHIFT_POWER        7
+
+typedef unsigned char uchar;
+typedef enum { OK, ERROR_NULL_POINTER } status_t;
+
+void print_reg(unsigned char reg);
+status_t toggle_bit(uchar *reg, uchar shift);
+
+int main (void)
+{
+    /* 0000 0000 */
+    uchar reg = 0x00;
+    print_reg(reg);
+
+    if(toggle_bit(&reg, SHIFT_POWER)) return ERROR_NULL_POINTER;
+    print_reg(reg);
+
+    if(toggle_bit(&reg, SHIFT_POWER)) return ERROR_NULL_POINTER;
+    print_reg(reg);
+    return 0;
+}
+
+void print_reg(uchar reg)
+{
+    for(size_t i = ((sizeof(reg) * 8)); i > 0; i--)
+        putchar((reg & (0x01 << (i - 1))) ? '1' : '0');
+
+    putchar('\n');
+}
+
+status_t toggle_bit(uchar *reg, uchar shift)
+{
+    if(!reg) return ERROR_NULL_POINTER;
+
+    *reg ^= (0x01 << shift);
+
+    return OK;
+}
diff --git a/guia07/ex08.c b/guia07/ex08.c
@@ -0,0 +1,116 @@
+#include <stdio.h>
+
+#define MASK_RED    0xFF0000
+#define MASK_GREEN    0x00FF00
+#define MASK_BLUE    0x0000FF
+
+#define SHIFT_RED    16
+#define SHIFT_GREEN    8
+#define SHIFT_BLUE    0
+
+typedef unsigned int uint;
+typedef unsigned char uchar;
+typedef enum { OK, ERROR_BRIGHTNESS_OUT_OF_RANGE, ERROR_NULL_POINTER } status_t;
+
+uchar get_red(uint color);
+uchar get_green(uint color);
+uchar get_blue(uint color);
+
+status_t rgb_components(uint color, uchar *red, uchar *green, uchar *blue);
+status_t rgb_components2(uint color, uchar *red, uchar *green, uchar *blue);
+
+uint mix_colors(uint color1, uint color2);
+uint change_brightness(uint *color, float brightness);
+uint complementary_color(uint color);
+
+int main (void)
+{
+    uint color1 = 0xFF6464;
+    uint color2 = 0x0100FF;
+
+    printf("color1: %06X\n", color1);
+    printf("color2: %06X\n", color2);
+    printf("color2: %06X\n", complementary_color(color1));
+    
+    printf("%X\n", mix_colors(color1, color2));
+
+    change_brightness(&color1, 1);
+
+    printf("%d red\n", get_red(color1));
+    printf("%d green\n", get_green(color1));
+    printf("%d blue\n", get_blue(color1));
+
+    return OK;
+}
+
+uchar get_red(uint color)
+{
+    return (uchar)((color & MASK_RED) >> SHIFT_RED);
+}
+
+uchar get_green(uint color)
+{
+    return (uchar)((color & MASK_GREEN) >> SHIFT_GREEN);
+}
+
+uchar get_blue(uint color)
+{
+    return (uchar)((color & MASK_BLUE) >> SHIFT_BLUE);
+}
+
+/* This implementation its independent from other functions */
+status_t rgb_components(uint color, uchar *red, uchar *green, uchar *blue)
+{
+    if(!red || !green || !blue) return ERROR_NULL_POINTER;
+
+    *red = (uchar)((color & MASK_RED) >> SHIFT_RED);
+    *green = (uchar)((color & MASK_GREEN) >> SHIFT_GREEN);
+    *blue = (uchar)((color & MASK_BLUE) >> SHIFT_BLUE);
+
+    return OK;
+}
+
+/* This implementation insted its dependent from other functions */
+status_t rgb_components2(uint color, uchar *red, uchar *green, uchar *blue)
+{
+    if(!red || !green || !blue) return ERROR_NULL_POINTER;
+
+    *red = get_red(color);
+    *green = get_green(color);
+    *blue = get_blue(color);
+
+    return OK;
+}
+
+uint mix_colors(uint color1, uint color2)
+{
+    uint aux, res;
+
+    /* Suma color con color, y lo va asignando en res, en caso de ser mayor al maximo 255, asigna 255 al color que corresponda */
+    res = ((aux = ((color1 & MASK_RED) + (color2 & MASK_RED))) > MASK_RED) ? MASK_RED : aux;
+    res += ((aux = ((color1 & MASK_GREEN) + (color2 & MASK_GREEN))) > MASK_GREEN) ? MASK_GREEN : aux;
+    res += ((aux = ((color1 & MASK_BLUE) + (color2 & MASK_BLUE))) > MASK_BLUE) ? MASK_BLUE : aux;
+
+    return res;
+}
+
+status_t change_brightness(uint *color, float brightness)
+{
+    if(!color) return ERROR_NULL_POINTER;
+    else if(brightness < 0 || brightness > 100) return ERROR_BRIGHTNESS_OUT_OF_RANGE;
+
+    /* Convierte el valor de brillo en porcentual */
+    brightness *= .01;
+
+    /* Multiplica cada color con brightness */
+    *color = ((int)(((*color & MASK_RED) >> SHIFT_RED) * brightness) << SHIFT_RED) + (*color & ~MASK_RED);
+    *color = ((int)(((*color & MASK_GREEN) >> SHIFT_GREEN) * brightness) << SHIFT_GREEN) + (*color & ~MASK_GREEN);
+    *color = ((int)(((*color & MASK_BLUE) >> SHIFT_BLUE) * brightness) << SHIFT_BLUE) + (*color & ~MASK_BLUE);
+
+    return OK;
+}
+
+uint complementary_color(uint color)
+{
+    return (MASK_RED - (color & MASK_RED)) + (MASK_GREEN - (color & MASK_GREEN)) + (MASK_BLUE - (color & MASK_BLUE));
+}