senem kumova metİn cs115 2008-2009 fall 1 arrays && sorting && strings chapter 6...

29
Senem KUMOVA METİN CS115 2008-2009 FA LL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont

Upload: draven-honn

Post on 14-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Senem KUMOVA METİN CS115 2008-2009 FALL

1

ARRAYS && SORTING && STRINGS

CHAPTER 6 cont

Senem KUMOVA METİN CS115 2008-2009 FALL

2

What is an Array?

int grade0, grade1, grade2;

int grade [3];

Arrays make it possible to represent large number of homogenous values

grade0 grade1 grade2

grade[0] grade[1] grade[2]

Senem KUMOVA METİN CS115 2008-2009 FALL

3

One Dimensional ArraysEXAMPLE

int grade[5];

data

datagrade[1]

datagrade[2]

data

data

grade[0]

grade[3]

grade[4]

MEMORY

first element in array

fift ( the last) element in array

starts from 0

ends at size - 1

Senem KUMOVA METİN CS115 2008-2009 FALL

4

Two Dimensional Arrays

int x[4][5];

column 0 column 1 column 2 …. column C-1

row 0 x[0][0] x[0][1] x[0][2] …. x[0][C-1]

row 1 x[1][0] x[1][1] x[1][2] …. x[1][C-1]

row 2 x[2][0] x[2][1] x[2][2] …. x[2][C-1]

row 3 x[3][0] x[3][1] x[3][2] …. x[3][C-1]

…. …. …. …. …. ….

row R-1 x[R-1][0] x[R-1][1] x[R-1][2] …. x[R-1][C-1]

Senem KUMOVA METİN CS115 2008-2009 FALL

5

Two Dimensional Array - Memoryx[0][0] data 0. row , 0. column

x[0][1] data 0.row , 1. column

x[0][2] data 0.row , 2. column

x[0][3] data 0.row , 3. column

x[1][0] data

x[1][1] data How can x be declared ???x[1][2] data

x[1][3] data

x[2][0] data 2. row , 0. column

x[2][1] data 2.row , 1. column

x[2][2] data 2.row , 2. column

x[2][3] data 2.row , 3. column

Senem KUMOVA METİN CS115 2008-2009 FALL

6

Two Dimensional Array Initialization

int y[2][3] = {1,2,3,4,5,6};

int y[2][3] ={{1,2,3},{4,5,6}};

int y[][3] = {{1,2,3},{4,5,6}};

1 2 3

4 5 6

Row 0

Row 1

Col 0 Col 1 Col 2

Number of rows

Number of columns

Senem KUMOVA METİN CS115 2008-2009 FALL

7

EXAMPLE: Two Dimensional Arrays

/* array2.c*/main(){

int x[5][4], r,c;for(r=0;r<5;r++)

for(c=0;c<4;c++){ x[r][c]= r*c ;

printf(“x[%d][%d] = %d\n”, r,c, x[r][c]); //what will be the output ???

}

}

Senem KUMOVA METİN CS115 2008-2009 FALL

8

EXAMPLE: Two Dimensional ArraysCOLUMNS

0 12 3

ROWS

0 0 0 0 01 0 1 2 3

2 0 2 4 6

3 0 3 6 94 0 4 8 12

REMEMBER !! COLUMN NUMBER WAS CHANGING FASTER !!

Senem KUMOVA METİN CS115 2008-2009 FALL

9

Multidimensional Arrays

int y[2][3]; // two dim. int z [2][7][3] // three dim int a[2][2][3] ={

{{1,1,0},{2,0,0}},

{{3,0,0},{4,4,0}} }

Senem KUMOVA METİN CS115 2008-2009 FALL

10

ARRAY SORTING : BUBBLE SORTSORT array decreasing

int a[6]={1,3,4,2,6,5} 

a[0] a[1] a[2] a[3] a[4] a[5]

  initial values in

array 1 3 4 2 6 5

X=0

Y=0 3 1 4 2 6 5

Y=1 3 4 1 2 6 5

Y=2 3 4 2 1 6 5

Y=3 3 4 2 6 1 5

Y=4 3 4 2 6 5 1

X=1

Y=0 4 3 2 6 5 1

Y=1 4 3 2 6 5 1

Y=2 4 3 6 2 5 1

Y=3 4 3 6 5 2 1

Y=4 4 3 6 5 2 1

X=2

Y=0 4 3 6 5 2 1

Y=1 4 6 3 5 2 1

