This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
dydaktyka:cprog:2016:revision_1-solutions [2015/11/08 19:45] 127.0.0.1 edycja zewnętrzna |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Zadania powtórkowe (1) - rozwiązania ====== | ||
- | Laboratorium: [[dydaktyka:cprog:2015:revision_1|Zadania powtórkowe (1)]] | ||
- | ===== Zadanie 1 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int ile_pierwiastkow(int a, int b, int c) { | ||
- | int delta = b * b - 4 * a * c; | ||
- | |||
- | if (delta < 0) { | ||
- | return 0; | ||
- | } else if (delta == 0) { | ||
- | return 1; | ||
- | } else { | ||
- | return 2; | ||
- | } | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double a, b, c; | ||
- | |||
- | printf("Podaj wspolczynniki a, b i c: "); | ||
- | scanf("%lf %lf %lf", &a, &b, &c); | ||
- | |||
- | printf("Ilosc pierwiastkow: %d\n", ile_pierwiastkow(a, b, c)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 2 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int czy_kwadrat(int n) { | ||
- | int i; | ||
- | |||
- | for (i = 1; i < n; i = i + 1) { | ||
- | if (i*i == n) { | ||
- | return 1; | ||
- | } | ||
- | } | ||
- | |||
- | return 0; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | int n; | ||
- | |||
- | n = 4; | ||
- | printf("Czy %d jest kwadratem pewnej liczby? - %d\n", n, czy_kwadrat(n)); | ||
- | |||
- | n = 5; | ||
- | printf("Czy %d jest kwadratem pewnej liczby? - %d\n", n, czy_kwadrat(n)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 3 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int wieksza(int a, int b) { | ||
- | if (a > b) { | ||
- | return a; | ||
- | } | ||
- | return b; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | int p = 4; | ||
- | int q = 7; | ||
- | |||
- | printf("Liczba wieksza z %d i %d to: %d\n", p, q, wieksza(p, q)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 4 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | double srednia(double a, double b, double c) { | ||
- | return (a + b + c) / 3; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double p = 2; | ||
- | double q = 4; | ||
- | double r = 9; | ||
- | |||
- | printf("Srednia z %f, %f i %f wynosi %f\n", p, q, r, srednia(p, q, r)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 5 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | double oblicz(double x) | ||
- | { | ||
- | return (x * x * x + 3 * x) / (x * x + 6); | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | double x = 2.0; | ||
- | |||
- | printf("y(%f) = %f\n", x, oblicz(x)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 6 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | void czy_zbuduje_trojkat(int a, int b, int c) | ||
- | { | ||
- | printf("Z patykow dlugosci %d, %d i %d ", a, b, c); | ||
- | if (a < b + c && b < a + c && c < a + b) { | ||
- | printf(" da sie zbudowac trojkat.\n"); | ||
- | } else { | ||
- | printf(" nie da sie zbudowac trojkata.\n"); | ||
- | } | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | czy_zbuduje_trojkat(1, 1, 2); | ||
- | czy_zbuduje_trojkat(3, 4, 5); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 7 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int silnia(int n) | ||
- | { | ||
- | int wynik = 1; | ||
- | |||
- | while (n > 1) { | ||
- | wynik = wynik * n; | ||
- | n = n - 1; | ||
- | } | ||
- | |||
- | return wynik; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | int n; | ||
- | |||
- | n = 3; | ||
- | printf("%d! = %d\n", n, silnia(n)); | ||
- | |||
- | n = 5; | ||
- | printf("%d! = %d\n", n, silnia(n)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 8 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | int suma_przedzialu(int a, int b) | ||
- | { | ||
- | int suma = 0; | ||
- | int i; | ||
- | |||
- | for (i = a; i <= b; i = i + 1) { | ||
- | suma = suma + i; | ||
- | } | ||
- | |||
- | return suma; | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | int a = 3; | ||
- | int b = 5; | ||
- | |||
- | printf("SUMA[%d, %d] = %d\n", a, b, suma_przedzialu(a, b)); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== Zadanie 9 ===== | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <stdlib.h> | ||
- | |||
- | void wypisz_wzor(int ilosc_wierszy, int ilosc_znakow_w_wierszu) | ||
- | { | ||
- | int i, j; | ||
- | |||
- | for (i = 0; i < ilosc_wierszy; i = i + 1) { | ||
- | for (j = 0; j < ilosc_znakow_w_wierszu; j = j + 1) { | ||
- | printf("$"); | ||
- | } | ||
- | |||
- | printf("\n"); | ||
- | } | ||
- | } | ||
- | |||
- | int main() | ||
- | { | ||
- | wypisz_wzor(1, 6); | ||
- | printf("\n"); | ||
- | wypisz_wzor(3, 5); | ||
- | |||
- | return 0; | ||
- | } | ||
- | </code> |