This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
dydaktyka:cprog:2016:test1-solutions [2016/12/01 10:29] pkleczek [Zadanie 2] |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~NOTRANS~~ | ||
- | |||
- | ====== Kolokwium 1 – rozwiązania ====== | ||
- | |||
- | ===== Grupa A ===== | ||
- | |||
- | ==== Zadanie 1 ==== | ||
- | |||
- | <code c test1a-1.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int main() | ||
- | { | ||
- | int rok; | ||
- | |||
- | printf("Podaj rok: "); | ||
- | scanf("%d", &rok); | ||
- | |||
- | if ((rok % 4 == 0 && rok % 100 != 0) || (rok % 400 == 0)) { | ||
- | printf("Rok przestepny\n"); | ||
- | } else { | ||
- | printf("Rok nieprzestepny\n"); | ||
- | } | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== Zadanie 2 ==== | ||
- | |||
- | <code c test1a-2.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | // Funkcja nazywa sie my_abs() aby nie powodowac konfliktu | ||
- | // z wbudowana funkcja abs() z bilbioteki math.h | ||
- | double my_abs(double x) { | ||
- | if (x >= 0) { | ||
- | return x; | ||
- | } else { | ||
- | return -x; | ||
- | } | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double x; | ||
- | |||
- | x = 3; | ||
- | printf("abs(%lf) = %lf\n", x, my_abs(x)); | ||
- | |||
- | x = -5; | ||
- | printf("abs(%lf) = %lf\n", x, my_abs(x)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | |||
- | </code> | ||
- | |||
- | ==== Zadanie 3 ==== | ||
- | |||
- | <code c test1a-3.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | double potega(double a, int n) { | ||
- | double wynik = 1; | ||
- | |||
- | // wartosc dla ktorej: wynik = a^i | ||
- | int i; | ||
- | | ||
- | for (i = 0; i < n; i = i + 1) { | ||
- | wynik = wynik * a; | ||
- | } | ||
- | |||
- | return wynik; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double a; | ||
- | int n; | ||
- | |||
- | a = 3; | ||
- | n = 2; | ||
- | printf("%f ^ %d = %f\n", a, n, potega(a, n)); | ||
- | |||
- | a = 2.7; | ||
- | n = 3; | ||
- | printf("%f ^ %d = %f\n", a, n, potega(a, n)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | ==== Zadanie 4 ==== | ||
- | |||
- | <code c test1a-4.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | // Funkcja tylko wypisuje, a nie zwraca wartosci | ||
- | // - zatem jest typu `void` | ||
- | void iks(int n) { | ||
- | // Indeks aktualnie wyswtetlanego wiersza | ||
- | int r; | ||
- | // Indeks aktualnie wyswtetlanej pozycji w wierszu | ||
- | int c; | ||
- | |||
- | for (r = 1; r <= n; r = r + 1) { | ||
- | for (c = 1; c <= n; c = c + 1) { | ||
- | if (r == c || r + c == n + 1) { | ||
- | printf("X"); | ||
- | } else { | ||
- | printf(" "); | ||
- | } | ||
- | } | ||
- | printf("\n"); | ||
- | } | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | iks(4); | ||
- | |||
- | printf("\n\n"); | ||
- | |||
- | iks(5); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | ===== Grupa B ===== | ||
- | |||
- | ==== Zadanie 1 ==== | ||
- | |||
- | <code c test1b-1.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int main() | ||
- | { | ||
- | int q; | ||
- | |||
- | printf("Podaj q: "); | ||
- | scanf("%d", &q); | ||
- | |||
- | if (20 <= q && q < 40 && q % 3 == 0) { | ||
- | printf("q spelnia zaleznosc\n"); | ||
- | } else { | ||
- | printf("q nie spelnia zaleznosci\n"); | ||
- | } | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== Zadanie 2 ==== | ||
- | |||
- | <code c test1b-2.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | double zeroCut(double x) { | ||
- | if (x >= 0) { | ||
- | return x; | ||
- | } else { | ||
- | return 0; | ||
- | } | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double x; | ||
- | |||
- | x = 3; | ||
- | printf("max(%lf, 0) = %lf\n", x, zeroCut(x)); | ||
- | |||
- | x = -5; | ||
- | printf("max(%lf, 0) = %lf\n", x, zeroCut(x)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ==== Zadanie 3 ==== | ||
- | |||
- | <code c test1b-3.c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | double potega(double a, int n) { | ||
- | double wynik = 1; | ||
- | |||
- | // wartosc dla ktorej: wynik = a^i | ||
- | int i = 0; | ||
- | |||
- | while (i < n) { | ||
- | wynik = wynik * a; | ||
- | i = i + 1; | ||
- | } | ||
- | |||
- | return wynik; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double a; | ||
- | int n; | ||
- | |||
- | a = 3; | ||
- | n = 2; | ||
- | printf("%f ^ %d = %f\n", a, n, potega(a, n)); | ||
- | |||
- | a = 2.7; | ||
- | n = 3; | ||
- | printf("%f ^ %d = %f\n", a, n, potega(a, n)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||