basic C learning
authorJens Kasten <jens@kasten-edv.de>
Wed, 02 Mar 2011 00:50:58 +0100
changeset 0 8da0314dad65
child 1 18e7503af60f
basic C learning
hebrae/Makefile
hebrae/hebrae.c
matadd/Makefile
matadd/matadd.c
quadzahl/Makefile
quadzahl/quadzahl.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hebrae/Makefile	Wed Mar 02 00:50:58 2011 +0100
@@ -0,0 +1,19 @@
+IDIR="/usr/src/linux/include/rsbac"
+CC=gcc
+CFLAGS=-I$(IDIR)
+
+ODIR=obj
+
+LIBS=-lrsbac
+OBJ=hebrae.o
+
+hebrae: $(OBJ)
+	gcc -o $@ $^ $(CFLAGS) $(LIBS)
+
+.PHONY: clean
+
+clean: 
+	rm -f *.o hebrae
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hebrae/hebrae.c	Wed Mar 02 00:50:58 2011 +0100
@@ -0,0 +1,51 @@
+#include <stdio.h>
+#include <string.h>
+
+#define CRYPT_ROWS 10
+#define CRYPT_COLUMNS 10
+#define TEXT_MAX_LENGTH CRYPT_ROWS * CRYPT_COLUMNS 
+
+char text[TEXT_MAX_LENGTH];
+
+int input_text(void)
+{
+    int  i = 0;
+    int c;
+
+    printf("Type your text: ");
+    while ((c=getchar()) != 10) { 
+        putchar(c);
+        text[i] = c;
+        i++;
+    }
+    return i-1;
+}
+
+int main(void)
+{
+    int crypt_text[CRYPT_ROWS][CRYPT_COLUMNS];
+    int text_length;
+    int i, j, k;
+
+    printf("Insert your text to: ");
+    text_length = input_text();
+    printf("text length: %d char(s)\n", text_length);
+    printf("text: %s\n", text);
+
+    for (i=0; i < CRYPT_ROWS; i++) {
+        for (j=0; j < CRYPT_COLUMNS; j++) {
+            for (k=0; k < strlen(text)-1; k++) {
+                crypt_text[i][j] = text[k];
+                printf("text k: %d\n", text[k]);
+            }
+        }
+    }
+
+    for (i=0; i < CRYPT_ROWS; i++) {
+        for (j=0; j < CRYPT_COLUMNS; j++) {
+            printf("crypt text [%d][%d] %d\n", i, j, crypt_text[i][j]);
+        }
+    }
+   
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/matadd/Makefile	Wed Mar 02 00:50:58 2011 +0100
@@ -0,0 +1,19 @@
+IDIR="/usr/src/linux/include/rsbac"
+CC=gcc
+CFLAGS=-I$(IDIR)
+
+ODIR=obj
+
+LIBS=-lrsbac
+OBJ=matadd.o
+
+matadd: $(OBJ)
+	gcc -o $@ $^ $(CFLAGS) $(LIBS)
+
+.PHONY: clean
+
+clean: 
+	rm -f *.o matadd
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/matadd/matadd.c	Wed Mar 02 00:50:58 2011 +0100
@@ -0,0 +1,75 @@
+#include <stdio.h>
+
+#define MATRIX_ROWS 3
+#define MATRIX_COLUMS 2
+
+void output_one(int matrix[MATRIX_ROWS][MATRIX_COLUMS])
+{
+    int i, j;
+
+    printf("The first matrix: \n");
+    for (i=0; i < MATRIX_ROWS; i++) {
+        for (j=0; j < MATRIX_COLUMS; j++) {
+            printf("[%d][%d] %d ", i, j, matrix[i][j]);
+        }
+        printf("\n");
+    }
+}
+
+void output_two(int matrix[MATRIX_ROWS][MATRIX_COLUMS])
+{
+    int i, j;
+
+    printf("The second matrix: \n");
+    for (i=0; i < MATRIX_ROWS; i++) {
+        for (j=0; j < MATRIX_COLUMS; j++) {
+            printf("[%d][%d] %d ", i, j, matrix[i][j]);
+        }
+        printf("\n");
+    }
+}
+
+void addition_matrix(int matrix_one[MATRIX_ROWS][MATRIX_COLUMS],
+                  int matrix_two[MATRIX_ROWS][MATRIX_COLUMS])
+{                 
+    int matrix_result[MATRIX_ROWS][MATRIX_COLUMS];
+    int i, j;
+
+    printf("Result:\n");
+    for (i=0; i < MATRIX_ROWS; i++) {
+        for (j=0; j < MATRIX_COLUMS; j++) {
+            matrix_result[i][j] = matrix_one[i][j] + matrix_two[i][j];
+            printf("[%d][%d] %d ", i, j, matrix_result[i][j]);
+        }
+        printf("\n");
+    }
+}
+
+int main(void)
+{
+    int matrix_one[MATRIX_ROWS][MATRIX_COLUMS];
+    int matrix_two[MATRIX_ROWS][MATRIX_COLUMS];
+    int i, j;
+
+
+    printf("This program does make an addition on two matrix[2][3] format.\n");
+    printf("Values for matrix one:\n");
+    for (i=0; i < MATRIX_ROWS; i++) {
+        for (j=0; j < MATRIX_COLUMS; j++) {
+            printf("matrix[%d][%d]: ", i, j);
+            scanf("%d", &matrix_one[i][j]);
+        }
+    }
+    printf("Values for matrix two:\n");
+    for (i=0; i < MATRIX_ROWS; i++) {
+        for (j=0; j < MATRIX_COLUMS; j++) {
+            printf("matrix[%d][%d]: ", i, j);
+            scanf("%d", &matrix_two[i][j]);
+        }
+    }
+
+    output_one(matrix_one);
+    output_two(matrix_two);            
+    addition_matrix(matrix_one, matrix_two);
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quadzahl/Makefile	Wed Mar 02 00:50:58 2011 +0100
@@ -0,0 +1,19 @@
+IDIR="/usr/src/linux/include/rsbac"
+CC=gcc
+CFLAGS=-I$(IDIR)
+
+ODIR=obj
+
+LIBS=-lrsbac
+OBJ=quadzahl.o
+
+quadzahl: $(OBJ)
+	gcc -o $@ $^ $(CFLAGS) $(LIBS)
+
+.PHONY: clean
+
+clean: 
+	rm -f *.o quadzahl
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quadzahl/quadzahl.c	Wed Mar 02 00:50:58 2011 +0100
@@ -0,0 +1,76 @@
+#include <stdio.h>
+
+#define MAX_SQUARE 20
+
+void square_a_number(int *psquare);
+void display_square(int *psquare);
+void display_even(int *psquare);
+void display_odd(int *psquare);
+
+int main(void)
+{
+    int square[MAX_SQUARE];
+    int i, number;
+    int *psquare;
+
+    psquare = square;
+    square_a_number(psquare);
+
+    printf("Choose an action: \n");
+    printf("1) to display all\n");
+    printf("2) to display even square\n");
+    printf("3) to display odd square\n");
+    scanf("%d", &number);
+    
+    switch (number) {
+        case 1: display_square(psquare); break;
+        case 2: display_even(psquare); break;
+        case 3: display_odd(psquare); break;
+    }        
+   
+    return 0;
+}
+
+void square_a_number(int *psquare) 
+{
+    int i;
+
+    for (i=0; i < MAX_SQUARE; i++) {
+        psquare[i] = (i+1) * (i+1);
+    }
+}
+
+void display_square(int *psquare) 
+{
+    int i;
+
+    printf("number: square\n");
+    for (i=0; i < MAX_SQUARE; i++) {
+        printf("%d: %d\n", i+1, psquare[i]);
+    }
+}
+
+void display_even(int *psquare) 
+{
+    int i;
+
+    printf("number: even square\n");
+    for (i=0; i < MAX_SQUARE; i++) {
+        if ((psquare[i] %2 ) == 0) {
+            printf("%d: %d\n", i+1, psquare[i]);
+        }
+    }
+}
+
+void display_odd(int *psquare) 
+{
+    int i;
+
+    printf("number: odd square\n");
+    for (i=0; i < MAX_SQUARE; i++) {
+        if ((psquare[i] % 2) == 1) {
+            printf("%d: %d\n", i+1, psquare[i]);
+        }
+    }
+}    
+