C-Kurs/(Pointer) Swapping/Musterlösung: Unterschied zwischen den Versionen
< C-Kurs | (Pointer) Swapping
(Die Seite wurde neu angelegt: „ #include <stdio.h> void swap(int *x, int *y) { int t = *x; *x = *y; *y = t; } int main(){ int i= 1; int j= 2; printf("i: ...“) |
PaulG (Diskussion | Beiträge) K (verschob „Ckurs/(Pointer) Swapping/Musterlösung“ nach „C-Kurs/(Pointer) Swapping/Musterlösung“) |
||
(7 dazwischenliegende Versionen von 4 Benutzern werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
+ | 1. Teilaufgabe | ||
+ | |||
#include <stdio.h> | #include <stdio.h> | ||
void swap(int *x, int *y) { | void swap(int *x, int *y) { | ||
+ | |||
+ | int temp= *x; | ||
+ | *x= *y; | ||
+ | *y= temp; | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(){ | ||
+ | |||
+ | |||
+ | int i= 1; | ||
+ | int j= 2; | ||
+ | |||
+ | printf("i: %i j: %i\n", i, j); | ||
+ | |||
+ | swap(&i, &j); | ||
+ | |||
+ | printf("i: %i j: %i\n", i, j); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | 2. Teilaufgabe | ||
− | int | + | #include <stdio.h> |
− | *x = *y; | + | |
− | *y = | + | |
+ | void swap(int *x, int *y) { | ||
+ | |||
+ | int temp= *x; | ||
+ | *x= *y; | ||
+ | *y= temp; | ||
+ | } | ||
+ | |||
+ | |||
+ | void print_int_array(char* name, int* array, int size){ | ||
+ | |||
+ | int i; | ||
+ | |||
+ | printf("%s:", name); | ||
+ | for(i= 0; i<size; i++){ | ||
+ | printf(" %i", array[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
} | } | ||
− | + | ||
int main(){ | int main(){ | ||
+ | |||
+ | int a[]= {1, 2, 3, 4}; | ||
+ | int b[]= {5, 6, 7, 8}; | ||
+ | |||
+ | int length= sizeof(a) / sizeof(int); | ||
+ | |||
+ | print_int_array("a", a, length); | ||
+ | print_int_array("b", b, length); | ||
+ | |||
+ | int i; | ||
+ | for(i=0; i<length; i++){ | ||
+ | swap(&a[i], &b[i]); | ||
+ | } | ||
+ | |||
+ | print_int_array("a", a, length); | ||
+ | print_int_array("b", b, length); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | |||
+ | 3.Teilaufgabe | ||
+ | |||
+ | #include <stdio.h> | ||
+ | |||
+ | |||
+ | void swap(int **x, int **y) { | ||
+ | |||
+ | int* temp= *x; | ||
+ | *x= *y; | ||
+ | *y= temp; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | int main(){ | ||
+ | |||
int i= 1; | int i= 1; | ||
int j= 2; | int j= 2; | ||
+ | |||
+ | int* p1= &i; | ||
+ | int* p2= &j; | ||
+ | |||
+ | printf("------------------------------------------\n"); | ||
+ | printf("before swap:\n"); | ||
+ | printf("------------------------------------------\n"); | ||
+ | printf(" p1:\t%p\t p2:\t%p\n", p1, p2); | ||
+ | printf("*p1:\t%10i\t*p2:\t%10i\n", *p1, *p2); | ||
+ | printf(" i:\t%10i\t j:\t%10i\n", i, j); | ||
+ | |||
+ | swap(&p1, &p2); | ||
+ | |||
+ | printf("------------------------------------------\n"); | ||
+ | printf("after swap:\n"); | ||
+ | printf("------------------------------------------\n"); | ||
+ | printf(" p1:\t%p\t p2:\t%p\n", p1, p2); | ||
+ | printf("*p1:\t%10i\t*p2:\t%10i\n", *p1, *p2); | ||
+ | printf(" i:\t%10i\t j:\t%10i\n", i, j); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | 4.Teilaufgabe | ||
+ | |||
+ | #include <stdio.h> | ||
+ | |||
+ | |||
+ | void swap(int **x, int **y) { | ||
+ | |||
+ | int* temp= *x; | ||
+ | *x= *y; | ||
+ | *y= temp; | ||
+ | } | ||
+ | |||
+ | |||
+ | void print_int_array(char* name, int* array, int size){ | ||
+ | |||
+ | int i; | ||
+ | |||
+ | printf("%s:", name); | ||
+ | for(i= 0; i<size; i++){ | ||
+ | printf(" %i", array[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(){ | ||
+ | |||
+ | int a[]= {1, 2, 3, 4}; | ||
+ | int b[]= {5, 6, 7, 8}; | ||
+ | |||
+ | int length= sizeof(a) / sizeof(int); | ||
+ | |||
+ | // a and b are constant | ||
+ | // alternatively, you can use calloc | ||
+ | |||
+ | int *front= &a[0]; | ||
+ | int *back= &b[0]; | ||
+ | |||
+ | print_int_array("front", front, length); | ||
+ | print_int_array("back ", back, length); | ||
+ | |||
+ | swap(&front, &back); | ||
+ | |||
+ | print_int_array("front", front, length); | ||
+ | print_int_array("back ", back, length); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
− | |||
− | + | 5. Teilaufgabe | |
− | printf("i: %i j: %i\n", i, | + | #include <stdio.h> |
+ | |||
+ | |||
+ | void swap(void **x, void **y) { | ||
+ | |||
+ | void *temp = *x; | ||
+ | *x = *y; | ||
+ | *y = temp; | ||
+ | } | ||
+ | |||
+ | |||
+ | void print_int_array(char* name, int* array, int size){ | ||
+ | |||
+ | int i; | ||
+ | |||
+ | printf("%s:", name); | ||
+ | for(i= 0; i<size; i++){ | ||
+ | printf(" %i", array[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | } | ||
+ | |||
+ | |||
+ | void print_float_array(char* name, float* array, int size){ | ||
+ | |||
+ | int i; | ||
+ | |||
+ | printf("%s:", name); | ||
+ | for(i= 0; i<size; i++){ | ||
+ | printf(" %f", array[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(){ | ||
+ | |||
+ | |||
+ | int i= 1; | ||
+ | int j= 2; | ||
+ | |||
+ | int* p1= &i; | ||
+ | int* p2= &j; | ||
+ | |||
+ | printf("i: %i j: %i\n", *p1, *p2); | ||
+ | |||
+ | swap((void*)&p1, (void*)&p2); | ||
+ | |||
+ | printf("i: %i j: %i\n", *p1, *p2); | ||
+ | |||
+ | int a[]= {1, 2, 3, 4}; | ||
+ | int b[]= {5, 6, 7, 8}; | ||
+ | |||
+ | int length= sizeof(a) / sizeof(int); | ||
+ | |||
+ | // a and b are constant | ||
+ | // alternatively, you can use calloc | ||
+ | |||
+ | p1= &a[0]; | ||
+ | p2= &b[0]; | ||
+ | |||
+ | print_int_array("a", p1, length); | ||
+ | print_int_array("b", p2, length); | ||
+ | |||
+ | swap((void**)&p1, (void**)&p2); | ||
+ | |||
+ | print_int_array("a", p1, length); | ||
+ | print_int_array("b", p2, length); | ||
+ | |||
+ | float c[]= {1.0, 2.0, 3.0, 4.0}; | ||
+ | float d[]= {5.0, 6.0, 7.0, 8.0}; | ||
+ | |||
+ | length= sizeof(c) / sizeof(int); | ||
+ | |||
+ | // c and d are constant | ||
+ | // alternatively, you can use calloc | ||
+ | |||
+ | float *p3= &c[0]; | ||
+ | float *p4= &d[0]; | ||
+ | |||
+ | print_float_array("x", p3, length); | ||
+ | print_float_array("y", p4, length); | ||
+ | |||
+ | swap((void**)&p3, (void**)&p4); | ||
+ | |||
+ | print_float_array("x", p3, length); | ||
+ | print_float_array("y", p4, length); | ||
+ | |||
+ | return 0; | ||
} | } |
Aktuelle Version vom 5. März 2013, 17:37 Uhr
1. Teilaufgabe
#include <stdio.h> void swap(int *x, int *y) { int temp= *x; *x= *y; *y= temp; } int main(){ int i= 1; int j= 2; printf("i: %i j: %i\n", i, j); swap(&i, &j); printf("i: %i j: %i\n", i, j); return 0; }
2. Teilaufgabe
#include <stdio.h> void swap(int *x, int *y) { int temp= *x; *x= *y; *y= temp; } void print_int_array(char* name, int* array, int size){ int i; printf("%s:", name); for(i= 0; i<size; i++){ printf(" %i", array[i]); } printf("\n"); } int main(){ int a[]= {1, 2, 3, 4}; int b[]= {5, 6, 7, 8}; int length= sizeof(a) / sizeof(int); print_int_array("a", a, length); print_int_array("b", b, length); int i; for(i=0; i<length; i++){ swap(&a[i], &b[i]); } print_int_array("a", a, length); print_int_array("b", b, length); return 0; }
3.Teilaufgabe
#include <stdio.h> void swap(int **x, int **y) { int* temp= *x; *x= *y; *y= temp; } int main(){ int i= 1; int j= 2; int* p1= &i; int* p2= &j; printf("------------------------------------------\n"); printf("before swap:\n"); printf("------------------------------------------\n"); printf(" p1:\t%p\t p2:\t%p\n", p1, p2); printf("*p1:\t%10i\t*p2:\t%10i\n", *p1, *p2); printf(" i:\t%10i\t j:\t%10i\n", i, j); swap(&p1, &p2); printf("------------------------------------------\n"); printf("after swap:\n"); printf("------------------------------------------\n"); printf(" p1:\t%p\t p2:\t%p\n", p1, p2); printf("*p1:\t%10i\t*p2:\t%10i\n", *p1, *p2); printf(" i:\t%10i\t j:\t%10i\n", i, j); return 0; }
4.Teilaufgabe
#include <stdio.h> void swap(int **x, int **y) { int* temp= *x; *x= *y; *y= temp; } void print_int_array(char* name, int* array, int size){ int i; printf("%s:", name); for(i= 0; i<size; i++){ printf(" %i", array[i]); } printf("\n"); } int main(){ int a[]= {1, 2, 3, 4}; int b[]= {5, 6, 7, 8}; int length= sizeof(a) / sizeof(int); // a and b are constant // alternatively, you can use calloc int *front= &a[0]; int *back= &b[0]; print_int_array("front", front, length); print_int_array("back ", back, length); swap(&front, &back); print_int_array("front", front, length); print_int_array("back ", back, length); return 0; }
5. Teilaufgabe
#include <stdio.h> void swap(void **x, void **y) { void *temp = *x; *x = *y; *y = temp; } void print_int_array(char* name, int* array, int size){ int i; printf("%s:", name); for(i= 0; i<size; i++){ printf(" %i", array[i]); } printf("\n"); } void print_float_array(char* name, float* array, int size){ int i; printf("%s:", name); for(i= 0; i<size; i++){ printf(" %f", array[i]); } printf("\n"); } int main(){ int i= 1; int j= 2; int* p1= &i; int* p2= &j; printf("i: %i j: %i\n", *p1, *p2); swap((void*)&p1, (void*)&p2); printf("i: %i j: %i\n", *p1, *p2); int a[]= {1, 2, 3, 4}; int b[]= {5, 6, 7, 8}; int length= sizeof(a) / sizeof(int); // a and b are constant // alternatively, you can use calloc p1= &a[0]; p2= &b[0]; print_int_array("a", p1, length); print_int_array("b", p2, length); swap((void**)&p1, (void**)&p2); print_int_array("a", p1, length); print_int_array("b", p2, length); float c[]= {1.0, 2.0, 3.0, 4.0}; float d[]= {5.0, 6.0, 7.0, 8.0}; length= sizeof(c) / sizeof(int); // c and d are constant // alternatively, you can use calloc float *p3= &c[0]; float *p4= &d[0]; print_float_array("x", p3, length); print_float_array("y", p4, length); swap((void**)&p3, (void**)&p4); print_float_array("x", p3, length); print_float_array("y", p4, length); return 0; }