Advertisement
vallec

Untitled

Apr 6th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double* getArray(int);
  4. double* addElement(double* grades, int size, double el, int newIndex);
  5. void printArr(double*, int);
  6. //double* removeElement(double* grades, int size, int index);
  7. double* removeElement(double* array, int *size, int index)
  8. {
  9.  
  10.  
  11. for(int i = 0; i < *size; i++)
  12. {
  13.  
  14. if(i >= index)
  15. {
  16.  
  17. array[i] = array[i+1];
  18.  
  19. }
  20.  
  21. }
  22.  
  23. *size -= 1;
  24. double *newArray = (double*)realloc(array, *size * sizeof(double));
  25.  
  26.  
  27. if(newArray != array)
  28. free(array);
  29.  
  30. return array;
  31.  
  32. }
  33.  
  34. void main()
  35. {
  36. int size = 5;
  37. double* grades = getArray(size);
  38.  
  39. printArr(grades, size);
  40.  
  41. printf("----Add-----\n");
  42. double newGrade = 1;
  43. while(newGrade != 0){
  44. printf("Vavedi nova ocenka (0 za krai): ");
  45. scanf("%lf", &newGrade);
  46. if(newGrade == 0){
  47. break;
  48. }
  49. int newIndex;
  50. printf("Vavedi index: ");
  51. scanf("%d", &newIndex);
  52.  
  53.  
  54. size++;
  55. grades = addElement(grades, size, newGrade, newIndex);
  56. }
  57. printf("----new-----\n");
  58.  
  59. printArr(grades, size);
  60.  
  61. printf("----Remove-----\n");
  62. double newIndex = 1;
  63. while(newIndex != -1){
  64. printf("Vavedi nova ocenka (-1 za krai): ");
  65. scanf("%lf", &newIndex);
  66. if(newIndex == -1){
  67. break;
  68. }
  69.  
  70.  
  71. grades = removeElement(grades, &size, newIndex);
  72. }
  73.  
  74.  
  75. printArr(grades, size);
  76. }
  77.  
  78.  
  79.  
  80. /*double* removeElement(double* grades, int size, int index)
  81. {
  82. double* newArray = (double*)realloc(grades, size * sizeof(double));
  83.  
  84. int currIndex = 0;
  85. int gradesIndex = 0;
  86. while(currIndex < size){
  87. if(currIndex == index){
  88. gradesIndex++;
  89. } else {
  90. newArray[currIndex] = grades[gradesIndex];
  91. currIndex++;
  92. gradesIndex++;
  93. }
  94.  
  95. }
  96.  
  97. return newArray;
  98. }*/
  99.  
  100. double* addElement(double* grades, int size, double el, int newIndex)
  101. {
  102. double* newArray = (double*)realloc(grades, size * sizeof(double));
  103.  
  104. double temp = newArray[newIndex];
  105. newArray[newIndex] = el;
  106. newArray[size-1] = temp;
  107.  
  108. return newArray;
  109. }
  110.  
  111. void printArr(double* grades, int size)
  112. {
  113. for (int i = 0; i < size; i++)
  114. {
  115. printf("Ocenka no. %d : %.2lf\n", i+1, grades[i]);
  116. }
  117. }
  118.  
  119. double* getArray(size)
  120. {
  121. double* arr = (double*)malloc(size * sizeof(double));
  122. // double* arr = calloc(5, sizeof(double));
  123.  
  124. for (int i = 0; i < size; i++)
  125. {
  126. printf("Vavedi ocenka no. %d: ", i+1);
  127. scanf("%lf", &arr[i]);
  128. }
  129.  
  130. return arr;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement