C-Kurs/(Pointer) Swapping/Musterlösung: Unterschied zwischen den Versionen
< C-Kurs | (Pointer) Swapping
Mutax (Diskussion | Beiträge) K (hat „Ckurs2009/(Pointer) Swapping/Musterlösung“ nach „Ckurs/(Pointer) Swapping/Musterlösung“ verschoben: Aufgaben sind Kurs übergreifend nutzbar) |
Klang (Diskussion | Beiträge) |
||
Zeile 6: | Zeile 6: | ||
void swap(int *x, int *y) { | void swap(int *x, int *y) { | ||
− | int | + | int temp= *x; |
− | *x = *y; | + | *x= *y; |
− | *y = | + | *y= temp; |
} | } | ||
Zeile 35: | Zeile 35: | ||
void swap(int *x, int *y) { | void swap(int *x, int *y) { | ||
− | int | + | int temp= *x; |
− | *x = *y; | + | *x= *y; |
− | *y = | + | *y= temp; |
} | } | ||
+ | |||
void print_int_array(char* name, int* array, int size){ | void print_int_array(char* name, int* array, int size){ | ||
Zeile 45: | Zeile 46: | ||
printf("%s:", name); | printf("%s:", name); | ||
− | for(i= 0; i<size; i++) | + | for(i= 0; i<size; i++){ |
printf(" %i", array[i]); | printf(" %i", array[i]); | ||
+ | } | ||
printf("\n"); | printf("\n"); | ||
} | } | ||
+ | |||
int main(){ | int main(){ | ||
Zeile 56: | Zeile 59: | ||
int b[]= {5, 6, 7, 8}; | int b[]= {5, 6, 7, 8}; | ||
− | + | int length= sizeof(a) / sizeof(int); | |
− | print_int_array("b", b, | + | |
+ | print_int_array("a", a, length); | ||
+ | print_int_array("b", b, length); | ||
int i; | int i; | ||
− | for(i=0; i< | + | for(i=0; i<length; i++){ |
− | swap(&a[i], &b[i]); | + | swap(&a[i], &b[i]); |
+ | } | ||
− | print_int_array("a", a, | + | print_int_array("a", a, length); |
− | print_int_array("b", b, | + | print_int_array("b", b, length); |
return 0; | return 0; | ||
Zeile 77: | Zeile 83: | ||
void swap(int **x, int **y) { | void swap(int **x, int **y) { | ||
− | int* | + | int* temp= *x; |
− | *x = *y; | + | *x= *y; |
− | *y = | + | *y= temp; |
} | } | ||
− | + | ||
+ | |||
int main(){ | int main(){ | ||
− | + | ||
int i= 1; | int i= 1; | ||
int j= 2; | int j= 2; | ||
− | int* | + | int* p1; |
− | + | p1= &i; | |
− | int* | + | int* p2; |
− | + | p2= &j; | |
− | printf(" | + | printf("------------------------------------------\n"); |
− | + | printf("before swap:\n"); | |
− | + | printf("------------------------------------------\n"); | |
− | + | printf(" p1:\t%p\t p2:\t%p\n", p1, p2); | |
− | printf("i: % | + | 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; | return 0; | ||
} | } | ||
− | + | ||
− | |||
4.Teilaufgabe | 4.Teilaufgabe | ||
Zeile 112: | Zeile 128: | ||
void swap(int **x, int **y) { | void swap(int **x, int **y) { | ||
− | int* | + | int* temp= *x; |
− | *x = *y; | + | *x= *y; |
− | *y = t; | + | *y= t; |
} | } | ||
− | + | ||
void print_int_array(char* name, int* array, int size){ | void print_int_array(char* name, int* array, int size){ | ||
Zeile 123: | Zeile 139: | ||
printf("%s:", name); | printf("%s:", name); | ||
− | for(i= 0; i<size; i++) | + | for(i= 0; i<size; i++){ |
printf(" %i", array[i]); | printf(" %i", array[i]); | ||
+ | } | ||
printf("\n"); | printf("\n"); | ||
} | } | ||
− | + | ||
+ | |||
int main(){ | int main(){ | ||
int a[]= {1, 2, 3, 4}; | int a[]= {1, 2, 3, 4}; | ||
int b[]= {5, 6, 7, 8}; | int b[]= {5, 6, 7, 8}; | ||
− | |||
− | |||
− | |||
− | int * | + | int length= sizeof a / sizeof(int); |
− | + | ||
+ | int *p1; | ||
+ | p1 = &a[0]; | ||
− | int * | + | int *p2; |
− | + | p2 = &b[0]; | |
− | print_int_array(" | + | print_int_array("front", p1, length); |
− | print_int_array(" | + | print_int_array("back", p2, length); |
− | swap( | + | swap(&p1, &p2); |
− | print_int_array(" | + | print_int_array("front", p1, length); |
− | print_int_array(" | + | print_int_array("back", p2, length); |
− | |||
− | |||
} | } | ||
Version vom 16. September 2010, 03:12 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; p1= &i; int* p2; 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= t; } 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); int *p1; p1 = &a[0]; int *p2; p2 = &b[0]; print_int_array("front", p1, length); print_int_array("back", p2, length); swap(&p1, &p2); print_int_array("front", p1, length); print_int_array("back", p2, length); }
5. Teilaufgabe
#include <stdio.h> void swap(void **x, void **y) { void *t = *x; *x = *y; *y = t; } 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* pi; pi= &i; int* pj; pj= &j; printf("i: %i j: %i\n", *pi, *pj); swap((void*)&pi, (void*)&pj); printf("i: %i j: %i\n", *pi, *pj); int a[]= {1, 2, 3, 4}; int b[]= {5, 6, 7, 8}; // a and b are constant // alternatively, you can use calloc int *pa; pa = &a[0]; int *pb; pb = &b[0]; print_int_array("a", pa, sizeof(a)/sizeof(int)); print_int_array("b", pb, sizeof(b)/sizeof(int)); swap((void**)&pa, (void**)&pb); print_int_array("a", pa, sizeof(a)/sizeof(int)); print_int_array("b", pb, sizeof(b)/sizeof(int)); float x[]= {1.0, 2.0, 3.0, 4.0}; float y[]= {5.0, 6.0, 7.0, 8.0}; // x and y are constant // alternatively, you can use calloc float *px; px = &x[0]; float *py; py = &y[0]; print_float_array("x", px, sizeof(x)/sizeof(int)); print_float_array("y", py, sizeof(y)/sizeof(int)); swap((void**)&px, (void**)&py); print_float_array("x", px, sizeof(x)/sizeof(int)); print_float_array("y", py, sizeof(y)/sizeof(int)); return 0; }