Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.33 KB | None | 0 0
  1. ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]#include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. int sum_greater(const int a, const int b, const int c);
  8. int find_first_A(const char string[]);
  9. char last_letter(const char string[]);
  10. int min_2d(const int size, const int array[][size]);
  11. int is_vowel(const char c);
  12. int is_hex_digit(const char c);
  13. int last(const int size, const int array[]);
  14. int sum_positives(const int size, const int array[]);
  15. int count_zeroes_2d(const int size, int array[][size]);
  16. int n_fib(const int n);
  17. int longest_row(const int rows, const int cols, char array[rows][cols]);
  18. char last_special(const char string[]);
  19. int negative_even(const int number);
  20. void change_whites(char string[]);
  21. void sort_desc(int array[], const int size);
  22. int is_won(const char a, const char b, const char c);
  23. int direction_correction(const int degree);
  24. int sum_digits(const int n);
  25. int count_negative_evens(const int size, const int array[]);
  26. int is_in_array(const int num, const int size, const int array[]);
  27. int leap_year(const int year);
  28. void digits_sum_sequence(const int size, int array[]);
  29. void swap_sign_2d(const int size , int array[][size]);
  30. void swap_case_2d(const int rows,const int cols, char strings[][cols]);
  31. int vowels_count_2d(const int rows, const int cols, char strings[][cols]);
  32. int is_in_array_2d(const int num, const int size, int array[][size]);
  33. int binary_num(const int num);
  34. int is_special(const char c);
  35. int check_SPZ(const char spz[]);
  36. void fib_2_array(const int size, int array[]);
  37. void string_to_upper(char string[]);
  38. int karel_asleep(const char position);
  39.  
  40. int main(){
  41.  
  42. printf("%d\n", sum_greater(-1, 3, -2));
  43. printf("%d\n", find_first_A("Tomorrow afternoon"));
  44.  
  45. printf("%c %c\n", last_letter("Once upon a time...."), last_letter("$a_b_c_d_1_2_3_4$"));
  46.  
  47. int array[2][2] = { {1, 2}, {0, -3} };
  48. printf("%d\n", min_2d(2, array));
  49.  
  50. printf("%d %d %d\n", is_vowel('b'), is_vowel('A'), is_vowel('a'));
  51.  
  52. printf("%d %d\n", is_hex_digit('a'), is_hex_digit('F'));
  53. printf("%d %d\n", is_hex_digit('h'), is_hex_digit('3'));
  54.  
  55. int arrayA[3] = {1,2,3};
  56. printf("%d\n", last(3, arrayA));
  57.  
  58. int arrayB[] = {0,1,-2,3};
  59. printf("%d\n", sum_positives(4, arrayB));
  60.  
  61. int arrayC[2][2] = { {1,2}, {0,0} };
  62. printf("%d\n", count_zeroes_2d(2, arrayC));
  63.  
  64. printf("%d\n", n_fib(10));
  65.  
  66. char arrayD[3][31] = {"Hello",
  67. "Hello, how are you?",
  68. "I hope today is a lucky day..."};
  69. printf("%d\n", longest_row(3,31,arrayD));
  70.  
  71. printf("%c %c\n", last_special("Once upon a time....."), last_special("$a_b_c_d_1_2_3_4$"));
  72.  
  73. printf("%d %d %d\n", negative_even(-10), negative_even(-11), negative_even(10));
  74.  
  75. char str[] = "Hello world!";
  76. change_whites(str);
  77. printf("%s\n", str);
  78.  
  79.  
  80. int arrayE[5] ={10,20,50,30,40};
  81. sort_desc(arrayE, 5);
  82. for(int i = 0; i < 5; i++){
  83. printf("%d", arrayE[i]);
  84. }
  85.  
  86.  
  87. printf("%d %d\n", is_won('y', 'Y', 'y'), is_won('a', 'B', 'B'));
  88.  
  89.  
  90. printf("%d %d %d\n", direction_correction(-90), direction_correction(540), direction_correction(180));
  91.  
  92. //printf("%d %d\n", sum_digits(0), sum_digits(15));
  93.  
  94.  
  95.  
  96.  
  97. return 0;
  98.  
  99. }
  100.  
  101. /////////////////////////////////////////////////////////////////////////////////////////////////////
  102.  
  103. int sum_greater(const int a, const int b, const int c){
  104.  
  105. if(a > c && a > b){
  106. return a + c;
  107.  
  108. }
  109. else{
  110. return a + b;
  111. }
  112. if(b > a && b > c){
  113. return b + a;
  114. }
  115. else{
  116. return b + c;
  117. }
  118. if(c > a && c > b){
  119. return c + a;
  120. }
  121. else{
  122. return c + b;
  123. }
  124.  
  125. }
  126.  
  127. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  128.  
  129. int find_first_A(const char string[]){
  130.  
  131. if(string == NULL){
  132. return -1;
  133.  
  134. }
  135. for(int i = 0; i < strlen(string); i++){
  136. if(string[i] == 'A' || string[i] == 'a'){
  137. return i;
  138. }
  139. }
  140.  
  141. if(string != 'A' || string != 'a'){
  142. return -1;
  143. }
  144.  
  145. }
  146.  
  147. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  148.  
  149. char last_letter(const char string[]){
  150.  
  151. if(string == NULL){
  152. return '?';
  153. }
  154.  
  155. char pos;
  156.  
  157. for(int j = 0; j < strlen(string); j++){
  158. if(string[j] >= 'A' && string[j] <= 'Z' || string[j] >= 'a' && string[j] <= 'z'){
  159. pos = string[j];
  160. }
  161. }
  162. if((pos >= 'a' && pos <= 'z') ||(pos >= 'A' && pos <= 'Z')){
  163. return pos;
  164. }
  165. else{
  166. return '?';
  167. }
  168.  
  169. }
  170.  
  171. //////////////////////////////////////////////////////////////////////////////////////////////////////
  172.  
  173. int min_2d(const int size, const int array[][size]){
  174. int min ;
  175.  
  176.  
  177. if(min == NULL){
  178. return -1;
  179. }
  180.  
  181. for(int i = 0; i < size; i++){
  182. for(int j = 0; j < size; j++){
  183. if(min > array[j][i])
  184. min = array[j][i];
  185.  
  186. }
  187. }
  188.  
  189. return min;
  190. }
  191.  
  192. ///////////////////////////////////////////////////////////////////////////////////////////////////
  193.  
  194. int is_vowel(const char c){
  195. if(c == 'a' || c == 'i' || c == 'e' || c == 'o' || c == 'u'){
  196. return 1;
  197. }
  198. if(c == 'A' || c == 'I' || c == 'E' || c == 'O' || c == 'U'){
  199. return 1;
  200. }
  201. else{
  202. return 0;
  203. }
  204.  
  205.  
  206.  
  207. }
  208.  
  209. \\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  210.  
  211. int is_hex_digit(const char c){
  212.  
  213. if(c >= 'A' && c <= 'F'){
  214. return 1;
  215. }
  216. if(c >= '0' && c <= '9'){
  217. return 1;
  218. }
  219. if(c >= 'a' && c <= 'f'){
  220. return 1;
  221. }
  222.  
  223. return 0;
  224. }
  225. int last(const int size, const int array[]){
  226. int i;
  227.  
  228. for(int i = 0; i < size; i++){
  229. if(i == ' ' || i == '.'){
  230. return i;
  231. }
  232. }
  233.  
  234. }
  235.  
  236. ///////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  237.  
  238. int sum_positives(const int size, const int array[]){
  239.  
  240. if(array == NULL){
  241. return -1;
  242. }
  243.  
  244.  
  245. int counter = 0;
  246.  
  247. for(int i = 0; i < size; i++){
  248. if(array[i] >= 0){
  249. counter += array[i];
  250. }
  251. }
  252. return counter;
  253. }
  254.  
  255. ///////////////////////////////////////////////////////////////////////////////////////////////////
  256.  
  257. int count_zeroes_2d(const int size, int array[][size]){
  258.  
  259. if(array == NULL){
  260. return -1;
  261. }
  262.  
  263. int counter = 0;
  264.  
  265. for(int i = 0; i < size; i++){
  266. for(int j = 0; j < size; j++){
  267. if(array[i][j] == 0){
  268. counter++;
  269. }
  270.  
  271.  
  272. }
  273. }
  274. return counter;
  275.  
  276. }
  277.  
  278. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  279.  
  280. int n_fib(const int n){
  281.  
  282. int fib = 0;
  283. int fib1 = 0;
  284. int fib2 = 1;
  285.  
  286.  
  287. if(n < 1 || n > 575){
  288. return -1;
  289. }
  290.  
  291. for(int i = 2; i <= n ; i++){
  292.  
  293. fib= fib1 + fib2;
  294. fib1 = fib2;
  295. fib2 = fib;
  296.  
  297. }
  298. return fib;
  299. }
  300.  
  301. ////////////////////////////////////////////////////////////////////////////////////////
  302.  
  303. int longest_row(const int rows, const int cols, char array[rows][cols]){
  304. int result = 0;
  305. int longest = 0;
  306. for(int y = 0; y < rows; y++){
  307. int len = strlen(array[y]);
  308. if(result < len){
  309. result = len;
  310. longest = y;
  311. }
  312. }
  313.  
  314. return longest;
  315. }
  316.  
  317. ////////////////////////////////////////////////////////////////////////////////////////
  318.  
  319. char last_special(const char string[]){
  320.  
  321. if(string == NULL){
  322. return '?';
  323. }
  324.  
  325. char pos;
  326.  
  327. for(int i = 0; i < strlen(string); i++){
  328. if(string[i] != 'a' && string[i] != 'z' || string[i] != 'A' && string[i] != 'Z' ){
  329. pos = string[i];
  330. }
  331. if(string[i] != '0' && string[i] != '9'){
  332. pos = string[i];
  333. }
  334.  
  335. }
  336.  
  337. return pos;
  338.  
  339. }
  340.  
  341. ////////////////////////////////////////////////////////////////////////////////////////
  342.  
  343.  
  344. int negative_even(const int number){
  345.  
  346. if(number % 2 == 0 && number < 0){
  347. return 1;
  348. }
  349. else{
  350. return 0;
  351. }
  352.  
  353. }
  354.  
  355. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  356.  
  357. void change_whites(char string[]){
  358.  
  359. if(string == NULL){
  360. return;
  361. }
  362. for(int i = 0; i < strlen(string); i++){
  363. if(string[i] == ' ' || string[i] == '\n'){
  364. string[i] = '.';
  365. }
  366. }
  367. }
  368.  
  369.  
  370. ////////////////////////////////////////////////////////////////////////////////////////
  371.  
  372. void sort_desc(int array[], const int size){
  373.  
  374. if(array == NULL || size < 1){
  375. return;
  376. }
  377.  
  378. int a;
  379.  
  380.  
  381. for(int i = 0; i < size; i++){
  382. for(int j = i; j < size; j++){
  383. if(array[i] < array[j]){
  384. a = array[i];
  385. array[i] = array[j];
  386. array[j] = a;
  387. }
  388. }
  389. }
  390.  
  391. }
  392.  
  393. ////////////////////////////////////////////////////////////////////////////////////////
  394.  
  395.  
  396. int is_won(const char a, const char b, const char c){
  397. char a1 = tolower(a);
  398. char b1 = tolower(b);
  399. char c1 = tolower(c);
  400. if((a1 == b1) && (b1 == c1) && (a1 == c1)){
  401. return 1;
  402. }
  403. else{
  404. return 0;
  405. }
  406. }
  407.  
  408. /////////////////////////////////////////////////////////////////////////////
  409.  
  410. int direction_correction(const int degree){
  411. // int direct = degree;
  412. if(degree > 0 && degree < 360)
  413. return degree;
  414. else if(degree > 360)
  415. return degree - 360;
  416. else if(degree < 0)
  417. return -1;
  418. }
  419.  
  420. ////////////////////////////////////////////////////////////////////////////////////////
  421.  
  422. int count_negative_evens(const int size, const int array[]){
  423. if(array == NULL){
  424. return -1;
  425. }
  426.  
  427.  
  428. int counter = 0;
  429. for(int i = 0; i < size; i++){
  430. if(array[i] < 0 && array[i] % 2 == 0){
  431. counter ++;
  432. }
  433. }
  434. return counter;
  435.  
  436. }
  437.  
  438. ////////////////////////////////////////////////////////////////////////////////////////
  439.  
  440. int is_in_array(const int num, const int size, const int array[]){
  441.  
  442. if(array == NULL){
  443. return -1;
  444. }
  445.  
  446. for(int i = 0; i < size; i++){
  447. if(num == array[i]){
  448. return 1;
  449. }
  450.  
  451. }
  452. return 0;
  453. }
  454.  
  455. ////////////////////////////////////////////////////////////////////////////////////////
  456.  
  457. int leap_year(const int year){
  458.  
  459. if(year < 1 || year > 4443){
  460. return -1;
  461. }
  462.  
  463.  
  464. if (year % 4 == 0) {
  465. if (year % 100 == 0) {
  466.  
  467. if (year % 400 == 0)
  468. return 1;
  469. else
  470. return 0;
  471. } else
  472. return 1;
  473. } else
  474. return 0;
  475.  
  476.  
  477. }
  478.  
  479. ///////////////////////////////////////////////////////////////////////////////////
  480.  
  481.  
  482. void digits_sum_sequence(const int size, int array[]){
  483. array[0] = 1;
  484.  
  485. for(int i = 1; i < size; i++){
  486. int a = i-1;
  487. array[i] = 0;
  488. while (a >= 0){
  489. if(array[a] > 9){
  490.  
  491. int cislo= array[a];
  492. int sum=0;
  493.  
  494. while(cislo>0){
  495. sum+=cislo%10;
  496. cislo/=10;
  497. }
  498.  
  499. array[i] += sum;
  500. }
  501.  
  502. else array[i] += array[a];
  503.  
  504. a--;
  505. }
  506.  
  507. }
  508. }
  509.  
  510. /////////////////////////////////////////////////////////////////////////////
  511.  
  512. void swap_sign_2d(const int size, int array[2][2]){
  513. if(array[size][size] == NULL){
  514. return;
  515. }
  516.  
  517. for(int i = 0; i < size; i++){
  518. for(int j = 0; j < size; j++){
  519. if(array[j][i] > 0){
  520. array[j][i] = (-1)*array[j][i];
  521. }
  522. else if(array[j][i] < 0){
  523. array[j][i] = (-1)*array[j][i];
  524. }
  525. else if(array [j][i] == 0){
  526. array[j][i] = 0;
  527. }
  528.  
  529. // array[j][i] = array[j][i] - 2*array[j][i];
  530. }
  531.  
  532. }
  533. }
  534.  
  535. /////////////////////////////////////////////////////////////////////
  536.  
  537. void swap_case_2d(const int rows,const int cols, char strings[][cols]){
  538. int word = 0;
  539. for(int y = 0; y < rows; y++){
  540. for(int x = 0; x < cols; x++){
  541. if(islower(strings[y][x]))
  542. strings[y][x]=toupper(strings[y][x]);
  543. else if(isupper(strings[y][x]))
  544. strings[y][x]=tolower(strings[y][x]);
  545.  
  546. }
  547. }
  548.  
  549.  
  550. }
  551.  
  552. ////////////////////////////////////////////////////////////////////////
  553.  
  554.  
  555. int vowels_count_2d(const int rows, const int cols, char strings[][cols]){
  556.  
  557. if(strings == NULL){
  558. return -1;
  559. }
  560. int counter = 0;
  561. for(int y = 0; y < rows; y++){
  562. for(int x = 0; x < cols; x++){
  563. if(strings[y][x] == 'a' || strings[y][x] == 'e' || strings[y][x] == 'i' || strings[y][x] == 'o' || strings[y][x] == 'u' || strings[y][x] == 'y'){
  564. counter = counter +1;
  565. }
  566. else if(strings[y][x] == 'A' || strings[y][x] == 'E' || strings[y][x] == 'I' || strings[y][x] == 'O' || strings[y][x] == 'U' || strings[y][x] == 'Y'){
  567. counter = counter+1;
  568. }
  569.  
  570.  
  571. }
  572.  
  573. }
  574. return counter;
  575. }
  576.  
  577. ////////////////////////////////////////////////////////////////////
  578.  
  579. int is_in_array_2d(const int num, const int size, int array[][size]){
  580. for(int y = 0; y < size; y++){
  581. for(int x = 0;x < size; x++){
  582. if(array[y][x] == num){
  583. return 1;
  584. }
  585.  
  586. }
  587. }
  588. return 0;
  589.  
  590. }
  591.  
  592. /////////////////////////////////////////////////////////////////////////////
  593.  
  594. int count_whites(const char string[]){
  595. char text = string;
  596. int counter = 0;
  597. int index = 0;
  598.  
  599. while(text[index] != '\0'){
  600. if(text[index] == NULL)
  601. return -1;
  602. else if(text[index] == ' ' || text[index] == '\t' || text[index] == '\n'){
  603. counter += 1;
  604. index++;
  605. }
  606. index++;
  607. // char str[] = "Geeks";
  608. }
  609.  
  610. return counter;
  611. }
  612.  
  613. /////////////////////////////////////////////////////////////////////////////////
  614.  
  615. int binary_num(const int num){
  616.  
  617. if(num == 1 || num == 0){
  618. return 1;
  619. }
  620. else if(num < -1000 || num > 1000 ){
  621. return -1;
  622. }
  623. else{
  624. return 0;
  625. }
  626. }
  627.  
  628. /////////////////////////////////////////////////////////////////////////////////
  629.  
  630. int is_special(const char c){
  631. if(c >= '0' && c <= '9'){
  632. return 0;
  633. }
  634. else if(c >= 'A' && c <= 'Z'){
  635. return 0;
  636. }
  637. else if(c >= 'a' && c <= 'z'){
  638. return 0;
  639. }
  640.  
  641.  
  642. return 1;
  643. }
  644.  
  645. /////////////////////////////////////////////////////////////////////////
  646.  
  647. int check_SPZ(const char spz[]){
  648.  
  649. for(int i = 0; i < strlen(spz); i++){
  650. if(strlen(spz) != 7 || spz[0] > 'Z' || spz[0] < 'A' || spz[1] > 'Z' || spz[1] < 'A' ||
  651. spz[2] > '9' || spz[2] < '0'|| spz[3] > '9' || spz[3] < '0' || spz[4] > '9' || spz[4] < '0'
  652. || spz[5] > 'Z' || spz[5] < 'A' || spz[6] > 'Z' || spz[6] < 'A'){
  653. return 0;
  654. }
  655. }
  656.  
  657.  
  658. return 1;
  659. }
  660.  
  661. ///////////////////////////////////////////////////////////////////////////////
  662.  
  663. void fib_2_array(const int size, int array[]){
  664. if(array == NULL){
  665. return;
  666. }
  667. array[0] = 1;
  668. array[1] = 1;
  669.  
  670. for(int i = 2; i < size; i++){
  671. array[i] = array[i - 2] + array [i - 1];
  672. }
  673. }
  674.  
  675. ///////////////////////////////////////////////////////////////////////////////
  676.  
  677. void string_to_upper(char string[]){
  678. int n=strlen(string);
  679. for(int i = 0; i < n; i++){
  680. if(islower(string[i])){
  681. string[i] = toupper(string[i]);
  682. }
  683. }
  684. }
  685.  
  686. //////////////////////////////////////////////////////////////////////////////////////////////
  687.  
  688. int karel_asleep(const char position){
  689.  
  690. if(position == '-'){
  691. return 1;
  692. }
  693. if(position == '|'){
  694. return 0;
  695. }
  696. return -1;
  697. }
  698.  
  699. ////////////////////////////////////////////////////////////////////////////
  700.  
  701. #include <stdio.h>
  702. #include <stdlib.h>
  703.  
  704. int main()
  705. {
  706. int n;
  707. scanf("%d", &n);
  708. int array[n];
  709. int pom;
  710. for(int i = 0; i < n; i++){
  711. scanf("%d",&pom);
  712. array[i] = pom;
  713. }
  714. for(int i = 0; i < n; i++){
  715. for(int j = i; j < n; j++){
  716. if(array[i] > array[j]){
  717. pom = array[i];
  718. array[i] = array[j];
  719. array[j] = pom;
  720.  
  721. }
  722. }
  723. printf("Z:%d\n", array[i]);
  724. }
  725. if(n % 2 == 0){
  726. float median = ((float)array[(n - 1) / 2] + (float)array[n / 2]) / 2;
  727. printf("Median je: %f\n", median);
  728. }
  729. else{
  730. printf("Median je : %d\n", array[n/2]);
  731. }
  732. return 0;
  733. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement