Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define _CRT_SECURE_NO_WARNINGS
  5.  
  6. void main() {
  7. /*Програма, която заделя памет за масив от цели числа.
  8. Броят на елементите на масива се определя от потребителя.
  9. Запълването на стойности се извършва от потребитеря.
  10. Потребителя въвежда нова големина на масива,
  11. посредством realloc се заделя нова големина на масива,
  12. ако новата големина е по-голяма от старата, потребителя трабва да допълни масива.
  13. След, което се отпечатва.
  14. */
  15.  
  16. int s1, s2, i;
  17. printf("Enter the length of the array: ");
  18. scanf("%d", &s1);
  19.  
  20. int* array;
  21. array = (int*)malloc(s1 * sizeof(int));
  22. if (array == NULL) exit(1);
  23. else printf("OK!\n");
  24.  
  25. for (i = 0; i < s1; i++) {
  26. printf("Enter value for element No %d: ", i+1);
  27. scanf("%d", array[i]);
  28. }
  29.  
  30. printf("Enter the length of the new array: ");
  31. scanf("%d", &s2);
  32.  
  33. if (s1 > s2 || s1 == s2)
  34. {
  35. for (int j = 0; j < s2; j++)
  36. {
  37. printf("%d", array[j]);
  38. }
  39. }
  40. else
  41. {
  42. array = (int*)realloc(array, s2);
  43. for (i; i < s2; i++)
  44. scanf("%d", array[i]);
  45. for (int j = 0; j < s2; j++)
  46. {
  47. printf("%d", array[j]);
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement