c-first-steps

a C playground
Index Commits Files Refs README
commit e8d4798ec11fda246a8e65e010526d4c39a16c48
parent 38474909a99d29efb03002231a495578a2b7a34c
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Thu, 10 Dec 2020 18:43:41 -0300

Added more exercises to c-repo/95.11/

Diffstat:
M95.11/guia03/ex29.c | 19+++++++++++++------
A95.11/guia03/ex30.c | 34++++++++++++++++++++++++++++++++++
A95.11/guia03/ex30_std_dev.c | 45+++++++++++++++++++++++++++++++++++++++++++++
A95.11/guia08/ex01.c | 44++++++++++++++++++++++++++++++++++++++++++++
A95.11/guia08/text.txt | 23+++++++++++++++++++++++
5 files changed, 159 insertions(+), 6 deletions(-)
diff --git a/95.11/guia03/ex29.c b/95.11/guia03/ex29.c
@@ -1,16 +1,23 @@
 #include <stdio.h>
+#include <stdlib.h>
 
 #define MAX_LEN 20
 
 int main (void) {
 
-    int i, num, vector[MAX_LEN];
+    int i, suma, vector[MAX_LEN], vector_end;
     char buffer[MAX_LEN];
 
-    
-
-    for(i = 0; i < MAX_LEN; i++)
-        printf("%d\n", vector[i]);
-
+    vector_end = MAX_LEN;
+    for(i = 0, suma = 0; i < vector_end; i++) {
+        if(fgets(buffer, sizeof(int) + 1, stdin) != NULL) {
+            vector[i] = atoi(buffer);
+            suma += vector[i];
+        } else {
+            vector_end = i;
+            i = MAX_LEN;
+        }
+    }
+    printf("%d\n", suma);
     return 0;
 }
diff --git a/95.11/guia03/ex30.c b/95.11/guia03/ex30.c
@@ -0,0 +1,34 @@
+//    Calculates the arithmetic mean of a certain amount of
+//    numbers. First ask for the quantity and then calculates
+//    it and prints it;
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define MAX_LEN 20
+
+int main (void) {
+
+    int i, n;
+    float x, suma, numbers[MAX_LEN];
+    char buffer[MAX_LEN];
+
+//    Read the quantity of numbers to mean;
+    if(fgets(buffer, sizeof(int) + 1, stdin) != NULL)
+        n = atoi(buffer);
+    else return 1;
+
+//    Read the values to mean  and stores it on numbers;
+    for(i = 0, suma = 0; i < n; i++) {
+        if(fgets(buffer, sizeof(int) + 1, stdin) != NULL) {
+            numbers[i] = atof(buffer);
+            suma += numbers[i];
+        } else return 1;
+    }
+//    Calculate the arithmetic mean and stores it on x;
+    x = ((1 / (float)n) * suma); 
+    
+//    Print the arithmetic mean;
+    printf("x = %.3f\n", x);
+    return 0;
+}
diff --git a/95.11/guia03/ex30_std_dev.c b/95.11/guia03/ex30_std_dev.c
@@ -0,0 +1,45 @@
+//    Calculates the arithmetic mean of a certain amount of
+//    numbers. First ask for the quantity and then calculates
+//    it and prints it;
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+#define MAX_LEN 20
+
+int main (void) {
+
+    int i, n;
+    float d, x, suma, numbers[MAX_LEN];
+    char buffer[MAX_LEN];
+
+//    Read the quantity of numbers to mean;
+    if(fgets(buffer, sizeof(int) + 1, stdin) != NULL)
+        n = atoi(buffer);
+    else return 1;
+
+//    Read the values to mean  and stores it on numbers;
+    for(i = 0, suma = 0; i < n; i++) {
+        if(fgets(buffer, sizeof(int) + 1, stdin) != NULL) {
+            numbers[i] = atof(buffer);
+            suma += numbers[i];
+        } else return 1;
+    }
+
+//    Calculate the arithmetic mean and stores it on x;
+    x = ((1 / (float)n) * suma); 
+
+//    Calculates the standard deviation;
+    for(i = 0; i < n; i++) {
+        suma += (numbers[i] - x);
+        d = sqrtf((1/n) * powf(suma, 2.0));
+    }
+
+//    Print the arithmetic mean;
+    printf("x = %.3f\n", x);
+
+//    Print the standard deviation;
+    printf("d = %.3f\n", d);
+    return 0;
+}
diff --git a/95.11/guia08/ex01.c b/95.11/guia08/ex01.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+#define NO_ARG              1
+#define MAX_ARR_SIZE    100
+
+typedef enum {
+    OK,
+    ERROR_PROGRAM_INVOCATION,
+    ERROR_NULL_POINTER,
+    ERROR_ALLOC
+} status_t;
+
+status_t validar_argumentos(int argc, char **argv);
+
+int main (int argc, char * argv[]) {
+
+    char *p;
+    int i;
+    status_t st;
+    if((st = validar_argumentos(argc, argv) != OK))
+        return st;
+
+    if((p = (char *)malloc(2 * (argc * MAX_ARR_SIZE))) == NULL)
+            return ERROR_ALLOC;
+
+    for(i = 0; i < (argc - 1); i++, argv++)
+        strlen((char *)argv);
+        (char *)p = ((char *)(*(++argv)));
+
+
+    free(p);
+    return 0;
+}
+
+status_t validar_argumentos(int argc, char **argv) {
+    if(argc == NO_ARG)
+        return ERROR_PROGRAM_INVOCATION;
+    else if(argv == NULL)
+        return ERROR_NULL_POINTER;
+        
+    return 0;
+}
diff --git a/95.11/guia08/text.txt b/95.11/guia08/text.txt
@@ -0,0 +1,23 @@
+The  malloc()  function allocates size bytes and returns a pointer
+to the allocated memory.  The memory is not initialized.  If  size
+is 0, then malloc() returns either NULL, or a unique pointer value
+that can later be successfully passed to free().
+
+The free() function frees the memory  space  pointed  to  by  ptr,
+which must have been returned by a previous call to malloc(), cal‐
+loc(), or realloc().  Otherwise, or if free(ptr) has already  been
+called  before, undefined behavior occurs.  If ptr is NULL, no op‐
+eration is performed.
+
+The calloc() function allocates memory for an array of nmemb  ele‐
+ments  of  size  bytes each and returns a pointer to the allocated
+memory.  The memory is set to zero.  If nmemb or size is  0,  then
+calloc()  returns  either NULL, or a unique pointer value that can
+later be successfully passed to free().  If the multiplication  of
+nmemb and size would result in integer overflow, then calloc() re‐
+turns an error.  By contrast, an integer overflow would not be de‐
+tected  in the following call to malloc(), with the result that an
+incorrectly sized block of memory would be allocated:
+
+   malloc(nmemb * size);
+