Advertisement
Stoycho_KK

Untitled

Apr 2nd, 2021
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #pragma warning(disable:4996)
  4.  
  5. double grades[] = { 5.50,3.57,4.20,5.80,6.00,4.50,2.00,5.00,6.00,3.80 };
  6.  
  7. double* arrayCapasityAndIni(int len) {
  8. double* arr2 = (double*)malloc(len * sizeof(double));
  9. int i;
  10.  
  11. for (i = 0; i < len; i++) {
  12. arr2[i] = grades[i];
  13. }
  14.  
  15. return arr2;
  16. }
  17. int gradesNumbers() {
  18. int n;
  19.  
  20. do {
  21. printf("Numbers: ");
  22. scanf("%d", &n);
  23. if (n > 10 || n < 0) {
  24. printf("Enter number between 0 and 10");
  25. }
  26. if (n == 0) {
  27. return 0;
  28. }
  29. } while (n < 0 || n>10);
  30.  
  31. return n;
  32. }
  33.  
  34. double* newCapasity(double arr[], int len, int newLen) {
  35. double* arr2 = (double*)realloc(arr, (newLen) * sizeof(double));
  36.  
  37. if (arr2 == NULL) {
  38. printf("Error recapasity");
  39. exit(1);
  40. }
  41. int i;
  42.  
  43. for (int i = 0; i < len; i++)
  44. {
  45. arr2[i] = arr[i];
  46. }
  47.  
  48. for (i = len; i < (newLen); i++) {
  49. arr[i] = 2.00;
  50. }
  51.  
  52. return arr2;
  53. }
  54.  
  55. int main()
  56. {
  57. int newGrades;
  58. int grades = gradesNumbers();
  59. int i;
  60. double* arr = arrayCapasityAndIni(grades);
  61.  
  62. for (i = 0; i < grades; i++) {
  63. printf("arr[%d]: %.2f\n", i + 1, arr[i]);
  64. }
  65.  
  66. printf("New grades: ");
  67. scanf("%d", &newGrades);
  68.  
  69. double* newArr = newCapasity(arr, grades, newGrades);
  70.  
  71. for (i = 0; i < newGrades; i++) {
  72. printf("arr[%d]: %.2f\n", i + 1, newArr[i]);
  73. }
  74.  
  75. free(arr);
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement