1
0

answer01.c 2.7 KB

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