Top Banner
Exercise 6 Arrays
40

Exercise 6

Mar 19, 2016

Download

Documents

clodia

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. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exercise 6

Exercise 6

Arrays

Page 2: Exercise 6

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

Page 3: Exercise 6

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

……

Page 4: Exercise 6

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;}

Page 5: Exercise 6

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

Page 6: Exercise 6

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]);

Page 7: Exercise 6

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

Page 8: Exercise 6

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

Page 9: Exercise 6

Example

Sort.c

Page 10: Exercise 6

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

Page 11: Exercise 6

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

Page 12: Exercise 6

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;

}

Page 13: Exercise 6

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;

}

Page 14: Exercise 6

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;

}

Page 15: Exercise 6

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;

}

Page 16: Exercise 6

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;

}

Page 17: Exercise 6

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;

}

Page 18: Exercise 6

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;

}

Page 19: Exercise 6

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;

}

Page 20: Exercise 6

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;

}

Page 21: Exercise 6

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;

}

Page 22: Exercise 6

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;

}

Page 23: Exercise 6

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;

}

Page 24: Exercise 6

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;

}

Page 25: Exercise 6

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;

}

Page 26: Exercise 6

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;

}

Page 27: Exercise 6

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;

}

Page 28: Exercise 6

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;

}

Page 29: Exercise 6

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;

}

Page 30: Exercise 6

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.

Page 31: Exercise 6

Solution

letter_count.c

Page 32: Exercise 6

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]

Page 33: Exercise 6

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!!

Page 34: Exercise 6

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;}

Page 35: Exercise 6

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

Page 36: Exercise 6

Arrays as function arguments

Functions can accept arrays as arguments

Usually the array’s size also needs to be passed (why?)

Page 37: Exercise 6

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!!

Page 38: Exercise 6

Example

calc_sum.c

Page 39: Exercise 6

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

Page 40: Exercise 6

Solution

compare_arrays.c