| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /*
- * Please fill the functions in this file.
- */
- #include <stdio.h>
- #include "pa01.h"
- /* == Note 1 :: C does not initialize variables for you ==
- *
- * Please keep in mind that C does not initialize variables.
- *
- * int sum;
- *
- * is different from
- *
- * int sum = 0;
- *
- * The value of sum is undefined (garbage) in the first case.
- */
- /* == Note 2 :: Arrays are zero-indexed ==
- *
- * If an array has "length" elements, then valid indices are between 0
- * and length - 1, not between 1 and length. Thus, array[length] will
- * access an invalid memory location and probably crash the program.
- */
- /* addElement: sums the elements of an array of integers
- Parameters:
- array: an array of integers
- length: the length of the array (i.e. the number of elements)
-
- Returns:
- sum: the sum of the integers in the array
- This function returns the sum of the elements.
- For example, if
- array[0] is 3
- array[1] is 11
- array[2] is 9
- and length is 3
- This function should return 3 + 11 + 9 = 23.
- If the array has no elements (length is 0), then the function returns 0.
- */
- int addElement(int * array, int length)
- {
- int sum = 0;
-
- int i = 0;
- for(i=0; i < length; i++)
- {
- sum+=array[i];
- }
-
- return sum;
- }
- /*
- * =================================================================
- */
- /* countNegative: how many negatives values there are in an array
- Parameters:
- array: an array of integers
- length: the length of the array (i.e. the number of elements)
- Returns:
- This function returns the number of negative elements in the array
- For example, if
- array[0] is -3
- array[1] is 11
- array[2] is -9
- array[3] is 0
- and length is 4
- This function should return 2.
- 0 is not considered to be a negative number.
- */
- int countNegative(int * array, int length)
- {
- int count = 0;
-
- int i = 0;
- for(i=0; i < length; i++)
- {
- if(array[i] < 0)
- {
- count++;
- }
- }
-
- return count;
- }
- /*
- * =================================================================
- */
- /* isIncreasing: Is an array of integers always increasing?
- Parameters:
- array: an array of integers
- length: the length of the array (i.e. the number of elements)
-
- Returns:
- This function returns 1 if the elements are increasing
- (i.e. array[i] < array[j] for all 0 <= i < j < length). If the
- elements are not increasing, this function returns 0.
- If the array has only one element (i.e. length is 1), the function
- returns 1. If the array has no elements (length is 0), the function
- also returns 1.
- Examples:
- If
- array[0] is 3
- array[1] is 9
- array[2] is 11
- and length is 3
- This function should return 1
-
- If
- array[0] is 3
- array[1] is 11
- array[2] is 9
- and length is 3
- This function should return 0
- If
- array[0] is 3
- array[1] is 3
- array[2] is 9
- and length is 3
- This function should return 0
- */
- int isIncreasing(int * array, int length)
- {
- int i = 0;
- for(i=0; i < length - 1; i++)
- {
- if(array[i] >= array[i + 1])
- {
- return 0;
- }
- }
- return 1;
- }
|