diff options
Diffstat (limited to 'test/gourdinl')
-rw-r--r-- | test/gourdinl/c/add_return.c | 1 | ||||
-rw-r--r-- | test/gourdinl/c/addresses.c | 32 | ||||
-rw-r--r-- | test/gourdinl/c/arith.c | 16 | ||||
-rw-r--r-- | test/gourdinl/c/arith_print.c | 19 | ||||
-rw-r--r-- | test/gourdinl/c/armstrong.c | 21 | ||||
-rw-r--r-- | test/gourdinl/c/array1.c | 64 | ||||
-rw-r--r-- | test/gourdinl/c/array2.c | 74 | ||||
-rw-r--r-- | test/gourdinl/c/biggest_of_3_int.c | 10 | ||||
-rw-r--r-- | test/gourdinl/c/bitwise1.c | 8 | ||||
-rw-r--r-- | test/gourdinl/c/cpintarray.c | 108 | ||||
-rw-r--r-- | test/gourdinl/c/enum1.c | 52 | ||||
-rw-r--r-- | test/gourdinl/c/enum2.c | 50 | ||||
-rw-r--r-- | test/gourdinl/c/floop.c | 8 | ||||
-rw-r--r-- | test/gourdinl/c/floor.c | 29 | ||||
-rw-r--r-- | test/gourdinl/c/funcs.c | 36 | ||||
-rw-r--r-- | test/gourdinl/c/hello.c | 6 | ||||
-rw-r--r-- | test/gourdinl/c/if.c | 7 | ||||
-rw-r--r-- | test/gourdinl/c/msb_pos.c | 20 | ||||
-rw-r--r-- | test/gourdinl/c/power2.c | 42 | ||||
-rw-r--r-- | test/gourdinl/c/prime.c | 23 | ||||
-rw-r--r-- | test/gourdinl/c/random.c | 50 | ||||
-rw-r--r-- | test/gourdinl/c/simple_op.c | 8 | ||||
-rw-r--r-- | test/gourdinl/c/wloop.c | 8 |
23 files changed, 692 insertions, 0 deletions
diff --git a/test/gourdinl/c/add_return.c b/test/gourdinl/c/add_return.c new file mode 100644 index 00000000..c29aeb16 --- /dev/null +++ b/test/gourdinl/c/add_return.c @@ -0,0 +1 @@ +int main(int r) { return r+1; } diff --git a/test/gourdinl/c/addresses.c b/test/gourdinl/c/addresses.c new file mode 100644 index 00000000..e3cb5201 --- /dev/null +++ b/test/gourdinl/c/addresses.c @@ -0,0 +1,32 @@ +/* addresses.c -- Playing with addresses of variables and their contents: + * what is done by C with variables, addresses, and values. + */ + +#include <stdio.h> + +void moo(int a, int * b); + +int main(void) { + int x; + int *y; + + x=1; + y=&x; + printf("Address of x = %d, value of x = %d\n", &x, x); + printf("Address of y = %d, value of y = %d, value of *y = %d\n", &y, y, *y); + moo(9,y); +} + +void moo(int a, int *b){ + printf("Address of a = %d, value of a = %d\n", &a, a); + printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b); +} + +/* Output from running this program on my computer: + +Address of x = 536869640, value of x = 1 +Address of y = 536869632, value of y = 536869640, value of *y = 1 +Address of a = 536869608, value of a = 9 +Address of b = 536869600, value of b = 536869640, value of *b = 1 + + */ diff --git a/test/gourdinl/c/arith.c b/test/gourdinl/c/arith.c new file mode 100644 index 00000000..02df141b --- /dev/null +++ b/test/gourdinl/c/arith.c @@ -0,0 +1,16 @@ +int main(int num1, int num2) +{ + int sum, sub, mult, mod; + float div; + + /* + * Perform all arithmetic operations + */ + sum = num1 + num2; + sub = num1 - num2; + mult = num1 * num2; + div = (float)num1 / num2; + mod = num1 % num2; + + return sum + sub + mult + div + mod; +} diff --git a/test/gourdinl/c/arith_print.c b/test/gourdinl/c/arith_print.c new file mode 100644 index 00000000..d404a151 --- /dev/null +++ b/test/gourdinl/c/arith_print.c @@ -0,0 +1,19 @@ +int main() +{ + int num1 = 2; + int num2 = 4; + int sum, sub, mult, mod; + float div; + + /* + * Perform all arithmetic operations + */ + sum = num1 + num2; + sub = num1 - num2; + mult = num1 * num2; + div = (float)num1 / num2; + mod = num1 % num2; + + printf("%d", sum + sub + mult + div + mod); + return; +} diff --git a/test/gourdinl/c/armstrong.c b/test/gourdinl/c/armstrong.c new file mode 100644 index 00000000..c5d838f9 --- /dev/null +++ b/test/gourdinl/c/armstrong.c @@ -0,0 +1,21 @@ +int main() +{ + int n,sum,i,t,a,z; + + for(i = 1; i <= 500; i++) + { + t = i; // as we need to retain the original number + sum = 0; + while(t != 0) + { + a = t%10; + sum += a*a*a; + t = t/10; + } + + if(sum == i) + z += i; + } + + return 0; +} diff --git a/test/gourdinl/c/array1.c b/test/gourdinl/c/array1.c new file mode 100644 index 00000000..5840ca66 --- /dev/null +++ b/test/gourdinl/c/array1.c @@ -0,0 +1,64 @@ +/* array1.c -- Simple operations with arrays. + */ + +#include <stdio.h> +#define N 10 + +void oneWay(void); +void anotherWay(void); + +int main(void) { + printf("\noneWay:\n"); + oneWay(); + printf("\nantherWay:\n"); + anotherWay(); +} + +/*Array initialized with aggregate */ +void oneWay(void) { + int vect[N] = {1,2,3,4,5,6,7,8,9,0}; + int i; + + for (i=0; i<N; i++) + printf("i = %2d vect[i] = %2d\n", i, vect[i]); +} + +/*Array initialized with loop */ +void anotherWay(void) { + int vect[N]; + int i; + + for (i=0; i<N; i++) + vect[i] = i+1; + + for (i=0; i<N; i++) + printf("i = %2d vect[i] = %2d\n", i, vect[i]); +} + +/* The output of this program is + +oneWay: +i = 0 vect[i] = 1 +i = 1 vect[i] = 2 +i = 2 vect[i] = 3 +i = 3 vect[i] = 4 +i = 4 vect[i] = 5 +i = 5 vect[i] = 6 +i = 6 vect[i] = 7 +i = 7 vect[i] = 8 +i = 8 vect[i] = 9 +i = 9 vect[i] = 0 + +antherWay: +i = 0 vect[i] = 1 +i = 1 vect[i] = 2 +i = 2 vect[i] = 3 +i = 3 vect[i] = 4 +i = 4 vect[i] = 5 +i = 5 vect[i] = 6 +i = 6 vect[i] = 7 +i = 7 vect[i] = 8 +i = 8 vect[i] = 9 +i = 9 vect[i] = 10 + + */ diff --git a/test/gourdinl/c/array2.c b/test/gourdinl/c/array2.c new file mode 100644 index 00000000..389e1596 --- /dev/null +++ b/test/gourdinl/c/array2.c @@ -0,0 +1,74 @@ +/* array2.c -- Read/writing/reversing integer arrays + */ + +#include <stdio.h> + +#define NMAX 10 + +void intSwap(int *x, int *y); +int getIntArray(int a[], int nmax, int sentinel); +void printIntArray(int a[], int n); +void reverseIntArray(int a[], int n); + +int main(void) { + int x[NMAX]; + int hmny; + + hmny = getIntArray(x, NMAX, 0); + printf("The array was: \n"); + printIntArray(x,hmny); + reverseIntArray(x,hmny); + printf("after reverse it is:\n"); + printIntArray(x,hmny); +} + +void intSwap(int *x, int *y) + /* It swaps the content of x and y */ +{ + int temp = *x; + *x = *y; + *y = temp; +} + +void printIntArray(int a[], int n) + /* n is the number of elements in the array a. + * These values are printed out, five per line. */ +{ + int i; + + for (i=0; i<n; ){ + printf("\t%d ", a[i++]); + if (i%5==0) + printf("\n"); + } + printf("\n"); +} + +int getIntArray(int a[], int nmax, int sentinel) + /* It reads up to nmax integers and stores then in a; sentinel + * terminates input. */ +{ + int n = 0; + int temp; + + do { + printf("Enter integer [%d to terminate] : ", sentinel); + scanf("%d", &temp); + if (temp==sentinel) break; + if (n==nmax) + printf("array is full\n"); + else + a[n++] = temp; + }while (1); + return n; +} + +void reverseIntArray(int a[], int n) + /* It reverse the order of the first n elements of a */ +{ + int i; + + for(i=0;i<n/2;i++){ + intSwap(&a[i],&a[n-i-1]); + } +} diff --git a/test/gourdinl/c/biggest_of_3_int.c b/test/gourdinl/c/biggest_of_3_int.c new file mode 100644 index 00000000..346908fc --- /dev/null +++ b/test/gourdinl/c/biggest_of_3_int.c @@ -0,0 +1,10 @@ +int main(int a, int b, int c) { + if ((a > b) && (a > c)) { + return a; + } else if (b > c) { + return b; + } else { + return c; + } + return 0; +} diff --git a/test/gourdinl/c/bitwise1.c b/test/gourdinl/c/bitwise1.c new file mode 100644 index 00000000..d20641de --- /dev/null +++ b/test/gourdinl/c/bitwise1.c @@ -0,0 +1,8 @@ +int main() +{ + int x = 6, y = 4; + x = x^y; + y = x^876987^x | y << 42; + + return ~x^y; +} diff --git a/test/gourdinl/c/cpintarray.c b/test/gourdinl/c/cpintarray.c new file mode 100644 index 00000000..8049fdfb --- /dev/null +++ b/test/gourdinl/c/cpintarray.c @@ -0,0 +1,108 @@ +/* cpintarray.c -- Example showing how addresses and arrays are alike + */ + +#include <stdio.h> +#define SIZE 8 + +void cpIntArray(int *a, int *b, int n) +/*It copies n integers starting at b into a*/ +{ + for(;n>0;n--) + *a++=*b++; +} + + +void printIntArray(int a[], int n) + /* n is the number of elements in the array a. + * These values are printed out, five per line. */ +{ + int i; + + for (i=0; i<n; ){ + printf("\t%d ", a[i++]); + if (i%5==0) + printf("\n"); + } + printf("\n"); +} + +int getIntArray(int a[], int nmax, int sentinel) + /* It reads up to nmax integers and stores then in a; sentinel + * terminates input. */ +{ + int n = 0; + int temp; + + do { + printf("Enter integer [%d to terminate] : ", sentinel); + scanf("%d", &temp); + if (temp==sentinel) break; + if (n==nmax) + printf("array is full\n"); + else + a[n++] = temp; + }while (1); + return n; +} + +int main(void){ + int x[SIZE], nx; + int y[SIZE], ny; + + printf("Read the x array:\n"); + nx = getIntArray(x,SIZE,0); + printf("The x array is:\n"); + printIntArray(x,nx); + + printf("Read the y array:\n"); + ny = getIntArray(y,SIZE,0); + printf("The y array is:\n"); + printIntArray(y,ny); + + cpIntArray(x+2,y+3,4); + /*Notice the expression 'x+2'. x is interpreted as the address for + the beginning of the x array. +2 sais to increment that address + by two units, in accordance with the type of x, which is + an integer array. Thus we move from x to two integer locations + past it, that is to the location of x[2]. The same reasoning applied + to 'y+3'. + */ + printf("Printing x after having copied 4 elements\n" + "from y starting at y[3] into x starting at x[2]\n"); + printIntArray(x,nx); +} + +/* Here is the interaction in a run of this program: + +Read the x array: +Enter integer [0 to terminate] : 1 +Enter integer [0 to terminate] : 3 +Enter integer [0 to terminate] : 5 +Enter integer [0 to terminate] : 7 +Enter integer [0 to terminate] : 9 +Enter integer [0 to terminate] : 11 +Enter integer [0 to terminate] : 13 +Enter integer [0 to terminate] : 15 +Enter integer [0 to terminate] : 0 +The x array is: + 1 3 5 7 9 + 11 13 15 +Read the y array: +Enter integer [0 to terminate] : 2 +Enter integer [0 to terminate] : 4 +Enter integer [0 to terminate] : 6 +Enter integer [0 to terminate] : 8 +Enter integer [0 to terminate] : 10 +Enter integer [0 to terminate] : 12 +Enter integer [0 to terminate] : 14 +Enter integer [0 to terminate] : 16 +Enter integer [0 to terminate] : 0 +The y array is: + 2 4 6 8 10 + 12 14 16 +Printing x after having copied 4 elements +from y starting at y[3] into x starting at x[2] + 1 3 8 10 12 + 14 13 15 + + */ diff --git a/test/gourdinl/c/enum1.c b/test/gourdinl/c/enum1.c new file mode 100644 index 00000000..d1f6b48d --- /dev/null +++ b/test/gourdinl/c/enum1.c @@ -0,0 +1,52 @@ +/* enum1.c -- Starting to use enumerated types: Printing for each + * day of the week, today, yesterday, and tomorrow, both + * as a string and as a number. + */ + +#include <stdio.h> + +/* Introducing an enumerated data type */ +enum days {monday,tuesday,wednesday,thursday,friday,saturday,sunday}; +typedef enum days days; // This allows us to use "days" as an abbreviation + // for "enum days" + +/* Two useful functions */ +days yesterday(days today){ + return (today+6)%7; +} +days tomorrow(days today){ + return (today+1)%7; +} + +// A useful array: thedays is an array of constant (i.e you cannot +// modify them) pointers to constant (i.e. you cannot modify them) strings +const char * const thedays[] = + {"monday", "tuesday", "wednesday", "thursday", + "friday", "saturday", "sunday"}; + +int main(void){ + days today; + + printf("today \tyesterday \ttomorrow\n" + "============================================\n"); + for (today=monday;today<=sunday;today++) + printf("%s = %d \t %s = %d \t %s = %d\n", + thedays[today], today, + thedays[yesterday(today)], yesterday(today), + thedays[tomorrow(today)], tomorrow(today)); +} + +/* + The output is: + +today yesterday tomorrow +============================================ +monday = 0 sunday = 6 tuesday = 1 +tuesday = 1 monday = 0 wednesday = 2 +wednesday = 2 tuesday = 1 thursday = 3 +thursday = 3 wednesday = 2 friday = 4 +friday = 4 thursday = 3 saturday = 5 +saturday = 5 friday = 4 sunday = 6 +sunday = 6 saturday = 5 monday = 0 + +*/ diff --git a/test/gourdinl/c/enum2.c b/test/gourdinl/c/enum2.c new file mode 100644 index 00000000..a18acb80 --- /dev/null +++ b/test/gourdinl/c/enum2.c @@ -0,0 +1,50 @@ +/* enum2.c -- Starting to use enumerated types: Printing for each + * day of the week, today, yesterday, and tomorrow, both + * as a string and as a number. We use typedef + */ + +#include <stdio.h> + +/* Introducing an enumerated data type */ +typedef enum {monday,tuesday,wednesday,thursday,friday,saturday,sunday} days; + +/* Two useful functions */ +days yesterday(days today); +days tomorrow(days today); + +char *thedays[] = {"monday", "tuesday", "wednesday", "thursday", + "friday", "saturday", "sunday"}; + +int main(void){ + days today; + + printf("today \tyesterday \ttomorrow\n" + "============================================\n"); + for (today=monday;today<=sunday;today++) + printf("%s = %d \t %s = %d \t %s = %d\n", + thedays[today], today, + thedays[yesterday(today)], yesterday(today), + thedays[tomorrow(today)], tomorrow(today)); +} + +days yesterday(days today){ + return (today+6)%7; +} +days tomorrow(days today){ + return (today+1)%7; +} + +/* + The output is: + +today yesterday tomorrow +============================================ +monday = 0 sunday = 6 tuesday = 1 +tuesday = 1 monday = 0 wednesday = 2 +wednesday = 2 tuesday = 1 thursday = 3 +thursday = 3 wednesday = 2 friday = 4 +friday = 4 thursday = 3 saturday = 5 +saturday = 5 friday = 4 sunday = 6 +sunday = 6 saturday = 5 monday = 0 + +*/ diff --git a/test/gourdinl/c/floop.c b/test/gourdinl/c/floop.c new file mode 100644 index 00000000..30270892 --- /dev/null +++ b/test/gourdinl/c/floop.c @@ -0,0 +1,8 @@ +int main(int x) +{ + int y = 4; + int s = 23; + for(int i = 0; i <= x; i++) + y << s; + return y; +} diff --git a/test/gourdinl/c/floor.c b/test/gourdinl/c/floor.c new file mode 100644 index 00000000..33a57af3 --- /dev/null +++ b/test/gourdinl/c/floor.c @@ -0,0 +1,29 @@ +int main(int n) +{ + int x = 1, i; + + /* for positive values */ + if (n > 0) + { + for (; x <= n >> 1;) + { + x = x << 1; + } + n = x; + } + /* for negative values */ + else + { + n = ~n; + n = n + 1; + for (; x <= n >> 1;) + { + x = x << 1; + } + x = x << 1; + x = ~x; + x = x + 1; + n = x; + } + return n; +} diff --git a/test/gourdinl/c/funcs.c b/test/gourdinl/c/funcs.c new file mode 100644 index 00000000..49e610d6 --- /dev/null +++ b/test/gourdinl/c/funcs.c @@ -0,0 +1,36 @@ +/* funcs.c -- More examples of functions + */ + +#include <stdio.h> + +int getint(void); /*It prompts user to enter an integer, which it returns*/ + +int getmax(int a, int b, int c); /*It returns value of largest of a, b, c*/ + +/* Main program: Using the various functions */ +int main (void) { + int x, y, z; + + x = getint(); + y = getint(); + z = getint(); + printf("The largest of %d, %d, and %d is %d\n", x, y, z, getmax(x,y,z)); +} + +int getint(void) { + int a; + + printf("Please enter an integer > "); + scanf("%d", &a); + return(a); +} + +int getmax(int a, int b, int c){ + int m = a; + + if (m<b) + m = b; + if (m<c) + m = c; + return(m); +} diff --git a/test/gourdinl/c/hello.c b/test/gourdinl/c/hello.c new file mode 100644 index 00000000..0279269e --- /dev/null +++ b/test/gourdinl/c/hello.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main(void) { + printf("Hello World!\n"); + return 0; +} diff --git a/test/gourdinl/c/if.c b/test/gourdinl/c/if.c new file mode 100644 index 00000000..7d2e249a --- /dev/null +++ b/test/gourdinl/c/if.c @@ -0,0 +1,7 @@ +int main(int x) +{ + if (x > 27) + return 11; + else x--; + return x; +} diff --git a/test/gourdinl/c/msb_pos.c b/test/gourdinl/c/msb_pos.c new file mode 100644 index 00000000..f2e7fe09 --- /dev/null +++ b/test/gourdinl/c/msb_pos.c @@ -0,0 +1,20 @@ +/* Function to find the MSB bit position */ +int main(int n) +{ + int i = 0, bit; + while (i < 32) + { + bit = n & 0x80000000; + if (bit == -0x80000000) + { + bit = 1; + } + + if (bit == 1) + break; + + n = n << 1; + i++; + } + return i; +} diff --git a/test/gourdinl/c/power2.c b/test/gourdinl/c/power2.c new file mode 100644 index 00000000..64707df9 --- /dev/null +++ b/test/gourdinl/c/power2.c @@ -0,0 +1,42 @@ +/* power2.c -- Print out powers of 2: 1, 2, 4, 8, .. up to 2^N + */ + +#include <stdio.h> +#define N 16 + +int main(void) { + int n; /* The current exponent */ + int val = 1; /* The current power of 2 */ + + printf("\t n \t 2^n\n"); + printf("\t================\n"); + for (n=0; n<=N; n++) { + printf("\t%3d \t %6d\n", n, val); + val = 2*val; + } + return 0; +} + +/* It prints out : + + n 2^n + ================ + 0 1 + 1 2 + 2 4 + 3 8 + 4 16 + 5 32 + 6 64 + 7 128 + 8 256 + 9 512 + 10 1024 + 11 2048 + 12 4096 + 13 8192 + 14 16384 + 15 32768 + 16 65536 + +*/ diff --git a/test/gourdinl/c/prime.c b/test/gourdinl/c/prime.c new file mode 100644 index 00000000..6c51db32 --- /dev/null +++ b/test/gourdinl/c/prime.c @@ -0,0 +1,23 @@ +/* prime1.c It prompts the user to enter an integer N. It prints out + * if it is a prime or not. If not, it prints out a factor of N. + */ + +#include <stdio.h> + +int main(int n) { + int i; + int flag; + + flag = 1; + for (i=2; (i<(n/2)) && flag; ) { /* May be we do not need to test + values of i greater than the square root of n? */ + if ((n % i) == 0) /* If true n is divisible by i */ + flag = 0; + else + i++; + } + + if (flag) + return 1; + return 0; +} diff --git a/test/gourdinl/c/random.c b/test/gourdinl/c/random.c new file mode 100644 index 00000000..50aa5737 --- /dev/null +++ b/test/gourdinl/c/random.c @@ -0,0 +1,50 @@ +/* Generating random number sequences using the formula (linear congruence) + x[k+1] = (a*x[k] + c)mod m + where a, c, and m are parameters set by the user and passed as command line + parameters together with a seed i.e. x[0] + As a simple example try a=7, c=1, m=13, and seed=5 + A more sophisticated selection would be a=69069, c=0, + m=2^32=4294967296, and seed=31 + It will print out, in a sort of random order, up to m-1 distinct values. + Then it loops. + */ + +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> + +static long seed = 13; +static long a; +static long c; +static long m; + +void random_init(long s) { + if (s != 0) seed = s; +} + +long random() { + seed = (a*seed + c)%m; + return seed; + } + +int main(int argc, char * argv[]) { + if (argc != 5) { + printf("usage: %s a, c, m, seed\n", argv[0]); + return 1; + } + a = atoi(argv[1]); + c = atoi(argv[2]); + m = atoi(argv[3]); + long s = atoi(argv[4]); + random_init(s); + int k; + for (k = 0; k < m-1; k++) { + printf("%8ld", random()); + if (k % 8 == 7) { // after 8 elements go to a new line + printf("\n"); + sleep(1); // sleep for a second + } + } + printf("\n"); + return 0; +} diff --git a/test/gourdinl/c/simple_op.c b/test/gourdinl/c/simple_op.c new file mode 100644 index 00000000..7c43b081 --- /dev/null +++ b/test/gourdinl/c/simple_op.c @@ -0,0 +1,8 @@ +int main(int argc, char ** argv) +{ + int n, m; + n = n + 1; + n = n * 7; + n / (8 - 2); + return n; +} diff --git a/test/gourdinl/c/wloop.c b/test/gourdinl/c/wloop.c new file mode 100644 index 00000000..5ba67419 --- /dev/null +++ b/test/gourdinl/c/wloop.c @@ -0,0 +1,8 @@ +int main(int x) +{ + int y = 4; + int s = 23; + while(s < x) + y << s; + return y; +} |