// A program to demonstrate a simple array being read // through and printing out the elements of that array // Sasha Week 3 Lecture 6 // Let's say I want an array with the values 1, 3, 4, 5, 9, 10, 11 // Let's do some functions: // 1. Calculate the product of these numbers // 2. Calculate average of these numbers // 3. Calculate the sum of these numbers // 4. Count the multiples of 3 #include #define MAX_ARRAY 7 void print_array(int array[MAX_ARRAY]); int product_array(int array[MAX_ARRAY]); int average_array(int array[MAX_ARRAY]); void multiple_three(int array[MAX_ARRAY]); int main (void) { //int a = 1; //int b = 3; //int c = 4; //int product = 1; //int sum = 0; // 1. Declare and initialise the array that you will use here int array[MAX_ARRAY] = {1, 3, 4, 5, 9, 10, 11}; // Traverse through the array and print out the elements of the array /*int i = 0; while (i < MAX_ARRAY) { printf("%d ", array[i]); i++; }*/ /*for(int i = 0; i < MAX_ARRAY; i++){ printf("%d ", array[i]); } printf("\n"); */ print_array(array); // Traverse through the array and multiply the numbers together /*for (int i = 0; i < MAX_ARRAY; i++) { product = product * array[i]; } */ // Output the result (product of numbers) printf("The product of the numbers is %d\n", product_array(array)); // Calculate average of these numbers /*for (int i = 0; i < MAX_ARRAY; i++) { sum = sum + array[i]; } int average = sum / MAX_ARRAY; */ printf("The average of the numbers is %d\n", average_array(array)); // Calculate the sum of these numbers // printf("The sum of the numbers is %d\n", sum); // Count the multiples of 3 // printf("The number of multiples of 3 is %d\n", multiples); multiple_three(array); return 0; } // This function will print out the array // OUTPUT: void // INPUT: an array // Name: print_array void print_array(int array[MAX_ARRAY]) { for(int i = 0; i < MAX_ARRAY; i++){ printf("%d ", array[i]); } printf("\n"); } // This function will print out the product of array numbers // OUTPUT: product (int) // INPUT: array // Name: product_array int product_array(int array[MAX_ARRAY]) { int product = 1; for (int i = 0; i < MAX_ARRAY; i++) { product = product * array[i]; } return product; } // This function will print out the average of the array numbers // OUTPUT: int // INPUT: array // Name: average_array int average_array(int array[MAX_ARRAY]) { int sum = 0; for (int i = 0; i < MAX_ARRAY; i++) { sum = sum + array[i]; } int average = sum / MAX_ARRAY; return average; } // This function will print out the sum of the array numbers // OUTPUT: // INPUT: // Name: // This function will print out the multiples of 3 in the array // OUTPUT: void // INPUT: array // Name: multiple_three void multiple_three(int array[MAX_ARRAY]) { // Check if a number in the array / 3, if yes print it out for(int i = 0; i < MAX_ARRAY; i++) { if (array[i] % 3 == 0){ printf("%d ", array[i]); } } }