Y=2 4 6 5 3 2 1

Y=3 4 6 5 3 2 1

Y=4 4 6 5 3 2 1

Senem KUMOVA METİN CS115 2008-2009 FALL

11

ARRAY SORTING : BUBBLE SORT

X=3

Y=0 6 4 5 3 2 1

Y=1 6 5 4 3 2 1

Y=2 6 5 4 3 2 1

Y=3 6 5 4 3 2 1

Y=4 6 5 4 3 2 1

X=4

Y=0 6 5 4 3 2 1

Y=1 6 5 4 3 2 1

Y=2 6 5 4 3 2 1

Y=3 6 5 4 3 2 1

Y=4 6 5 4 3 2 1

X=5

Y=0 6 5 4 3 2 1

Y=1 6 5 4 3 2 1

Y=2 6 5 4 3 2 1

Y=3 6 5 4 3 2 1

Y=4 6 5 4 3 2 1

Senem KUMOVA METİN CS115 2008-2009 FALL

12

ARRAY SORTING : BUBBLE SORTvoid swap(int *, int *);  /* swap defined before */

void bubble(int a[ ], int size) 

{

int x, y;

for(x =0; x < size; x++)

for(y = 0; y < size-1 ; y++)

if(a[y] < a[y+1])

{ swap(&a[y], &a[y+1]); }

}

main()

{ int a[6]={1,3,4,2,6,5};

bubble(a,6); }

Senem KUMOVA METİN CS115 2008-2009 FALL

13

ARRAY SORT : MERGE SORTrecursion

Senem KUMOVA METİN CS115 2008-2009 FALL

14

ARRAY SORT : SELECTION SORT#include<stdio.h>void selectionSort(int n [], int size){ int i, j; int min_index, temp;

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

min_index = i; for (j = i+1; j < size; j++) {

if (n [j] < n [min_index]) min_index = j; } temp = n [i]; n[i] = n[min_index]; n[min_index] = temp;

}}

main(){

int i;int a[7]={5,3,7,12,4,8,9};

for(i=0;i<7;i++)printf("%d

",a[i]);printf("\n");

selectionSort(a,7);

for(i=0;i<7;i++)printf("%d

",a[i]);

}

Senem KUMOVA METİN CS115 2008-2009 FALL

15

STRINGS One dimensional arrays of type char

String constants are terminated by the end of string sentinel \0 (null)

char st1[] = {‘a’,’b’,’c’,’\0’};printf(“%s”, st1);

char * st2 = “abc”;printf(“%s”, st2);

String constants are written between double quotes, e.g., "abc", "" (an empty string containing null character \0) “a” 2 elements ‘a’ 1 element

Senem KUMOVA METİN CS115 2008-2009 FALL

16

STRING HANDLING FUNCTIONS Requires #include <string.h>

SOME FUNCTIONS :

1. char *strcat(char *s1, const char *s2);  takes two strings as arguments, concatenates them, puts the results in s1, and returns string s1 char * d1=“abc”; char * d2=“def”;strcat(d1,d2)d1???? d2????

2. int strcmp(const char *s1, const char *s2); two strings are compared. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2

Senem KUMOVA METİN CS115 2008-2009 FALL

17

STRING HANDLING FUNCTIONS3. char *strcpy(char *s1, const char *s2); the characters in the string s2 are copied into string s1 until \0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer to s1 is returned.

4. size_t strlen(const char *s); a count of the number of characters before \0 is returned. ANSI C requires the type size_t to be an integral unsigned type.

Senem KUMOVA METİN CS115 2008-2009 FALL

18

STRING HANDLING FUNCTIONSchar d1[]= “ball hall”; char d2[] =“car toy”;

strlen(d1) 9strcmp(d1,d2) negative

printf(“%s”,d1+5) hallstrcpy(d1+5,d2+4) hall toy ( is it true?)

strcat(d1, “s!!”) hall toys!! ( is it true?)

b a l l h a l l \0

d1 d1+1 . . . d1+9

b a l l h a l l \0

d1+5

Senem KUMOVA METİN CS115 2008-2009 FALL

19

STRING HANDLING FUNCTIONS#include <stdio.h>#include<string.h>main(){ char d1[]= "ball hall";

char d2[] ="car toy";printf("%d\n ", strlen(d1));

printf("%d\n",strcmp(d1,d2));

printf("%s\n",d1); printf("%s\n",d1+5);

strcpy(d1+5,d2+4) ;printf("%s\n",d1) ;

strcat(d1, "s!!") ;printf("%s\n",d1) ; }

Senem KUMOVA METİN CS115 2008-2009 FALL

20

ARRAYS OF POINTERSint x=1; int y=2; int z=3;

int * p1= &x;int * p2= &y;int * p3= &z;printf(“%d %d %d \n”, *p1, *p2, *p3);

int x=1; int y=2; int z=3;

int * p[3];p[0] = &x; p[1]=&y; p[2]=&z;printf(“%d %d %d \n”, *p[0], *p[1], *p[2]);

Senem KUMOVA METİN CS115 2008-2009 FALL

21

ARRAYS OF POINTERS

x[0] --> C S 1 1 5 \0

x[1] --> P r o g r a m m i n g \0

x[2] --> i s \0

x[3] --> n o t \0

x[4] --> e a s y \0

x[5] --> : ) \0

