JakubKaczmarek_123

Funkcje zadania

Jan 29th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. ZADANIE 1
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. float suma(float a, float b);
  7. int main(){
  8. cout << "suma to: " << suma(10.5, 19.2) << endl;
  9. cout << "suma to: " << suma(10.2, 16.2) << endl;
  10. cout << "suma to: " << suma(10.7, 11.2) << endl;
  11. cout << "suma to: " << suma(10.9, 12.2) << endl;
  12. }
  13. float suma(float a, float b){
  14. return a+b;
  15. }
  16.  
  17. ZADANIE 2
  18.  
  19. #include <iostream>
  20. #include <math.h>
  21. using namespace std;
  22. int WARTOSC_BEZWZGLEDNA(int a);
  23. int main(){
  24. cout << "wartosc bezwzgledna to: " << WARTOSC_BEZWZGLEDNA(10) << endl;
  25. cout << "wartosc bezwzgledna to: " << WARTOSC_BEZWZGLEDNA(16) << endl;
  26. cout << "wartosc bezwzgledna to: " << WARTOSC_BEZWZGLEDNA(-16) << endl;
  27. cout << "wartosc bezwzgledna to: " << WARTOSC_BEZWZGLEDNA(-20) << endl;
  28. }
  29. int WARTOSC_BEZWZGLEDNA(int a){
  30. return abs(a);
  31. }
  32.  
  33. ZADANIE 3
  34.  
  35. #include <iostream>
  36. #include <math.h>
  37. using namespace std;
  38. bool CZY_PARZYSTA(int a);
  39. int main(){
  40. cout << CZY_PARZYSTA(4) << endl;
  41. cout << CZY_PARZYSTA(5) << endl;
  42. cout << CZY_PARZYSTA(7) << endl;
  43. cout << CZY_PARZYSTA(190) << endl;
  44.  
  45. }
  46. bool CZY_PARZYSTA(int a){
  47. if (a % 2 == 0) return true;
  48. else return false;
  49. }
  50.  
  51. ZADANIE 4
  52.  
  53. #include <iostream>
  54. #include <math.h>
  55. using namespace std;
  56. void SZLACZEK(int a, char b);
  57. int main(){
  58. SZLACZEK(10, 'f');
  59. cout << endl;
  60. SZLACZEK(5, 's');
  61. cout << endl;
  62. SZLACZEK(15, '*');
  63. cout << endl;
  64. SZLACZEK(29, '-');
  65. }
  66. void SZLACZEK(int a, char b){
  67. for (int i; i < a; i++){
  68.     cout << b;
  69. }
  70. }
  71.  
  72. ZADANIE 5
  73.  
  74. #include <iostream>
  75. #include <math.h>
  76. using namespace std;
  77. int POTEGA(int x, int y);
  78. int main(){
  79. cout << POTEGA(2, 6) << endl;
  80. cout << POTEGA(2, 2) << endl;
  81. cout << POTEGA(4, 3) << endl;
  82. cout << POTEGA(4, 5) << endl;
  83. }
  84. int POTEGA(int x, int y){
  85.     int z = x;
  86. for (int i = 1; i < y; i++) z *= x;
  87. return z;
  88. }
  89.  
  90. ZADANIE 6
  91.  
  92. #include <iostream>
  93. #include <math.h>
  94. using namespace std;
  95. float OBWOD_TROJKATA(float x, float y, float z);
  96. int main(){
  97.     float x, y, z;
  98.     cout << "wprowadz boki trojkata: " << endl;
  99.     cin >> x >> y >> z;
  100. if (OBWOD_TROJKATA(x, y, z) == -1) cout << "nie da sie stworzyc trojkata!" << endl;
  101. else cout << "obwod trojkata to: " << OBWOD_TROJKATA(x, y, z) << endl;
  102. }
  103. float OBWOD_TROJKATA(float x, float y, float z){
  104.     if (x<y+z && y<x+z && z<x+y) return x+y+z;
  105.     else return -1;
  106. }
  107.  
  108. ZADANIE 7
  109.  
  110. #include <iostream>
  111. #include <math.h>
  112. using namespace std;
  113. int SILNIA(int a);
  114. int main(){
  115. cout << SILNIA(0) << endl;
  116. cout << SILNIA(1) << endl;
  117. cout << SILNIA(2) << endl;
  118. cout << SILNIA(3) << endl;
  119. cout << SILNIA(4) << endl;
  120. cout << SILNIA(5) << endl;
  121. cout << SILNIA(6) << endl;
  122. cout << SILNIA(7) << endl;
  123. cout << SILNIA(8) << endl;
  124.  
  125. }
  126. int SILNIA(int a){
  127. int b = 1;
  128. for (int i = 2; i <= a; i++) b *= i;
  129. return b;
  130. }
  131.  
  132. ZADANIE 8
  133.  
  134. #include <iostream>
  135. #include <math.h>
  136. using namespace std;
  137. int MAX(int a, int b, int c);
  138. int main(){
  139. cout << MAX(1,2,3) << endl;
  140. cout << MAX(4,2,3) << endl;
  141. cout << MAX(1,2,3) << endl;
  142. cout << MAX(1,190,6) << endl;
  143. }
  144.  
  145. int MAX(int a, int b, int c){
  146. return max(a, max(b,c));
  147. }
  148.  
  149. ZADANIE 9
  150.  
  151. #include <iostream>
  152. #include <math.h>
  153. using namespace std;
  154. int DZIELNIKI(int n);
  155. int main(){
  156. DZIELNIKI(4);
  157. DZIELNIKI(40);
  158. DZIELNIKI(51);
  159. DZIELNIKI(412);
  160. }
  161. int DZIELNIKI(int n){
  162.     for (int i = 1; i <= n; i++) if (n%i == 0) cout << i << endl;
  163. }
  164.  
  165. ZADANIE 10
  166.  
  167. #include <iostream>
  168. #include <math.h>
  169. using namespace std;
  170. float ILORAZ(float a, float b);
  171. int main(){
  172. cout << "wprowadz 2 liczby" << endl;
  173. int a, b;
  174. cin >> a >> b;
  175. if (ILORAZ(a,b) == -1) cout << "Nie dziel przez 0" << endl;
  176. else if (ILORAZ(a, b) == -2) cout << "Nie wprowadzaj liczb ujemnych" << endl;
  177. else cout << "wynik to: " << ILORAZ(a,b) << endl;
  178. }
  179. float ILORAZ(float a, float b){
  180.     if (b == 0) return -1;
  181.     else if (a < 0 || b < 0) return -2;
  182.     else return a/b;
  183. }
  184.  
  185. ZADANIE 11
  186.  
  187. #include <iostream>
  188. #include <math.h>
  189. using namespace std;
  190. int PODZIELNOSC(int a, int b);
  191. int main(){
  192. int a, b;
  193. cout << "wprowadz 2 liczby: "<<endl;
  194. cin >> a >> b;
  195. cout << endl;
  196. cout << "Twoje liczby to: ";
  197. PODZIELNOSC(a,b);
  198. }
  199. int PODZIELNOSC(int a, int b){
  200. for (int i = pow(10, a-1); i < pow(10, a); i++) if (i%b == 0) cout << i << " ";
  201. }
  202.  
  203. ZADANIE 12
  204.  
  205. #include <iostream>
  206. #include <math.h>
  207. using namespace std;
  208. int LICZBA_CYFR(int a);
  209. int main(){
  210. cout << LICZBA_CYFR(10)<<endl;
  211. cout << LICZBA_CYFR(101)<<endl;
  212. cout << LICZBA_CYFR(10123241)<<endl;
  213. cout << LICZBA_CYFR(0)<<endl;
  214. cout << LICZBA_CYFR(1)<<endl;
  215. }
  216. int LICZBA_CYFR(int a){
  217.     if (a == 0) return 1;
  218.     int i = 0;
  219. while (a >= pow(10, i)) i++;
  220. return i;
  221. }
  222.  
  223. ZADANIE 13
  224.  
  225. #include <iostream>
  226. #include <math.h>
  227. #include <string>
  228. using namespace std;
  229. int SUMA_CYFR(int a);
  230. int main(){
  231. cout << "wprowadz liczbe: " << endl;
  232. int a;
  233. cin >> a;
  234. int wynik = SUMA_CYFR(a);
  235. while (wynik >= 10) wynik = SUMA_CYFR(wynik);
  236. cout << "wynik to: " << wynik << endl;
  237. }
  238.  
  239.  
  240. int SUMA_CYFR(int a){
  241. string b = to_string(a);
  242. int c = 0;
  243. for (int i = 0; i < b.length(); i++) c += b[i] - '0';
  244. return c;
  245. }
  246.  
  247. ZADANIE 14 I 15
  248.  
  249. #include <iostream>
  250. #include <math.h>
  251. #include <string>
  252. using namespace std;
  253. float POLE_TROJKATA(float a, float b);
  254. float POLE_TROJKATA(float x1, float y1, float x2, float y2, float x3, float y3);
  255. int main(){
  256. cout << POLE_TROJKATA(1, 3) << endl;
  257. cout << POLE_TROJKATA(2, 6) << endl;
  258. cout << POLE_TROJKATA(1,1,5,2,2,5) << endl;
  259. cout << POLE_TROJKATA(0,0,3,0,0,3) << endl;
  260. }
  261.  
  262.  
  263. float POLE_TROJKATA(float a, float b){
  264. return a*b/2;
  265. }
  266. float POLE_TROJKATA(float x1, float y1, float x2, float y2, float x3, float y3){
  267.     float a = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
  268.     float b = sqrt(pow(x2 - x3, 2) + pow(y2 - y3, 2));
  269.     float c = sqrt(pow(x3 - x1, 2) + pow(y3 - y1, 2));
  270.     float o = (a+b+c)/2;
  271.     return sqrt(o*(o-a)*(o-b)*(o-c));
  272. }
Advertisement
Add Comment
Please, Sign In to add comment