Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
#include <stdio.h> int main() { int a = 10, b = 2; int *p1, *p2; p1 = &a; p2 = &b; printf("%p %p \n", p1, p2); printf("%d %d \n", *p1, *p2); return 0; } |
#include <stdio.h> void swap(int * q,int * p) { int temp = *p; *p = *q; *q = temp; } int main() { int a = 10, b = 2, x = 3, y = 5; printf("a,b,x,y: %d,%d,%d,%d\n", a, b, x, y); swap(&x, &y); swap(&a, &b); printf("a,b,x,y: %d,%d,%d,%d\n", a, b, x, y); } |
#include <stdio.h> void rectangle(int a, int b, int * area, int * perim); int main() { int x, y; int area, perim; scanf("%d %d", &x, &y); rectangle(x, y, &area, &perim); printf("Area is %d Perimeter is %d\n", area, perim); return 0; } void rectangle(int a,int b,int * area,int * perim) { *area = a * b; *perim = 2 * (a + b); } |
#include <stdio.h> void get_array(int a[], int size); void prt_array(int a[], int size); #define SIZE 10 int main() { int a[SIZE]; get_array(a, SIZE); prt_array(a, SIZE); printf("\n"); return 0; } void get_array(int a[], int size) { int i; for (i = 0; i < size; i++) scanf("%d", &a[i]); } void prt_array(int a[], int size) { int i; for (i = 0; i < size; i++) printf("%d", a[i]); } |
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 5 void fill_array(int array[], int size); void print_array(int array[], int size); void reverse_array1(int array[], int size); /* using for */ void reverse_array2(int * begin, int * end); /* using while */ void reverse_array3(int * begin, int * end); /* using recursion */ void swap(int * a, int * b); int main() { int my_arr[SIZE]; fill_array(my_arr, SIZE); print_array(my_arr, SIZE); /* reverse_array1(my_arr, SIZE); */ reverse_array3(my_arr, my_arr + SIZE - 1); print_array(my_arr, SIZE); return 0; } void reverse_array1(int array[], int size) { int i; for (i = 0; i < size/2; i++) swap(&array[i], &array[size - 1 - i]); } void reverse_array2(int * begin, int * end) { while (begin < end) { swap(begin, end); begin++; end--; } } void reverse_array3(int * begin, int * end) { if (begin < end) { swap(begin, end); reverse_array3(begin + 1, end - 1); } } |
#include <stdio.h> int * find_sub_array(int * array, int size, int * sub_array, int sub_size); int main() { int array1[] = {1, 45, 67, 1001, -19, 67, 89, 1004, -867, 34, 3, -1900, 10029}; int array2[] = {34, 3, -1900}; int * position; position = find_sub_array(array1, sizeof (array1) / sizeof (int), array2, sizeof (array2) / sizeof(int)); printf("array2 appears in array1 starting from place : %d.\n", (position == NULL)? -1 : position - array1 + 1); return 0; } int * find_sub_array(int * array, int size, int * sub_array, int sub_size) { int i, j; for (i = 0; i++ < size; ++array) { for (j = 0; j < sub_size; ++j) if ( *(array + j) != sub_array[j]) break; if (j == sub_size) return array; } return NULL; } |