Exercise 6 Arrays
Mar 19, 2016
Exercise 6
Arrays
Arrays
A block of many variables of the same type Array can be declared for any type
E.g. int A[10] is an array of 10 integers. Examples:
list of students’ marks series of numbers entered by user vectors matrices
Arrays in Memory
Sequence of variables of specified type The array variable itself holds the address in
memory of beginning of sequence Example: double S[10];
The k-th element of array A is specified by A[k-1] (0-based)
0 1 2 3 4 5 6 7 8 9
S
……
Example - reverse#include <stdio.h>
int main(void){ int i, A[10];
printf("please enter 10 numbers:\n"); for(i=0; i<10; i++) scanf("%d", &A[i]);
printf("numbers in reversed order:\n"); for(i=9; i>=0; i--) printf("%d\n", A[i]);
return 0;}
Define
Magic Numbers (like 10 in the last example) in the program convey little information to the reader
Hard to change in a systematic way #define defines a symbolic name During preprocessing phase, symbolic
names are replaced by the replacement text
Reverse with #define
/* get 10 integers from the user and printing them in reversed order*/
#include <stdio.h>
#define NUM 10
int main(void){ int i; int A[NUM];
printf(“Please enter %d numbers:\n",NUM); for (i=0; i<NUM; i++) scanf("%d",&A[i]);
printf("numbers in reversed order:\n"); for (i=NUM-1; i>=0; i--) printf("%d\n",A[i]);
Like in the case of regular variables, we can initialize the array during declaration.
the number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are
initialized to 0
Initialization
if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations :
int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};
Initialization
Example
Sort.c
Sort – step by step
#include <stdio.h>
#define SIZE 3
int main(void){
int A[SIZE] = {4,8,2}; int min_index;int i,j,tmp;
4 8 2A[0] A[1] A[2]
min_index----
---- ---- ----i j tmp
Sort – step by step
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
4 8 2A[0] A[1] A[2]
min_index----
0 ---- ----i j tmp
Sort – step by step4 8 2A[0] A[1] A[2]
min_index0
0 ---- ----i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step4 8 2A[0] A[1] A[2]
min_index0
0 1 ----i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step4 8 2A[0] A[1] A[2]
min_index0
0 1 ----i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step4 8 2A[0] A[1] A[2]
min_index0
0 2 ----i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step4 8 2A[0] A[1] A[2]
min_index2
0 2 ----i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step4 8 2A[0] A[1] A[2]
min_index2
0 3 ----i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step4 8 2A[0] A[1] A[2]
min_index2
0 3 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 2A[0] A[1] A[2]
min_index2
0 3 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index2
0 3 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index2
1 3 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index1
1 3 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index1
1 2 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index2
1 2 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index2
1 3 4i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 8 4A[0] A[1] A[2]
min_index2
1 3 8i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 4 4A[0] A[1] A[2]
min_index2
1 3 8i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 4 8A[0] A[1] A[2]
min_index2
1 3 8i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Sort – step by step2 4 8A[0] A[1] A[2]
min_index2
2 3 8i j tmp
for(i=0; i<SIZE-1; i++){
min_index=i;for(j=i+1; j<SIZE; j++)
min_index=((A[min_index]>A[j] ? j) : min_index);
tmp=A[i];A[i]=A[min_index];A[min_index]=tmp;
}
Exercise
Write a program that gets an input line from the user (ends with ‘\n’) and displays the number of times each letter appears in it.
Example:For the input line: hello, world!The output should be:d – 1e – 1h – 1l – 3o – 2w – 1r – 1
Assume that the input is all in lower-case.
Solution
letter_count.c
Multi-dimensional arrays
Array of arrays:int A[2][3] = { {1, 2, 3}, {4, 5, 6} };
Means an array of 2 integer arrays, each of length 3.
Access: j-th element of the i-array is A[i][j]
Multi-dimensional arrays
The size of the array can be determined by the compiler:
double B[][2] = {{1,2}, {2,3}, {3,4}};
Cannot skip this!!
Example: matrix addition#include <stdio.h>
#define SIZE 3
int main() {
double A[][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}};double B[][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}};double C[SIZE][SIZE];int i, j;
for(i=0; i<SIZE; i++) for(j=0; j<SIZE; j++) C[i][j]=A[i][j] + B[i][j];
return 0;}
Exercise
Write a program that defines 3 matrices A,B,C of size 3x3 with float elements; initialize the first two matrices (A and B)
Compute the matrix multiplication of A and B and store it in C (i.e. C = A*B)
Print all the matrices on the screen
Arrays as function arguments
Functions can accept arrays as arguments
Usually the array’s size also needs to be passed (why?)
Arrays as function arguments
For example:int CalcSum(int arr[], int size);
Within the function, arr is accessed in the usual way
Changes in the function affect the original array!!
Example
calc_sum.c
Exercise
Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise
Write a program that accepts two arrays of integers from the user and checks for equality
Solution
compare_arrays.c