Transcript tirgul6

Agenda
Arrays:






Definition
Memory
Examples
Passing arrays to functions
Multi dimensional arrays
1
Arrays
Array: sequential block of memory that
holds variables of the same type
Array can be declared for any type



Example: int A[10] is an array of 10 integers.
Examples:





list of students’ marks
series of numbers entered by user
vectors
matrices
2
Arrays in Memory



Sequence of variables of specified type
The array variable itself holds the address in
memory of beginning of sequence
Example:
… 0 1 2 3 4 5 6 7 8
double s[10];
9
…
s

The k-th element of array A is specified by
A[k-1] (0 based)
3
Arrays in Memory


Access array’s content
Change array’s content
4
Example – Find Minimum
int i, min, array[10];
printf("please enter 10 numbers:\n");
for(i = 0; i < 10; ++i)
scanf("%d", &array[i]);
min = array[0];
for(i = 1; i < 10; ++i) {
if (array[i] < min)
min = array[i];
}
printf("the minimum is: %d\n", min);
5
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
6
minimum with #define
#include <stdio.h>
#define ARRAY_SIZE 10
int main(void)
Use capital letters
{
int i, min, array[ARRAY_SIZE];
printf("please enter %d numbers:\n", ARRAY_SIZE);
for(i = 0; i < ARRAY_SIZE; ++i)
scanf("%d", &array[i]);
min = array[0];
for(i = 1; i < ARRAY_SIZE; ++i) {
if (array[i] < min)
min = array[i];
}
printf("the minimum is: %d\n", min);
return 0;
}
7
Initialization



Can be initialized 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
if you like, the array size can be inferred from the number of
initializers (not recommended)
 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};
8
Exercise
Write a program that gets 10 numbers from the user.
It then accepts another number and checks to see if
that number was one of the previous ones.
Example 1:
Please enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
Please enter a number to search for: 8
I found it!
Example 2:
Please enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
Please enter a number to search for: 30
Sorry, it’s not there.
9
Solution (simple_search.c)
#include <stdio.h>
#define ARRAY_SIZE 10
int main(void)
{
int array[ARRAY_SIZE], i, num;
printf("Please enter %d numbers:\n", ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; ++i)
scanf("%d", &array[i]);
printf("Please enter a number to search for:\n");
scanf("%d", &num);
for (i = 0; i < ARRAY_SIZE; ++i) {
if (array[i] == num) {
printf("I found it!\n");
return 0;
}
}
printf("Sorry, it's not there\n");
return 0;
}
10
Some Notes on Array




Arrays versus basic variables
Out of range
Equality: do not use the operator ‘=‘ on
arrays (until we learn otherwise…).
Copy instead
Size is always a constant (until we learn
otherwise…)
11
Agenda
Arrays:






Definition
Memory
Examples
Passing arrays to functions
Multi dimensional arrays
12
Arrays as function arguments


Functions can accept arrays as
arguments
The array’s size also needs to
be passed (why?)
13
Arrays as function arguments

For example:
int calc_sum(int arr[], int size);


Within the function, arr is accessed in
the usual way
Changes to the array in the function
change the original array! (why?)
14
Example (mult_all.c)
void mult_all(int arr[], int size, int multiplier)
{
int i = 0;
for (i = 0; i < size; ++i)
arr[i] *= multiplier;
}
int main()
{
int i,array[]={1,2,3,4,5,6,7,8,9,10};
mult_all(array,10,5);
printf(“array is now:\n”);
for (i=0; i<10; i++)
printf(“%d “,array[i]);
printf(“\n”);
return 0;
}
15
Example - Sort

We would like to sort the elements in
an array in an ascending order.
7 2 8 5 4
sort
2 4 5 7 8
16
Example of Sort (Bubble Sort)
7 2 8 5 4
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 7 8 5 4
2 7 5 4 8
2 5 4 7 8
2 4 5 7 8
2 7 8 5 4
2 5 7 4 8
2 4 5 7 8
2 7 5 8 4
2 5 4 7 8
(done)
2 7 5 4 8
17
Bubble Sort
void sort(int a[], int size)
{
int i, j, temp;
for (i = size - 1; i >= 0; --i) /* counting down
*/
{
for (j = 0; j < i; ++j)
/* bubbling up
*/
{
if (a[j] > a[j+1])
/* if out of order... */
{
/* ... then swap */
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
18
Using sort
#include <stdio.h>
#define ARRAY_SIZE 5
void sort(int a[], int size);
int main()
{
int array[ARRAY_SIZE] = {7, 2, 8, 5, 4};
int i = 0;
sort(array, ARRAY_SIZE);
/* print the sorted array */
for (i = 0; i < ARRAY_SIZE; ++i)
printf("%d ", array[i]);
return 0;
}
19
Exercise (@ home?)


Implement a function that accepts two
integer arrays and returns 1 if they are
equal, 0 otherwise.
The arrays are of the same size
Write a program that accepts two arrays
of integers from the user and checks for
equality
20
Solution (compare_arrays.c)
int compare_arrays(int arr1[], int arr2[], int size)
{
int i = 0;
/* compare the elements one at a time */
for (i = 0; i < size; ++i)
{
if (arr1[i] != arr2[i])
return 0;
}
/* if we got here, both arrays are identical */
return 1;
}
21
Agenda
Arrays:






Definition
Memory
Examples
Passing arrays to functions
Multi dimensional arrays
22
Multi-dimensional arrays

Array of arrays:
int A[2][3] = { {1, 2, 3},
{4, 5, 6} };
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]
23
Multi-dimensional arrays

The size of the array can be determined
by the compiler (not recommended):
int B[][2] = {{1,2}, {2,3}, {3,4}};
Cannot skip this!!
24
Example: matrix addition
#include <stdio.h>
#define SIZE 3
int main()
{
int
int
int
int
A[SIZE][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}};
B[SIZE][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}};
C[SIZE][SIZE];
i = 0, j = 0;
for (i = 0; i < SIZE; ++i)
for (j = 0; j < SIZE; ++j)
C[i][j] = A[i][j] + B[i][j];
return 0;
}
25
2D arrays as function
arguments

The second subscript must be specified
and it must be constant
void print_matrix(int mat[3][3])
{
int i, j;
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
printf("\t%d ", mat[i][j]);
printf("\n");
}
}
26
Exercise @ home



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)
Matrix Multiplication:
n 1
C i  j    Ai k * Bk  j 
k 0

Print all the matrices on the screen
27
Solution
mat_mul.c
28
Debugger (if time allows)
Use the debugger on one of the
above examples, see that you can
follow execution step by step and see
the variables values at each stage
29
More time (no chance)?
Talk about the loops-related
questions from 2 weeks ago
30