מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02...

29
םםםםםם( arrays ) 15/06/22 21:21 1 Department of Computer Science-BGU

Transcript of מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02...

Page 1: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

מערכים(arrays)

21/04/23 07:36

1

Department of Computer Science-BGU

Page 2: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Problems with simple variables

Hard to give up values for high number of variables.

Complex to sort a high number of variables by value.

Impossible to use loops .

21/04/23 07:36

2

Department of Computer Science-BGU

Page 3: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

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

21/04/23 07:36

3

Department of Computer Science-BGU

Page 4: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Arrays in Memory

Sequence of variables of specified typeThe array variable itself holds the address in

memory of beginning of sequenceExample: 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

……

21/04/23 07:36

4

Department of Computer Science-BGU

Constant integer type

Page 5: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Example - reverse

#include <stdio.h>void main() { 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]);}

Constant integer type

21/04/23 07:36

5

Department of Computer Science-BGU

Page 6: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

#define

#define defines a symbolic name.

During preprocessing phase, symbolic names are replaced by the replacement text.

21/04/23 07:36

6

Department of Computer Science-BGU

Page 7: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Reverse with #define

/* get 10 integers from the user and printing them in reversed order*/

#include <stdio.h>#define NUM 10

void main() { 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]);} 21/04/23 07:36

7

Department of Computer Science-BGU

Page 8: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Will it work?

Example :#include <stdio.h>#define NUM 10

void main() { int i = 10; int A[i];

… NO !! Need a constant integer type , no

variable !

21/04/23 07:36

8

Department of Computer Science-BGU

Page 9: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Initialization

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 lessin which case, the remaining elements are initialized to 0

The next declarations:int array1[5] = {0};

int array2[8] = {2, 4, 6, 8}; meaning:

int array1[5] = {0,0,0,0,0};int array2[] = {2, 4, 6, 8,0,0,0,0};

21/04/23 07:36

9

Department of Computer Science-BGU

Page 10: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Initialization (cont.)

If you like, the array size can be inferred from the number of initializers.

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

21/04/23 07:36

10

Department of Computer Science-BGU

Page 11: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Bubble Sort

#include <stdio.h>  void main() { 

int array[] = {78,73,63,62,58,45,34,11};  int i,temp, n = 8 , flag;  do {

flag = 0;for ( i = 0 ; i < n-1 ; i++) {

if ( array[i] > array[i+1] ) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; flag = 1;

} }

} while (flag);  }

21/04/23 07:36

11

Department of Computer Science-BGU

Page 12: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Insertion Sort

#include <stdio.h> int main() {

int array[]= {12 ,3,34,14,98,33,9,17}; int temp,loops = 0 ,j, i, n = 8; for ( i = 1 ; i < n ; i++) {

temp = array[i];j = i-1;loops++;while (( j >=0) && ( temp < array[j])) {

array[j+1] = array[j];j--;loops++;

}array[j+1] = temp;} 

}

21/04/23 07:36

12

Department of Computer Science-BGU

Page 13: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Selection Sort

void selection_sort(int list[], int n) {    int i, j, min;   for (i = 0; i < n - 1; i++)   {      min = i;      for (j = i+1; j < n; j++)    {         if (list[j] < list[min]) {                      min = j;         }      }    temp = list[i]; list[i] = list[min]); list[min] = list[i]; } }

21/04/23 07:36

13

Department of Computer Science-BGU

Page 14: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Two Dimensional Arrays

We sometimes want to keep an inherent Two-Dimensional structure of data.

Example: We can define a two-dimensional 3x3 matrix

by

double A[3][3];

21/04/23 07:36

14

Department of Computer Science-BGU

Page 15: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Two 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-th array is

A[i][j]

21/04/23 07:36

15

Department of Computer Science-BGU

Page 16: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Initialization Two Dimensional Arrays

When initializing the array, it is necessary to indicate the size of the second dimension:

double B[][2] = {{1,2}, {2,3}, {3,4}};

Filling with zeros (0) is similar to one dimension array.

21/04/23 07:36

16

Department of Computer Science-BGU

Page 17: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Exercise

jkBkiAjiCn

k

*1

0

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:

Print all the matrices on the screen

21/04/23 07:36

17

Department of Computer Science-BGU

Page 18: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Solution

#include <stdio.h>#define N 3void main(){

int a[N][N],b[N][N],c[N][N]={0};int i,j,k;for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d%d",&a[i][j],&b[i][j]);for(i=0;i<N;i++) for(j=0;j<N;j++)

