answer01.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Please fill the functions in this file.
  3. */
  4. #include "pa01.h"
  5. /*
  6. * C does not initialize variables.
  7. *
  8. * int sum;
  9. *
  10. * is different from
  11. *
  12. * int sum = 0;
  13. *
  14. * The value of sum is undefined (garbage) in the first case.
  15. *
  16. */
  17. /*
  18. array is an array of integers
  19. length is the length of the array (i.e. the number of elements)
  20. If an array has length elements. The valid indexes are between 0
  21. and length - 1, not between 1 and length. array[length] will
  22. access an invalid memory location and possibly terminate the
  23. program abnormally.
  24. This function returns the sum of the elements.
  25. For example, if
  26. array[0] is 3
  27. array[1] is 11
  28. array[2] is 9
  29. and length is 3
  30. This function should return 3 + 11 + 9 = 23.
  31. If the array has no element (length is 0), the function returns 0.
  32. */
  33. int addElement(int * array, int length)
  34. {
  35. return 0;
  36. }
  37. /*
  38. * =================================================================
  39. */
  40. /*
  41. array is an array of integers
  42. length is the length of the array (i.e. the number of elements)
  43. If an array has length elements. The valid indexes are between 0
  44. and length - 1, not between 1 and length. array[length] will
  45. access an invalid memory location and possibly terminate the
  46. program abnormally.
  47. This function returns the number of negative elements in the array
  48. For example, if
  49. array[0] is -3
  50. array[1] is 11
  51. array[2] is -9
  52. array[3] is 0
  53. and length is 4
  54. This function should return 2.
  55. 0 is not considered as a negative number.
  56. */
  57. int countNegative(int * array, int length)
  58. {
  59. return 0;
  60. }
  61. /*
  62. * =================================================================
  63. */
  64. /*
  65. array is an array of integers
  66. length is the length of the array (i.e. the number of elements)
  67. If an array has length elements. The valid indexes are between 0
  68. and length - 1, not between 1 and length. array[length] will
  69. access an invalid memory location and possibly terminate the
  70. program abnormally.
  71. This function returns 1 if the elements are increasing
  72. (i.e. array[i] < array[j] for all 0 <= i < j < length). If the
  73. elements are not increasing, this function returns 0.
  74. For example, if
  75. array[0] is 3
  76. array[1] is 9
  77. array[2] is 11
  78. and length is 3
  79. This function should return 1.
  80. If
  81. array[0] is 3
  82. array[1] is 11
  83. array[2] is 9
  84. and length is 3
  85. This function should return 0.
  86. If
  87. array[0] is 3
  88. array[1] is 3
  89. array[2] is 9
  90. and length is 3
  91. This function should return 0.
  92. If the array has only one element (i.e. length is 1), the function
  93. returns 1. If the array has no element (length is 0), the function
  94. also returns 1.
  95. */
  96. int isIncreasing(int * array, int length)
  97. {
  98. return 0;
  99. }