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

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
EC-211 DATA STRUCTURES LECTURE 2. EXISTING DATA STRUCTURES IN C/C++ ARRAYS – A 1-D array is a finite, ordered set of homogeneous elements – E.g. int a[100]
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Introduction to Programming Lecture 39. Copy Constructor.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Senem Kumova Metin CHAPTER 7 Bitwise Operators and Enumeration Types.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences National Cheng.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Multidimensional Arrays. Example Write a program to keep track of all warmup scores for all students. Need a list of a list of scores Student – score.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CS Data Structures Chapter 2 Arrays and Structures.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Dynamic Memory Allocation. One Dimensional Dynamic Memory #define SIZE1 25 #define SIZE2 36 int *p; long double *q; p = (int *)malloc(SIZE1 * sizeof(int));
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
STRUCTURES. Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Senem KUMOVA METİN // Fall CS 115 Introduction to Programming Introduction to Computing.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Engineering Computing I Chapter 5 Pointers and Arrays.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Introduction to programming in java Lecture 23 Two dimensional (2D) Arrays – Part 3.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Two-Dimensional Arrays
Two Dimensional Array Mr. Jacobs.
CS SUMMER LECTURE 1 by İlker Korkmaz.
ECE Application Programming
14th September IIT Kanpur
Multidimensional Arrays
prepared by Senem Kumova Metin modified by İlker Korkmaz
MSIS 655 Advanced Business Applications Programming
Arrays Week 2.
CS2011 Introduction to Programming I Multidimensional Arrays
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Session #6 Memory Allocation and leaks Structures
Presentation transcript:

Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont

Senem KUMOVA METİN CS FALL 2 What is an Array?  int grade0, grade1, grade2;  int grade [3];  Arrays make it possible to represent large number of homogenous values grade0grade1grade2 grade[0]grade[1]grade[2]

Senem KUMOVA METİN CS 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 CS 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 CS 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 CS 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}}; Row 0 Row 1 Col 0 Col 1 Col 2 Number of rows Number of columns

Senem KUMOVA METİN CS 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 CS FALL 8 EXAMPLE: Two Dimensional Arrays COLUMNS ROWS REMEMBER !! COLUMN NUMBER WAS CHANGING FASTER !!

Senem KUMOVA METİN CS 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 CS 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  X=0 Y= Y= Y= Y= Y= X=1 Y= Y= Y= Y= Y= X=2 Y= Y= Y= Y= Y=

Senem KUMOVA METİN CS FALL 11 ARRAY SORTING : BUBBLE SORT X=3 Y= Y= Y= Y= Y= X=4 Y= Y= Y= Y= Y= X=5 Y= Y= Y= Y= Y=

Senem KUMOVA METİN CS 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 CS FALL 13 ARRAY SORT : MERGE SORT  recursion

Senem KUMOVA METİN CS 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 CS 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 CS 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 CS 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 CS 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 d d1+9 ball hall\0 d1+5

Senem KUMOVA METİN CS 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 CS 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 CS 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 CS 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 CS FALL 23 SORT for strings??? HOMEWORK !!! pg 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 CS 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 CS 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 CS 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 CS 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 CS 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 CS 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