# Senem KUMOVA METİN CS115 2008-2009 FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.

## Presentation on theme: "Senem KUMOVA METİN CS115 2008-2009 FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont."— Presentation transcript:

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

Senem KUMOVA METİN CS115 2008-2009 FALL 3 One Dimensional Arrays EXAMPLE int grade[5]; data grade[1] data grade[2] 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 0column 1column 2….column C-1 row 0x[0][0]x[0][1]x[0][2]….x[0][C-1] row 1x[1][0]x[1][1]x[1][2]….x[1][C-1] row 2x[2][0]x[2][1]x[2][2]….x[2][C-1] row 3x[3][0]x[3][1]x[3][2]….x[3][C-1] …. row R-1x[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 - Memory x[0][0]data0. row, 0. column x[0][1]data0.row, 1. column x[0][2]data0.row, 2. column x[0][3]data0.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]data2.row, 1. column x[2][2]data2.row, 2. column x[2][3]data2.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}}; 123 456 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 Arrays COLUMNS 01 23 ROWS 0 0000 1 0123 2 0246 3 0369 4 04812 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 SORT SORT 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  134265 X=0 Y=0314265 Y=1341265 Y=2342165 Y=3342615 Y=4342651 X=1 Y=0432651 Y=1432651 Y=2436251 Y=3436521 Y=4436521 X=2 Y=0436521 Y=1463521 Y=2465321 Y=3465321 Y=4465321

Senem KUMOVA METİN CS115 2008-2009 FALL 11 ARRAY SORTING : BUBBLE SORT X=3 Y=0645321 Y=1654321 Y=2654321 Y=3654321 Y=4654321 X=4 Y=0654321 Y=1654321 Y=2654321 Y=3654321 Y=4654321 X=5 Y=0654321 Y=1654321 Y=2654321 Y=3654321 Y=4654321

Senem KUMOVA METİN CS115 2008-2009 FALL 12 ARRAY SORTING : BUBBLE SORT void 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 SORT  recursion

Senem KUMOVA METİN CS115 2008-2009 FALL 14 ARRAY SORT : SELECTION SORT #include 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  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 FUNCTIONS 3. 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 FUNCTIONS char d1[]= “ball hall”; char d2[] =“car toy”; strlen(d1)  9 strcmp(d1,d2)  negative printf(“%s”,d1+5)  hall strcpy(d1+5,d2+4)  hall toy ( is it true?) strcat(d1, “s!!”)  hall toys!! ( is it true?) ballhall\0 d1 d1+1... d1+9 ball hall\0 d1+5

Senem KUMOVA METİN CS115 2008-2009 FALL 19 STRING HANDLING FUNCTIONS #include 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 POINTERS int 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] -->CS115\0 x[1] -->Programming\0 x[2] -->is\0 x[3] -->not\0 x[4] -->easy\0 x[5] -->:)\0 char * x[6]

Senem KUMOVA METİN CS115 2008-2009 FALL 22 ARRAYS OF POINTERS #include #include // for strcpy #include // 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 -290 void 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 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 #include 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

Download ppt "Senem KUMOVA METİN CS115 2008-2009 FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont."

Similar presentations