for(k=0;k<N;k++) c[i][j]+= a[i][k]*b[k][j];

printf("\n\n");

for(i=0;i<N;i++){for(j=0;j<N;j++)

printf("%4d",a[i][j]);printf("\n");

{

for(i=0;i<N;i++){for(j=0;j<N;j++)

printf("%4d",b[i][j]);printf("\n");

}for(i=0;i<N;i++){

for(j=0;j<N;j++)printf("%4d",c[i][j]);

printf("\n");{

{

21/04/23 07:36Department of Computer Science-BGU

18

Page 19: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Strings in C

21/04/23 07:36

19

Department of Computer Science-BGU

Page 20: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

String

A sequence of characters.Stored, as might be expected, in an array

of chars.Another way to initialize: char A[]=“blabla”;

Problem : It may be much shorter

than the array where it’s stored

How can we know where a string end ?!

21/04/23 07:36

20

Department of Computer Science-BGU

Page 21: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

The Terminator

Strings terminate with NULL character, signed by ‘\0’ (ASCII code 0).

This is a convention used to know where the string ends.

It means that in order to hold a string of 7 chars we need an array of length at least 8

So the previous initialization : char A[]=“blabla”;

is equivalent to char A[] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\

0’};

21/04/23 07:36

21

Department of Computer Science-BGU

Page 22: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Printing strings

printf allows printing whole strings at once using %s –

char str[200];/* … */printf(“%s\n”, str);

This will print the contents of str ,cell by cell, until a ‘\0’ is encountered this may be less than 200, but also more

21/04/23 07:36

22

Department of Computer Science-BGU

Page 23: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

The fool on the null

#include <stdio.h>void main() { char str[]="I'm a full string"; printf("%s\n",str); str[7]='o'; str[8]='o'; printf("%s\n",str); str[11]='\0'; printf("%s\n",str); str[11] = ‘s’; printf("%s\n", str);}

21/04/23 07:36

23

Department of Computer Science-BGU

Page 24: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Reading-in strings

There are several ways of accepting strings as input from the user.

The obvious way to go is read character by character using getchar()

21/04/23 07:36

24

Department of Computer Science-BGU

Page 25: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Reading-in strings - scanf

A simpler way is to use scanfTo read in a string to a variable str we can use:

scanf(“%s”, str); Note there’s no ‘&’ sign!!!

Scanf reads-in letters until a space or newline is encountered

The maximum length can be stated in the parentheses – scanf(“%10s”, str); This will read in 10 letters, plus the ‘\0’ sign (so str should

have place for 11 characters)

21/04/23 07:36

25

Department of Computer Science-BGU

Page 26: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Reading-in strings – gets)(

An other simpler way is to use gets()To read in a string to a variable str we can use:

gets( str); Note there’s no ‘&’ sign too!!!

gets reads-in letters until a newline (pressing enter on the keyboard) is encountered.

The maximum length can be stated for the array of characters declaration minus 1.

gets change the new line for the `\0` sign as last character in the string.

21/04/23 07:36

26

Department of Computer Science-BGU

Page 27: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Writing out strings – puts)(

A simpler way to write out a string is to use puts()To write out a string that is into variable str we

can use: puts( str);

puts write out letters until a `\0` sign is encountered.

puts change the `\0` sign for new line as the last character in the string.

21/04/23 07:36

27

Department of Computer Science-BGU

Page 28: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

Comparing strings

We cannot just compare strings’ contents by char A[7]=“Hello”; char B[7]=“Hello”; if(A==B) { … }Because A and B are addresses of A[0] and B[0]

A==B only if A and B are the same string in memory In order to compare the contents we must scan char by

char

‘H’

‘e’

‘l’ ‘l’ ‘o’

\‘0’ .…

B

….

….

‘H’

‘e’

‘l’ ‘l’ ‘o’

\‘0’ .…

A

….

….

21/04/23 07:36

28

Department of Computer Science-BGU

Page 29: מערכים (arrays) 02 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 1502 דצמבר 15 02 דצמבר 15 02 דצמבר 15 1 Department

String library

Like in the case of stdio.h and math.h, we have a special library for handling strings

We should #include <string.h>Functions:

strlen(s) – returns the length of s strcmp(s1, s2) – compares s1 with s2 strcpy(s1, s2) – copies to contents of s2 to s1 strcat(s1 , s2) – add the s1 at the end of s2 and more…

21/04/23 07:36

29

Department of Computer Science-BGU