Moonbear

Untitled

Jul 28th, 2021
559
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. /***************
  4. *              *
  5. *   ANSWERS    *
  6. *              *
  7. ****************/
  8.  
  9. /*
  10. Moshe is a great magshimim student, he wrote some code but he had a lot of errors, can you help him?
  11. when the code works enter the number: 10 and the numbers: 23,4,16,42,8,15,64,26,3,112
  12. see what it prints...
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. int function1();
  19.  
  20. void function3(int *array_pointer, int *length);
  21. /* DON'T TOUCH function4!!!!!!*/
  22. void function4(int *array_pointer, int *length);
  23. int function2(int* p_num);
  24.  
  25.    
  26.  
  27. int main()
  28. {
  29.     function1();
  30.     system("Pause");
  31. }
  32.  
  33. int function1()
  34. {
  35.     int* p_num = (int*)malloc(sizeof(double long));
  36.     if (p_num)
  37.     {
  38.         printf("Please enter a number : \n");
  39.         scanf("%d", p_num);
  40.         function2(p_num);
  41.         free(p_num);
  42.     }
  43.     else
  44.     {
  45.         printf("Unsuccesful malloc!\n");
  46.         return 0;
  47.     }
  48.  
  49. }
  50.  
  51. int function2(int* p_num)
  52. {
  53.     int* array_pointer = (int*)malloc(sizeof(int)* (*p_num));
  54.     int i;
  55.     if (array_pointer)
  56.     {
  57.         for (i = 0; i < *(p_num); i++)
  58.         {
  59.             printf("Please enter a number :\n");
  60.             scanf("%d", array_pointer + i);
  61.         }
  62.         function3(array_pointer, p_num);
  63.         free(array_pointer);
  64.     }
  65.     else
  66.     {
  67.         printf("Unsuccesful malloc!\n");
  68.         return(0);
  69.     }
  70.  
  71.  
  72. }
  73.  
  74. void function3(int *array_pointer, int *length)
  75. {
  76.     int i;
  77.     char j;
  78.     int temp =0;
  79.     for (i = 1; i <= *length - 1; i++)
  80.     {
  81.         j = i;
  82.         while ((j > 0) && (*(array_pointer + j) < *(array_pointer + j - 1)))
  83.         {
  84.             temp = *(array_pointer + j);
  85.             *(array_pointer + j) = *(array_pointer + j - 1);
  86.             *(array_pointer + j - 1) = temp;
  87.             j--;
  88.         }
  89.     }
  90.     function4(array_pointer, length);
  91.     printf("\n");
  92.  
  93. }
  94.  
  95. //DO NOT touch this function
  96. void function4(int *array_pointer, int *length)
  97. {
  98.     int i;
  99.     char* very_interesting_string = (char*)malloc(sizeof(char)* 5);
  100.     char buffer[3];
  101.  
  102.     *(very_interesting_string) = (char)(*(array_pointer)+63);
  103.     *(very_interesting_string + 1) = (char)(*(array_pointer + 1) + 80);
  104.     *(very_interesting_string + 2) = (char)(*(array_pointer + 2) + 60);
  105.     *(very_interesting_string + 3) = (char)(*(array_pointer + 3) + 108);
  106.     *(very_interesting_string + 4) = (char)(*(array_pointer + 9) + 13);
  107.  
  108.     for (i = 0; i < 4; i++)
  109.     {
  110.         printf("%c", *(very_interesting_string + i));
  111.     }
  112.  
  113.     for (i = 4; i <= 8; i++)
  114.     {
  115.         itoa(*(array_pointer + i), buffer, 10);
  116.         *(very_interesting_string) = (*(array_pointer));
  117.         printf("%s", buffer);
  118.     }
  119.  
  120.     printf("%c", *(very_interesting_string + 4));
  121.  
  122. }
RAW Paste Data