char * x[6]

Senem KUMOVA METİN CS115 2008-2009 FALL

22

ARRAYS OF POINTERS#include<stdio.h>#include<string.h> // for strcpy#include<stdlib.h> // for malloc

main(){ int i; char word[100];

char *x[6];

for(i=0; i<6; i++){ scanf("%s", word);

x[i]=malloc((strlen(word)+1)*sizeof(char)); strcpy(x[i],word); }

for(i=0; i<6; i++)printf("%s\n",x[i]);

}

Senem KUMOVA METİN CS115 2008-2009 FALL

23

SORT for strings???HOMEWORK !!! pg 284 -290void swap(char **p, char **q){ char *tmp;

tmp = *p; *p = *q; *q = tmp;}

main(){ …..

char * w[5]; swap(&w[1], &w[4]);…….

}

Senem KUMOVA METİN CS115 2008-2009 FALL

24

Arguments to main() Two arguments, called argc and argv, can be used with

main() to communicate with the operating system

The variable argc >= 1 provides a count to the number of command line arguments, including command name itself

The array argv is an array of pointers, each pointer pointing to a string - a component of the command line

main( int argc, char *argv[])

Senem KUMOVA METİN CS115 2008-2009 FALL

25

Arguments to main()/* echoing the command line arguments in file my_echo.c */

#include <stdio.h>

 int main(int argc, char *argv[]) {    int i;    printf("argc = %d\n", argc);    for (i = 0; i < argc; ++i)        printf("argv[%d] = %s\n", i, argv[i]);    return 0; }

/* my_echo a simple example */

Senem KUMOVA METİN CS115 2008-2009 FALL

26

RAGGED ARRAY An array of pointers whose elements are used

to point to arrays of varying sizes

char *p[2] = {"abc", "a is for apple"} ;

Senem KUMOVA METİN CS115 2008-2009 FALL

27

Functions as Arguments to Functions In C, a function name by itself is treated by the

compiler as a pointer to the function, which is analogous to the idea that an array name by itself is treated as a pointer to the base of the array in memory

f            the pointer to a function *f           the function itself (after dereferencing) (*f)(x)    the call to the function (equivalently f(x))

Senem KUMOVA METİN CS115 2008-2009 FALL

28

#include <math.h>    #include <stdio.h>    double  f(double x); /* function prototypes */    double  sum_square(double f(double x), int m, int n);

    int main(void)    {     printf("%f\n%f\n", sum_square(f, 1, 10000), sum_square(sin, 2, 13));     return 0;    }

    double sum_square(double f(double x), int m, int n)      /* double sum_square(double (*f)(double), int m, int n)                  */     {       int     k;       double  sum = 0.0;       for (k = m; k <= n; ++k)            sum += f(k) * f(k);       /* equivalently can be written as sum += (*f)(k) * (*f)(k) */       return sum;     }

    double f(double x)   /* function f declaration */    {      return 1.0 / x;    }

Senem KUMOVA METİN CS115 2008-2009 FALL

29

The Type Qualifiers const and volatile they restrict, or qualify, the way an identifier of a given type can be used

const comes after the storage class (if any), but before the type, means that k can be initialized, but thereafter k cannot be assigned to or modified

static const int k = 3

Volatile object is one that can be modified in some unspecified way by

the hardware

extern const volatile int real_time_clock;

The extern means look for real_time_clock either in this file or in some other file,

the volatile qualifier indicates that the object may be acted on by the hardware; because it is const, it cannot be modified by the program, however the hardware can change the clock