You are on page 1of 4

CS1010E Programming Methodology Semester 1 2011/2012 Week of 17 21 October 2011 Tutorial 8 Arrays 1. What is printed by the following program?

? You are advised to hand-trace the program to obtain the answer before running the program to verify.
#include <stdio.h> void void void void void passElement(int num); changeElements(int list[]); copyArray(int list1[], int mylist2[], int numElem); printArray(int list[], int numElem); printPattern(int list[], int numElem);

int main(void) { int list1[] = {11, 22, 33, 44, 55}; int list2[] = {99, 99, 99, 99, 99}; printf("Original array:\n"); printArray(list1, 5); passElement(list1[0]); printf("After passing one element:\n"); printArray(list1, 5); changeElements(list2); printf("After changing individual elements:\n"); printArray(list2, 5); copyArray(list2, list1, 5); printf("After copying each array element:\n"); printArray(list2, 5); printf("Pattern printing...\n"); printPattern(list1,5); return 0; } void passElement(int num) { num = 1234; return; }

void changeElements(int list[]) { list[2] = 77; list[4] = 88; return; } void copyArray(int list1[], int list2[], int numElem) { int i; for (i = 0; i < numElem; i++) list1[i] = list2[i]; return; } void printArray(int list[], int numElem) { int i; for (i = 0; i < numElem; i++) printf("%d ", list[i]); printf("\n"); return; } void printPattern(int list[], int numElem) { int i; for (i = 1; i <= numElem; i++) printArray(list+(numElem-i),i); return; }

2. We would like to nd out the statistics of the recent mid-semester test. The marks of all students who took the test are randomly stored in the le marks.txt. (a) Within the main function, declare an integer array marks to store all the marks using le input. Assume that the class size for the module will not exceed 1000. (b) The mean (or average) value, , is given by =
n1 k=0

xk

Write a function mean that takes in the array of marks and the number of elements to process, n. The function returns the mean mark. double mean(int marks[], int n); 2

(c) The population variance, 2 , is the average squared deviation from the mean. 2 =
n1 k=0

(xk )2 n

Write a function variance that takes in the array of marks and the number of elements to process, n. The function returns the variance. double variance(int marks[], int n); (d) The standard deviation, , is the square root of the variance. Write a function stdDev that takes in the array of marks and the number of elements to process, n. The function returns the standard deviation. double stdDev(int marks[], int n); (e) The mode is the value that occurs most frequently in the data. Write a function mode that takes in the array of marks and the number of elements to process, n. The function returns the mode. int mode(int marks[], int n); Hint: Create a frequency table to track the number of occurrences of marks ranging from 0 to 10. You need only a single pass through the array. 3. Conways Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970 that has attracted much interest, because of the surprising ways in which patterns can evolve. In this exercise, we shall look at a one-dimensional version of the game of life. A population can be represented by a row of cells where each cell represents an organism. Each cell may be alive or dead. Assume a population of nine cells with only one living organism (marked x) in the initial state (or generation). x For each generation, a cell is alive or dead depending on its own previous state and the previous states of the two neighbour cells. We adopt the following rules: If a cell is alive in one generation, it will be dead in the next. If a cell is dead in one generation, but has one and only one live neighbour cell, it will be alive in the next generation. Applying the above rules to the initial generation above, we obtain the next generation x x

Applying the rules again will generate x x

For a population size of 63, starting with a single organism * in the middle, the printout of the rst 32 generations is below. 3

* * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

The following program is given to you. #include <stdio.h> #define MAX 80 int main(void) { int cell[MAX] = {0}, n, gen, i; printf("Enter starting location and number of generations: "); scanf("%d %d", &n, &gen); cell[n] = 1; printArray(cell,MAX); for (i = 2; i <= gen; i++) { generate(cell,MAX); printArray(cell,MAX); } return 0; } (a) Include the function generate that takes in the cell array in the current generation and modies the array to give the next generation. (b) Include the function printArray such that the current generation is output with an asterisk * indicating a living cell, and a blank indicating a dead cell. (c) Extra question: Modify your program to allow for a list of positions for multiple organisms in the rst generation (either read from the user or from a le), and print the pattern on the screen or writing to an output le. 4

You might also like