Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

6 - Funkce, ukazatele, pole

1. úloha

#include <stdio.h>
 
int pole_nacteni (int *pole, int velikost)
{
        for (int i = 0; i < velikost; i++)
        {
                if (scanf("%i", &pole[i]) != 1) return 1;
        }
        return 0;
}
 
void pole_tisk (int pole[], int velikost)
{
        for (int i = 0; i < velikost; i++)
        {
                printf("%i ", *(pole + i));
        }
        printf("\n");
}
 
void pole_min_max (int *pole, int *min, int *max, int velikost)
{
        *min = 1000;
        *max = 0;
 
        for (int i = 0; i < velikost; i++)
        {
                if (pole[i] > *max) *max = pole[i];
                if (pole[i] < *min) *min = pole[i];
        }
}
 
void pole_skalar (int *pole, int skalar, int velikost)
{
        int a, b, s;
 
        pole_min_max (pole, &a, &b, velikost);
 
        for (int i = 0; i < velikost; i++)
        {
                s = pole[i] + skalar;
                pole[i] = (s > b) ? a + (s - b) : s;
        }
}
 
int main(void)
{
        int N = 0, ret = 0;
        int *n = &N;
 
        if (scanf("%i", n) != 1)
        {
                fprintf(stderr, "chyba vstupu\n");
                return 100;
        }
 
        if (N > 0)
        {
                int a[N];
 
                if (pole_nacteni (a, N))
                {
                        fprintf(stderr, "chyba nacitani\n");
                        return 101;
                }
                pole_tisk (a, N);
                pole_skalar (a, 2, N);
                pole_tisk(a, N);
        }
        else
        {
                fprintf(stderr, "pozadovano pole nulove delky\n");
                return 102;
        }
 
        return 0;
}

2. úloha

#include <stdio.h>
 
typedef struct {float re, im;} komplex;
 
void swap1 (komplex *a, komplex *b)
{
        komplex tmp = {.re = a->re, .im = a->im};
        a = b;
        b = tmp;
}
 
void swap2 (komplex *a, komplex *b)
{
        a->re += b->re;
        b->re = a->re - b->re;
        a->re = a->re - b->re;
 
        a->im += b->im;
        b->im = a->im - b->im;
        a->im = a->im - b->im;
}
 
void print (komplex a, char nazev)
{
        printf ("%c = %.2f%+.2f\n", nazev, a.re, a.im);
}
 
int main(void)
{
        komplex x = {.re = 2.2, .im = 3.3}, y = {0.0, 0.0};
 
        printf ("-- pocatecni hodnoty promennych --\n");
        print (x, 'x');
        print (y, 'y');
        printf ("-- swap 1 ------------------------\n");
        swap1 (&x, &y);
        print (x, 'x');
        print (y, 'y');
        printf ("-- swap 2 ------------------------\n");
        swap2 (&x, &y);
        print (x, 'x');
        print (y, 'y');
 
        return 0;
}

courses/b0b99prpa/solutions/lab06.txt · Last modified: 2019/12/05 08:11 by viteks