Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* Execution Functions declarations */
- void Ex1();
- void Ex2();
- void Ex3();
- void Ex4();
- void Ex5();
- /* Declarations of other functions */
- int replace_couple(int n); // 1
- int increase_decrease(int n, int f); // 2
- int index_three_minus_all(); // 3
- int k_palindrome(int* arr, int k, int n); // 4
- int* min_of_arr(int* a, int n, int k, int* p);
- int main()
- {
- int select = 0, i, all_Ex_in_loop = 0;
- printf("Run menu once or cyclically?\n(Once - enter 0, cyclically - enter other number) ");
- if (scanf_s("%d", &all_Ex_in_loop) == 1)
- do
- {
- for (i = 1; i <= 5; i++)
- printf("Ex%d--->%d\n", i, i);
- printf("EXIT-->0\n");
- do {
- select = 0;
- printf("please select 0-5 : ");
- scanf_s("%d", &select);
- } while ((select < 0) || (select > 5));
- switch (select)
- {
- case 1: Ex1(); break;
- case 2: Ex2(); break;
- case 3: Ex3(); break;
- case 4: Ex4(); break;
- case 5: Ex5(); break;
- }
- } while (all_Ex_in_loop && select);
- return 0;
- }
- // Function 1
- int replace_couple(int n)
- {
- // 123456 - 214365
- // 1234567 - 1325476
- int curr_num, prev_num;
- int new_num;
- curr_num = n % 10; // 5
- prev_num = (n / 10) % 10; // 6
- new_num = (curr_num * 10) + prev_num;
- if (n < 100 && n > 9) // Even > 10 --- 100
- return new_num;
- if (n < 10)
- return curr_num;
- return replace_couple(n / 100) * 100 + new_num;
- }
- // Function 2
- int increase_decrease(int n, int f)
- {
- switch (f)
- {
- case 0:
- {
- // f = 0 Start
- int curr_num, prev_num;
- if (n < 10)
- return 1;
- curr_num = n % 10;
- prev_num = (n / 10) % 10;
- if (prev_num > curr_num)
- return increase_decrease(n / 10, 0);
- return 0;
- // End of f = 0 Stop
- }
- case 1:
- {
- // For f = 1 Start
- int curr_num, prev_num;
- if (n < 10)
- return 1;
- curr_num = n % 10;
- prev_num = (n / 10) % 10;
- if (curr_num > prev_num)
- return increase_decrease(n / 10, 1);
- return 0;
- // End of f = 0 Stop
- }
- case 2:
- {
- // For f = 2 Start
- int curr_num, prev_num;
- curr_num = n % 10;
- prev_num = (n / 10) % 10;
- if (prev_num > curr_num) // 12341
- return increase_decrease(n / 10, 4);
- else
- return 0;
- // End of f = 2 Stop
- }
- case 3: //
- {
- int curr_num, prev_num;
- curr_num = n % 10;
- prev_num = (n / 10) % 10; // 432123
- if (prev_num < curr_num)
- return increase_decrease(n / 10, 5);
- else
- return 0;
- }
- case 4:
- {
- int curr_num, prev_num;
- if (n < 10)
- return 0;
- curr_num = n % 10;
- prev_num = (n / 10) % 10;
- if (prev_num > curr_num) // 123421
- return increase_decrease(n / 10, 4);
- if (prev_num < curr_num)
- return increase_decrease(n / 10, 1);
- }
- case 5:
- {
- int curr_num, prev_num;
- if (n < 10)
- return 0;
- curr_num = n % 10;
- prev_num = (n / 10) % 10;
- if (prev_num < curr_num) // 432123
- return increase_decrease(n / 10, 5);
- if (prev_num > curr_num)
- return increase_decrease(n / 10, 0);
- }
- } // end of switch
- }
- // Function 3
- int index_three_minus_all()
- {
- int sum_by_three = 0;
- int all_except_three = 0;
- for (int i = 1; i <= 3; i++)
- {
- int curr_num;
- printf("Enter num: ");
- scanf_s("%d", &curr_num);
- if (i == 3 && curr_num != -1)
- {
- sum_by_three = curr_num;
- all_except_three += 0;
- }
- else
- {
- if (curr_num == -1)
- all_except_three += 0;
- else
- all_except_three += curr_num;
- }
- if (curr_num == -1)
- return (sum_by_three - all_except_three);
- }
- int result = sum_by_three - all_except_three;
- return result + index_three_minus_all();
- }
- // Function 4
- int k_palindrome(int* arr, int k, int n)
- {
- // 10,20,3,6,1,16,3,14,2,7,25,5
- // 0 1 2 3 4 5 6 7 8 9 10 11
- // n = 12
- if (n < 2 * k)
- return 1;
- int left = 0, right = 0;
- for (int i = 0; i < k; i++)
- {
- left += arr[i];
- }
- for (int j = n - 1; j >= n - k; j--)
- {
- right += arr[j];
- }
- if (left == right)
- return k_palindrome(arr + k, k, n - (2 * k));
- else
- return 0;
- }
- // Function 5
- int* min_of_arr(int* a, int n, int k, int* p)
- {
- int* b;
- b = (int*)malloc(k * sizeof(int));
- // [10,5,2,60,2,7,2,10]
- int min_value, min_index;
- int max_value;
- int sum_b = 0;
- for (int i = 0; i < k; i++) // 4
- {
- min_value = a[0];
- min_index = 0;
- max_value = a[0];
- for (int j = 1; j < n; j++) // 8
- {
- if (max_value <= a[j])
- max_value = a[j]; // 60
- if (min_value >= a[j])
- {
- min_value = a[j]; // 2
- min_index = j; // 6
- }
- }
- a[min_index] = max_value;
- sum_b += min_value;
- b[i] = min_value;
- }
- *p = sum_b;
- return b;
- }
- // Execution Functions
- /*.............................*/
- void Ex1()
- { // This function will return a new number with reversed
- // couple of numbers
- int num;
- printf("\nThis function will flip every couple of numbers\nof the original number.\n");
- printf("\nEnter an integer: ");
- scanf_s("%d", &num);
- printf("\nThe new number is: %d\n\n", replace_couple(num));
- }
- /*.............................*/
- void Ex2()
- {
- int num;
- int f;
- printf("\nThis function will return '1' if true and '0' if false,\ndepending on the function.\n");
- printf("\nFunction:\n");
- printf("\n0: Right-To-Left; Number is extremely increasing.");
- printf("\n1: Right-To-Left; Number is extremely decreasing.");
- printf("\n2: Right-To-Left; Number is extremely increasing and then extremely decreasing.");
- printf("\n3: Right-To-Left; Number is extremely decreasing and then extremely increasing.\n");
- printf("\nEnter function: ");
- scanf_s("%d", &f);
- printf("\nEnter number: ");
- scanf_s("%d", &num);
- printf("\nResult is: %d\n\n", increase_decrease(num, f));
- }
- /*.............................*/
- void Ex3()
- {
- 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");
- printf("\nEnter numbers and input -1 to stop running..\n");
- int result = index_three_minus_all();
- printf("\nResult is: %d\n\n", result);
- }
- /*.............................*/
- void Ex4()
- {
- int n, k;
- int* arr;
- printf("\nThis function will get an array and show if there is a palindrome in the array.\n'1' = True, '0' = False\n");
- printf("\nIt can check if a sum of 'k' numbers is the same of the sum of 'k' numbers in the other side.\n");
- printf("Enter k: ");
- scanf_s("%d", &k);
- printf("Enter quantity of numbers: ");
- scanf_s("%d", &n);
- arr = (int*)malloc(n * sizeof(int));
- for (int i = 0; i < n; i++)
- {
- printf("Enter num %d/%d: ", i + 1, n);
- scanf_s("%d", (arr + i));
- }
- printf("\n\n");
- int result = k_palindrome(arr, k, n);
- printf("\nResult is: %d\n", result);
- free(arr);
- }
- /*.............................*/
- void Ex5()
- {
- int n, k;
- int* a;
- int* b; // new array
- int p; // sum of new array
- printf("\nThis function will show the smallest numbers in a sequence of numbers and will also print their sum.\n");
- printf("\nHow many numbers would you like to view?: ");
- scanf_s("%d", &k);
- printf("Enter quantity of numbers in the sequence: ");
- scanf_s("%d", &n);
- a = (int*)malloc(n * sizeof(int));
- for (int i = 0; i < n; i++)
- {
- printf("Enter num %d/%d: ", i + 1, n);
- scanf_s("%d", (a + i));
- }
- b = min_of_arr(a, n, k, &p);
- for (int j = 0; j < k; j++)
- {
- printf("%d ", b[j]);
- }
- printf("\nSum: %d\n", p);
- free(a);
- free(b);
- }
Add Comment
Please, Sign In to add comment