Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Pointers and Arrays…Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Arrays and StringsDepartment of Computer ScienceGeorge Mason UniversityWhy do we need data structures?What examples of data structures can you think of?Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical data, represented by x1, x2, . . . , xn). In such situations it is often convenient to place the data items into an array The individual data items can be characters, integers, floating-point numbers, etc. However, they must all be of the same type and the same storage class. Each array element (i.e., each individual data item) is referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets..For a one-dimensional array, the size is specified by a positive integer expression, enclosed in square brackets. In general terms, a one-dimensional array declaration may be expressed as: DataType array[expression];External and static array declarations can include the assignment of initial values if desired. The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed in braces and separated by commas. DataType array[expression] = {value1, value2, ..., valuen}The array size need not be specified explicitly when initial values are included as a part of an array declaration. With a numerical array, the array size will automatically be set equal to the number of initial values included within the square brackets int digits[] = {3, 5, 9, 4, 8, 1}; float error[] = {0, 0.25, 0.15, 0.2};Pointers and Arrays… An array variable is actually just a pointer to the first element in the array You can access array elements using array notation or pointers More on pointers next week… The only difference between an array name and a pointer: A pointer is a variable, so pa = a and pa++ is legal An array name is not a variable, so a = pa and a++ is illegal Why is this the cause? Because arrays are assigned a fixed place in memory therefore it does not make sense to change it. Array addresses are reserved for elementso fthat arraySingle operations which involve entire arrays are not permitted in C. if a and b are similar arrays, assignment operations, comparison operations, etc. must be carried out on an element-by-element basis.An entire array can be passed to a function as an argument. To pass an array to a function, the array name must appear by itself, without brackets or subscripts, as an argument within the function call.ExercisesCreate a function that finds the second smallest element in an arrayCreate a function that sorts the elements of an array in ascending orderStringsThere is no string type in C! I Instead, strings are implemented as arrays of characters: char* or char [] Enclosed in double-quotes Terminated by NULL character (’\0’)Are these two array declarations the same?char color[4] = “BLUE”;char color[] = “BLUE”;int strlen(char *s){int n = 0;while (*s != ’\0’) {s++;n++;}return n;}char *p = "hello, world"; strlen(p); strlen(p + 7);char * strcpy(char *destination, char *source){ char *start = destination; while(*source != '\0') { *destination = *source; destination++; source++; }What is a potential issue that you see with strcpy?Note this is not the official source code for strcpy, rather an alternative/* Append SRC on the end of DEST. */char *STRCAT (char *dest, const char *src){ strcpy (dest + strlen (dest), src); return dest;}Whats the issue with strcat?/* Compare S1 and S2, returning less than, equal to or greater than zero if S1 is lexicographically less than, equal to or greater than S2. */ int STRCMP (const char *p1, const char *p2) { const unsigned char *s1 = (const unsigned char *) p1; const unsigned char *s2 = (const unsigned char *) p2; unsigned char c1, c2; do { c1 = (unsigned char) *s1++; c2 = (unsigned char) *s2++; if (c1 == '\0') return c1 - c2; } while (c1 == c2); return c1 - c2; }Alternatives to strcpyn strcat. And srtcmpstrncpy and strncatchar *strncpy(char *dest, const char *src, size_t n)char *strncat(char *dest, const char *src, size_t n)int strncmp(const char *str1, const char *str2, size_t n)ExercisesCreate a function that converts an uppercase string to its lowercase equivalentCreate a function that determines if a String is a palindromeRecall that a string is a palindrome if it reads the same forwards as


View Full Document

MASON CS 262 - Arrays and Strings

Download Arrays and Strings
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and Strings and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays and Strings 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?