Chapter 6. Arrays – list of elements
6.1 Average program
main() { long float
n1, n2, n3, sum; avg; printf(“Enter three numbers: “); scanf(“%d %d %d”, &n1, &n2, &n3); sum = n1 + n2 + n3; avg = (float)sum / 3.0;
printf(“Average of the three numbers = %f\n”, avg);
}
• Three variables for three input numbers • How about 10 or 100 numbers?
Revised average program
#include #define NUMBERS 10 main() { int float
num[NUMBERS], sum, i; avg;
printf(“Enter three numbers: “); for (i=0,sum=0; i main() { int numbers[3] = {34, 29, 41};
printArray(numbers, 3);
} void printArray(int array[], nElements) { int i; for (i=0; i #define MAX 50 #define SENTINEL 0 #define FOREVER 1
int void void void void GetIntegerArray(int []); PrintIntegerArray(int [], int); ReverseIntegerArray(int [], int); SwapIntegerElements(int [], int, int); GiveInstructions(void);
int main(void) { int list[MAX], nNum; GiveInstructions(); nNum = GetIntegerArray(list); ReverseIntegerArray(list, nNum); PrintIntegerArray(list, nNum); }
Reversing array elements
/* * Display Instructions */
void GiveInstructions(void) { printf(“This program reads an array of maximum”); printf(“ %d integers\n”, MAX); printf(“and reverses and displays it\n\n”); printf(“Enter an array (0 to end) : “); }
Reversing array elements
/* * This function reads integers from keyboard and stores * it in the array. The end of the input is indicated by * the SENTINEL */
int GetIntegerArray(int array[]) { int nNum=0, inputNum; while (FOREVER) { scanf(“%d”, &inputNum); if (inputNum == SENTINEL) break; if (nNum == MAX) { printf(“Too many input!\n”); exit(1); } array[nNum++] = inputNum; } return(nNum); }
Reversing array elements
/* * Reverse an array */
void ReverseIntegerArray(int array[], int n) { int i; for (i=0; i #define NITEMS 8
void swap(int [], int, int); void printArray(int [], int); int main(void) { int array[NITEMS] = {7, 3, 66, 3, -5, 22, -77, 2}; int i, j;
printArray(array, NITEMS); for (i=0; i array[j]) swap(array, j-1, j);
printArray(array, NITEMS);
}
Sorting example – bubble sort
void swap(int array[], int i, int j) { int temp; temp = array[i]; array[i] = array[j]; array[j] = temp;
} void printArray(int array[], int n) { int i; for (i=0; i (3+3+7+2+6+11+1+1+3)/9 = 37/9 = 4.11
• Median
– The middle value ex) 3, 3, 7, 2, 6, 11, 1, 1, 3 -> 1, 1, 2, 3, 3, 3, 6, 7, 11
• Mode
– The value that occurs most frequently ex) 3, 3, 7, 2, 6, 11, 1, 1, 3 -> 3
Computing Mean, Median, and Mode
/* This program introduces the topic of survey data analysis. It computes the mean, median, and mode of the data*/ #include #define SIZE 99 void void void void void mean(int []); median(int []); mode(int [], int []); bubbleSort(int []); printArray(int []);
main() { int frequency[10] = {0}, response[SIZE] = {6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 4, 5, 6, 1, 6, 5, 7, 8, 7}; mean(response); median(response); mode(frequency, response); return 0; }
Computing Mean, Median, and Mode
• Function mean
– Totals the 99 elements and divides the result by 99
void mean(int answer[]) { int j, total = 0; printf(“%s\n%s\n%s\n”, “********”, “ Mean”, “********”); for (j = 0; j <= SIZE – 1; j++) total += answer[j];
printf(“The mean is the average value of the data\n”); “items. The mean is equal to the total of\n” “of data items (%d). The mean value for\n” “this run is : %d / %d = %.4f\n\n”, SIZE, total, SIZE, (float) total / SIZE);
}
Computing Mean, Median, and Mode
• Function median
– Calls function bubbleSort to sort the array into ascending order, and picks the middle element
void median(int answer[]) { printf(“\n%s\n%s\n%s\n%s”, “********”, “ Median”, “********”, “The unsorted array of responses is”); printArray(answer); bubbleSort(answer); printf(“\n\nThe sorted array is”); printArray(answer); printf(“\n\nThe median is element %d of\n” “the sorted %d element array. \n” “For this run the median is %d\n\n”, SIZE / 2, SIZE, answer[SIZE / 2]);
}
Computing Mean, Median, and Mode
• Function mode
– Counts the number of responses of each type, then selecting the value with greatest count – Produces a histogram to aid in determining the mode graphically
void mode(int freq[], int answer[]) { int rating, j, h, largest = 0, modeValue = 0;
printf(“\n%s\n%s\n%s\n”, “********”, “ Mode”, “********”); for (j = 0; j <= SIZE – 1; j++) ++freq[answer[j]];
/* see the next page */
Computing Mean, Median, and Mode
printf(“%s%11s%19s\n\n%54s\n%54s\n\n”, “Response”, “Frequency”, “Histogram”, “1 1 2 2”, “5 0 5 0 5”);
for (rating = 1; rating <= 9; rating++) { printf(“%8d%11d ”, rating, freq[rating]); if(freq[rating] > largest) { largest = freq[rating]; modeValue = rating; } for (h = 1; h <= freq[rating]; h++) printf(“*”); }
printf(“\n”);
}
printf(“The mode is the most frequent value.\n” “For this run the mode is %d which occurred” “ %d times. \n”, modeValue, largest);
6.7 Multi-dimensional arrays
• C allows any-dimensional arrays
– – – – int char int float a[10]; b[10][15]; c[31][6][55]; d[10][19]…[32]; /* /* /* /* 1-dimensional 2-dimensional 3-dimensional n-dimensional array array array array */ */ */ */
• Required memory
– – – – int char int float a[10]; b[10][15]; c[31][6][55]; d[10][19]…[32]; : : : : sizeof(int)*10 sizeof(char)*10*15 sizeof(int)*31*6*55 sizeof(float)*10*19*…*32
• Multi-dimensional arrays take much memory space, programmers must consider the amount of physical memory and array size
Multi-dimensional arrays
• Initialization
– int a[2][3] = { {1, 2, 3}, {4, 5, 6} }; – int b[2][3] = { {1, 2, 3, 4, 5} }; – int c[2][3] = { {1, 2}, {4} };
Array a[2][3]
1 4 2 5 3 6
b[2][3]
1 4 2 5 3 0
c[2][3]
1 4 2 0 0 0
• First subscript can be omitted
– int a[][3] = { {1, 2, 3}, {4, 5, 6} };
• Any method of initializations is okay, but use the more readable one if possible
6.8 Multiplication table
/* * Displays a multiplication table */ #include
int main(void) { int mtable[9][9]; int row, col;
for (row=0; row<9; row++) for (col=0; col<9; col++) mtable[row][col] = (row+1)*(col+1); printf(“ 1 2 3 4 5 6 7 8 9\n”); for (row=0; row<9; row++) { printf(“%2d “, row+1); for (col=0; col<9; col++) printf(“%2d “, mtable[row][col]); printf(“\n”); }
}
Multiplication table
#include void InitTable(int [][], int, int); int main(void) { int mtable[9][9]; int row, col;
InitTable(mtable, 9, 9); printf(“ 1 2 3 4 5 6 7 8 9\n”); for (row=0; row<9; row++) { printf(“%2d “, row+1); for (col=0; col<9; col++) printf(“%2d “, mtable[row][col]); printf(“\n”); } } void InitTable(int table[][9], int rows, int cols) { int i, j; for (i=0; i