answer01.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. sum: the sum of the integers in the array
  30. This function returns the sum of the elements.
  31. For example, if
  32. array[0] is 3
  33. array[1] is 11
  34. array[2] is 9
  35. and length is 3
  36. This function should return 3 + 11 + 9 = 23.
  37. If the array has no elements (length is 0), then the function returns 0.
  38. */
  39. int addElement(int * array, int length)
  40. {
  41. int sum = 0;
  42. int i = 0;
  43. for(i=0; i < length; i++)
  44. {
  45. sum+=array[i];
  46. }
  47. return sum;
  48. }
  49. /*
  50. * =================================================================
  51. */
  52. /* countNegative: how many negatives values there are in an array
  53. Parameters:
  54. array: an array of integers
  55. length: the length of the array (i.e. the number of elements)
  56. Returns:
  57. This function returns the number of negative elements in the array
  58. For example, if
  59. array[0] is -3
  60. array[1] is 11
  61. array[2] is -9
  62. array[3] is 0
  63. and length is 4
  64. This function should return 2.
  65. 0 is not considered to be a negative number.
  66. */
  67. int countNegative(int * array, int length)
  68. {
  69. int count = 0;
  70. int i = 0;
  71. for(i=0; i < length; i++)
  72. {
  73. if(array[i] < 0)
  74. {
  75. count++;
  76. }
  77. }
  78. return count;
  79. }
  80. /*
  81. * =================================================================
  82. */
  83. /* isIncreasing: Is an array of integers always increasing?
  84. Parameters:
  85. array: an array of integers
  86. length: the length of the array (i.e. the number of elements)
  87. Returns:
  88. This function returns 1 if the elements are increasing
  89. (i.e. array[i] < array[j] for all 0 <= i < j < length). If the
  90. elements are not increasing, this function returns 0.
  91. If the array has only one element (i.e. length is 1), the function
  92. returns 1. If the array has no elements (length is 0), the function
  93. also returns 1.
  94. Examples:
  95. If
  96. array[0] is 3
  97. array[1] is 9
  98. array[2] is 11
  99. and length is 3
  100. This function should return 1
  101. If
  102. array[0] is 3
  103. array[1] is 11
  104. array[2] is 9
  105. and length is 3
  106. This function should return 0
  107. If
  108. array[0] is 3
  109. array[1] is 3
  110. array[2] is 9
  111. and length is 3
  112. This function should return 0
  113. */
  114. int isIncreasing(int * array, int length)
  115. {
  116. int i = 0;
  117. for(i=0; i < length - 1; i++)
  118. {
  119. if(array[i] >= array[i + 1])
  120. {
  121. return 0;
  122. }
  123. }
  124. return 1;
  125. }