Idanref

Assignment 2 - Intro CS

Dec 24th, 2020 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. /* Execution Functions declarations */
  6. void Ex1();
  7. void Ex2();
  8. void Ex3();
  9. void Ex4();
  10. void Ex5();
  11.  
  12. /* Declarations of other functions */
  13.  
  14. int replace_couple(int n); // 1
  15. int increase_decrease(int n, int f); // 2
  16. int index_three_minus_all(); // 3
  17. int k_palindrome(int* arr, int k, int n); // 4
  18. int* min_of_arr(int* a, int n, int k, int* p);
  19.  
  20.  
  21. int main()
  22. {
  23.     int select = 0, i, all_Ex_in_loop = 0;
  24.     printf("Run menu once or cyclically?\n(Once - enter 0, cyclically - enter other number) ");
  25.     if (scanf_s("%d", &all_Ex_in_loop) == 1)
  26.         do
  27.         {
  28.             for (i = 1; i <= 5; i++)
  29.                 printf("Ex%d--->%d\n", i, i);
  30.             printf("EXIT-->0\n");
  31.             do {
  32.                 select = 0;
  33.                 printf("please select 0-5 : ");
  34.                 scanf_s("%d", &select);
  35.             } while ((select < 0) || (select > 5));
  36.             switch (select)
  37.             {
  38.                 case 1: Ex1(); break;
  39.                 case 2: Ex2(); break;
  40.                 case 3: Ex3(); break;
  41.                 case 4: Ex4(); break;
  42.                 case 5: Ex5(); break;
  43.             }
  44.         } while (all_Ex_in_loop && select);
  45.         return 0;
  46. }
  47.  
  48.  
  49. // Function 1
  50. int replace_couple(int n)
  51. {
  52.     // 123456 - 214365
  53.     // 1234567 - 1325476
  54.    
  55.     int curr_num, prev_num;
  56.     int new_num;
  57.  
  58.     curr_num = n % 10; // 5
  59.     prev_num = (n / 10) % 10; // 6
  60.  
  61.     new_num = (curr_num * 10) + prev_num;
  62.  
  63.     if (n < 100 && n > 9) // Even > 10 --- 100
  64.         return new_num;
  65.  
  66.     if (n < 10)
  67.         return curr_num;
  68.  
  69.  
  70.     return replace_couple(n / 100) * 100 + new_num;
  71.  
  72. }
  73.  
  74. // Function 2
  75. int increase_decrease(int n, int f)
  76. {
  77.     switch (f)
  78.     {
  79.         case 0:
  80.         {
  81.             // f = 0 Start
  82.             int curr_num, prev_num;
  83.  
  84.             if (n < 10)
  85.                 return 1;
  86.  
  87.             curr_num = n % 10;
  88.             prev_num = (n / 10) % 10;
  89.  
  90.             if (prev_num > curr_num)
  91.                 return increase_decrease(n / 10, 0);
  92.  
  93.             return 0;
  94.             // End of f = 0 Stop
  95.         }
  96.  
  97.         case 1:
  98.         {
  99.             // For f = 1 Start
  100.  
  101.             int curr_num, prev_num;
  102.  
  103.             if (n < 10)
  104.                 return 1;
  105.  
  106.             curr_num = n % 10;
  107.             prev_num = (n / 10) % 10;
  108.  
  109.             if (curr_num > prev_num)
  110.                 return increase_decrease(n / 10, 1);
  111.  
  112.             return 0;
  113.             // End of f = 0 Stop
  114.         }
  115.  
  116.         case 2:
  117.         {
  118.             // For f = 2 Start
  119.             int curr_num, prev_num;
  120.  
  121.             curr_num = n % 10;
  122.             prev_num = (n / 10) % 10;
  123.  
  124.             if (prev_num > curr_num) // 12341
  125.                 return increase_decrease(n / 10, 4);
  126.  
  127.             else
  128.                 return 0;
  129.  
  130.             // End of f = 2 Stop
  131.         }
  132.  
  133.         case 3: //
  134.         {
  135.             int curr_num, prev_num;
  136.  
  137.             curr_num = n % 10;
  138.             prev_num = (n / 10) % 10; //  432123
  139.  
  140.             if (prev_num < curr_num)
  141.                 return increase_decrease(n / 10, 5);
  142.  
  143.             else
  144.                 return 0;
  145.         }
  146.  
  147.         case 4:
  148.         {
  149.             int curr_num, prev_num;
  150.  
  151.             if (n < 10)
  152.                 return 0;
  153.  
  154.             curr_num = n % 10;
  155.             prev_num = (n / 10) % 10;
  156.  
  157.             if (prev_num > curr_num) // 123421
  158.                 return increase_decrease(n / 10, 4);
  159.  
  160.             if (prev_num < curr_num)
  161.                 return increase_decrease(n / 10, 1);
  162.         }
  163.  
  164.         case 5:
  165.         {
  166.             int curr_num, prev_num;
  167.  
  168.             if (n < 10)
  169.                 return 0;
  170.  
  171.             curr_num = n % 10;
  172.             prev_num = (n / 10) % 10;
  173.  
  174.             if (prev_num < curr_num) // 432123
  175.                 return increase_decrease(n / 10, 5);
  176.  
  177.             if (prev_num > curr_num)
  178.                 return increase_decrease(n / 10, 0);
  179.         }
  180.     } // end of switch
  181. }
  182.  
  183. // Function 3
  184. int index_three_minus_all()
  185. {
  186.     int sum_by_three = 0;
  187.     int all_except_three = 0;
  188.  
  189.     for (int i = 1; i <= 3; i++)
  190.     {
  191.         int curr_num;
  192.  
  193.         printf("Enter num: ");
  194.         scanf_s("%d", &curr_num);
  195.  
  196.         if (i == 3 && curr_num != -1)
  197.         {
  198.             sum_by_three = curr_num;
  199.             all_except_three += 0;
  200.         }
  201.  
  202.         else
  203.         {
  204.             if (curr_num == -1)
  205.                 all_except_three += 0;
  206.             else
  207.                 all_except_three += curr_num;
  208.         }
  209.  
  210.         if (curr_num == -1)
  211.             return (sum_by_three - all_except_three);
  212.     }
  213.  
  214.     int result = sum_by_three - all_except_three;
  215.  
  216.     return result + index_three_minus_all();
  217. }
  218.  
  219. // Function 4
  220. int k_palindrome(int* arr, int k, int n)
  221. {
  222.     // 10,20,3,6,1,16,3,14,2,7,25,5
  223.     // 0  1  2 3 4 5  6  7 8 9 10 11
  224.     // n = 12
  225.  
  226.     if (n < 2 * k)
  227.         return 1;
  228.  
  229.     int left = 0, right = 0;
  230.  
  231.     for (int i = 0; i < k; i++)
  232.     {
  233.         left += arr[i];
  234.     }
  235.  
  236.     for (int j = n - 1; j >= n - k; j--)
  237.     {
  238.         right += arr[j];
  239.     }
  240.  
  241.     if (left == right)
  242.         return k_palindrome(arr + k, k, n - (2 * k));
  243.  
  244.     else
  245.         return 0;
  246.  
  247. }
  248.  
  249. // Function 5
  250. int* min_of_arr(int* a, int n, int k, int* p)
  251. {
  252.  
  253.     int* b;
  254.     b = (int*)malloc(k * sizeof(int));
  255.  
  256.     // [10,5,2,60,2,7,2,10]
  257.  
  258.     int min_value, min_index;
  259.     int max_value;
  260.  
  261.     int sum_b = 0;
  262.  
  263.     for (int i = 0; i < k; i++) // 4
  264.     {
  265.         min_value = a[0];
  266.         min_index = 0;
  267.  
  268.         max_value = a[0];
  269.  
  270.         for (int j = 1; j < n; j++) // 8
  271.         {
  272.             if (max_value <= a[j])
  273.                 max_value = a[j]; // 60
  274.  
  275.             if (min_value >= a[j])
  276.             {
  277.                 min_value = a[j]; // 2
  278.                 min_index = j; // 6
  279.             }
  280.         }
  281.  
  282.         a[min_index] = max_value;
  283.  
  284.         sum_b += min_value;
  285.         b[i] = min_value;
  286.     }
  287.  
  288.     *p = sum_b;
  289.     return b;
  290. }
  291.  
  292.  
  293. // Execution Functions
  294. /*.............................*/
  295. void Ex1()
  296. {   //  This function will return a new number with reversed
  297.     // couple of numbers
  298.    
  299.     int num;
  300.  
  301.     printf("\nThis function will flip every couple of numbers\nof the original number.\n");
  302.     printf("\nEnter an integer: ");
  303.     scanf_s("%d", &num);
  304.     printf("\nThe new number is: %d\n\n", replace_couple(num));
  305. }
  306.  
  307. /*.............................*/
  308. void Ex2()
  309. {
  310.     int num;
  311.     int f;
  312.  
  313.     printf("\nThis function will return '1' if true and '0' if false,\ndepending on the function.\n");
  314.     printf("\nFunction:\n");
  315.     printf("\n0: Right-To-Left; Number is extremely increasing.");
  316.     printf("\n1: Right-To-Left; Number is extremely decreasing.");
  317.     printf("\n2: Right-To-Left; Number is extremely increasing and then extremely decreasing.");
  318.     printf("\n3: Right-To-Left; Number is extremely decreasing and then extremely increasing.\n");
  319.  
  320.     printf("\nEnter function: ");
  321.     scanf_s("%d", &f);
  322.  
  323.     printf("\nEnter number: ");
  324.     scanf_s("%d", &num);
  325.    
  326.     printf("\nResult is: %d\n\n", increase_decrease(num, f));
  327. }
  328.  
  329. /*.............................*/
  330. void Ex3()
  331. {
  332.     printf("\nThis function will print the sum of every number in index number that divides by 3,\n minus all the rest of the numbers.\n");
  333.     printf("\nEnter numbers and input -1 to stop running..\n");
  334.  
  335.     int result = index_three_minus_all();
  336.  
  337.     printf("\nResult is: %d\n\n", result);
  338. }
  339.  
  340. /*.............................*/
  341. void Ex4()
  342. {
  343.     int n, k;
  344.     int* arr;
  345.  
  346.     printf("\nThis function will get an array and show if there is a palindrome in the array.\n'1' = True, '0' = False\n");
  347.     printf("\nIt can check if a sum of 'k' numbers is the same of the sum of 'k' numbers in the other side.\n");
  348.    
  349.     printf("Enter k: ");
  350.     scanf_s("%d", &k);
  351.  
  352.     printf("Enter quantity of numbers: ");
  353.     scanf_s("%d", &n);
  354.  
  355.     arr = (int*)malloc(n * sizeof(int));
  356.  
  357.     for (int i = 0; i < n; i++)
  358.     {
  359.         printf("Enter num %d/%d: ", i + 1, n);
  360.         scanf_s("%d", (arr + i));
  361.     }
  362.  
  363.     printf("\n\n");
  364.  
  365.     int result = k_palindrome(arr, k, n);
  366.  
  367.     printf("\nResult is: %d\n", result);
  368.  
  369.     free(arr);
  370. }
  371.  
  372. /*.............................*/
  373. void Ex5()
  374. {
  375.     int n, k;
  376.     int* a;
  377.  
  378.     int* b; // new array
  379.     int p; // sum of new array
  380.  
  381.     printf("\nThis function will show the smallest numbers in a sequence of numbers and will also print their sum.\n");
  382.  
  383.     printf("\nHow many numbers would you like to view?: ");
  384.     scanf_s("%d", &k);
  385.  
  386.     printf("Enter quantity of numbers in the sequence: ");
  387.     scanf_s("%d", &n);
  388.  
  389.     a = (int*)malloc(n * sizeof(int));
  390.  
  391.     for (int i = 0; i < n; i++)
  392.     {
  393.         printf("Enter num %d/%d: ", i + 1, n);
  394.         scanf_s("%d", (a + i));
  395.     }
  396.  
  397.     b = min_of_arr(a, n, k, &p);
  398.  
  399.     for (int j = 0; j < k; j++)
  400.     {
  401.         printf("%d   ", b[j]);
  402.     }
  403.  
  404.     printf("\nSum: %d\n", p);
  405.  
  406.     free(a);
  407.     free(b);
  408. }
Add Comment
Please, Sign In to add comment