Search
#include <stdlib.h> int main() { // volani malloc(pocet bytu) vrati adresu v dynamicke pameti, pokud to bylo uspesne volani // adresa je datoveho typu void*, je vhodné ji přetypovat int *a, *b; const int n = 4; a = (int *)malloc(n*sizeof(int)); b = (int *)calloc(n, sizeof(int)); if (a == NULL) { // kontrola, zda se podařilo alokovat potřebnou pamět, měla by být vždy // zde chybí kontrola pole b printf("nedarilo ze alokovat!\n"); return 100; } else { printf("adresa alokovaneho prostoru: %p\n", a); for (int i = 0; i < n; i++) *(a+i) = i; for (int i = 0; i < n; i++) printf("a[%d]=%d\n", i, a[i]); for (int i = 0; i < n; i++) printf("b[%d]=%d\n", i, b[i]); free(a); free(b); } return 0; }
3 3.14 5.34 8.33
$ ./a.out < in.txt
#include <stdlib.h> #include <stdio.h> int main() { int n, m = 0; float b; scanf("%d", &n); float *p = (float *)malloc(n*sizeof(float)); for (int cnt=0;cnt<n;cnt++){ scanf("%f", &b); p[cnt]= b; cnt++; printf("%2.2f\n", b); } printf("pole: "); for (int i = 0; i < n; i++) { printf("%2.2f ", p[i]); } printf("\n"); free(p); return 0; }
#include <stdlib.h> #include <stdio.h> #define swap(a,b) {int t; t=a; a=b; b=t;} void pole_print (int * a, int velikost) { for (int i = 0; i < velikost; i++) { printf("%i ", a[i]); } printf("\n"); } void pole_sort (int * a, int velikost) { int i, j, swapped; for (j = 0; j < velikost; j++) { swapped = 0; for (i = 0; i < velikost - 1; i++) { if (a[i] > a[i+1]) { swap(a[i], a[i+1]) swapped = 1; /* int tmp = a[i]; a[i] = a[i+1]; a[i+1] = tmp; */ } } if (swapped == 0) { printf("%i/%i\n", j, velikost); return; } } } int main() { int a, i = 0; int *p = (int *)malloc(sizeof(int)); while (scanf("%i", &a) > 0) { // printf("%i ", a); p[i++] = a; p = realloc(p, (i+1)*sizeof(int)); } printf("i = %i\n", i); pole_print (p, i); pole_sort (p, i); pole_print (p, i); free (p); return 0; }