Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved."— Presentation transcript:

1 Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

2 2 A array is a collection of variables of the same data type that are referred to by a common name. A array is a collection of variables of the same data type that are referred to by a common name. Arrays may have from one to several dimensions, but the one- dimensional array is the most common. Arrays may have from one to several dimensions, but the one- dimensional array is the most common. Arrays are convenient for grouping related variables together. Arrays are convenient for grouping related variables together. A character array is very commonly used because it can hold strings. A character array is very commonly used because it can hold strings. Remember that C/C++ does not define a built-in string data type (C++ does implement a string class), but implements them as null terminated arrays of characters. Remember that C/C++ does not define a built-in string data type (C++ does implement a string class), but implements them as null terminated arrays of characters. What are Arrays?

3 3 Review of Characters in Memory You can represent a single character in code by 'B'. Note the use of single quotes around the character. You can represent a string in code by "Hello World". Note the use of double quotes around the character(s). In memory each character is stored in a single byte. In memory each character of a string is stored in a single byte and it is terminated with a NULL character. Example: \0dlroW olleH B Memory Character String NULL Character

4 4 One-Dimensional Arrays A one-dimensional array is a list of related variables. Its general format is: The type declares the base type for the elements of the array and size defines the number of elements in the array. For example, the following code defines an array containing 10 integers. To index value must be used to access an individual element of an array. An index describes the position (relative to zero) of an element in an array. In C/C++ ALL arrays are zero based! type var-name[size]; int sample[10];

5 5 One-Dimensional Arrays (continued) Here is a sample program that defines, loads, and then displays the contents of an array. #include using namespace std; int main() { int sample[10]; // this reserves 10 integer elements int t; for( t = 0; t < 10; ++t ) // load the array sample[t] = t; for( t = 0; t < 10; ++t ) // display the array cout << sample[t] << ' '; return 0; } All arrays occupy contiguous memory locations.

6 6 One-Dimensional Arrays (continued) This example creates an array of 8 integers. Each element is identified by its index number. The valid indexes for this array are 0 – 7. To reference/access the 4 th element of this array you must use the following: What does this do? 110056714881238453 Array x with 8 elements. Index 0 Index 7 int x[8]; // defines an array of 8 integers cout << x[3] << endl; // Why the 3? x[5] = 3467; x[0] x[7]x[6]x[5]x[4]x[3]x[2]x[1] total bytes = number of bytes in type x number of elements

7 7 One-Dimensional Arrays (continued) To process the data stored in an array, you reference each individual element by specifying the array’s name and the index number of the element. This example declares an integer array and initializes it: In memory the array would look like this. Array X with 2 elements. Index 0 13512 int X[2]; // defines an array of two integers X[0] = 12; // sets first element to 12 X[1] = 135; // sets second element to 135

8 8 One-Dimensional Arrays (continued) Here is a program that defines an array of 10 elements, assigns each element to a random value, and displays the minimum and maximum values. #include using namespace std; int main() { int i, min_value, max_value; int list[10]; // define an array of 10 integers for(i = 0; i < 10; i++) list[i] = rand(); min_value = list[0]; // find minimum value for(i = 1; i < 10; i++) if( min_value > list[i] ) min_value = list[i]; cout << "minimum value: " << min_value << endl'; max_value = list[0]; // find maximum value for(i = 1; i < 10; i++) if( max_value < list[i] ) max_value = list[i]; cout << "maximum value: " << max_value << endl; return 0; }

9 9 Lab 1 Code: #include using namespace std; void main() { int Ary[4]; for (int x = 0; x < 4; x++) { cout << "Enter a whole number: "; cin >> Ary[x]; } for (int y = 0; y < 4; y++) cout << "Element " << y << " contains " << Ary[y] << endl; }

10 10 Arrays You CANNOT assign one array to another array. The following is illegal. You must manually copy each element of an array to the other array as follows: for( int i = 0; i < 10; i++) a[i] = b[i]; int a[10]; int b[10]; a = b; // error -- illegal

11 11 No Bounds Checking C/C++ performs NO bounds checking on arrays. Nothing prevents you from overrunning the end of an array. It is your responsibility to make sure this does not happen. int y[8]; for( int i = 0; i < 10; i++) a[i] = i; 0123456789 This memory has been overlaid. NOT GOOD Memory allocated for the array All bets are now off concerning the well being of the program’s execution.

12 12 Sorting Arrays Sorting the data contained in arrays is a common operation. There are several type of sorts you can use: Quicksort Quicksort Shaker sort Shaker sort Shell sort Shell sort Bubble sort Bubble sort Since sorting arrays is so common, a standard library function is provided which implements the Quicksort. It is one of the fastest general purpose sorting routines available. The text covers the Bubble sort, but we will learn to use the qsort() library function. This function is designed to sort an array of any type data in either ascending or descending sequence.

13 13 The qsort() Function Format: The qsort() function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array (usually the first element of the array) to be sorted. compare is the name of a function, which MUST be provided by you, that controls the sequence of the elements (ascending or descending). It is called automatically by the qsort() function and NOT by you. qsort() overwrites this array with the sorted elements. void qsort( void *base, // Start of target array size_t num, // number of elements to sort size_t width, // size in bytes of each element // compare function used by the qsort() (int (*)(const void *, const void *)) compare )

14 14 The qsort() Function (continued) The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort() calls the compare routine one or more times during the sort, passing pointers to two array elements on each call: The routine must compare the elements, then return one of the following values: <0 if elem1 is less than elem2 <0 if elem1 is less than elem2 0 if elem1 is equal to elem2 0 if elem1 is equal to elem2 >0 if elem1 is greater than elem2 >0 if elem1 is greater than elem2 The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function. compare( (void *) elem1, (void *) elem2 );

15 15 Example of Using the qsort() Function #include using namespace std; int Numeric(int *p1, int *p2) { if (*p1 < *p2 ) return -1; if (*p1 == *p2 ) return 0; return 1; } int main() { int A[5] = {345, 90000, 1, 56, 4000}; int i; for(i = 0; i < 5; i++) // Display array before sorting cout << setw(8) << A[i]; cout << endl; qsort(&A, 5, sizeof(A[0]),(int(*) (const void *, const void *)) Numeric); for(i = 0; i < 5; i++) // Display sorted array cout << setw(8) << A[i]; cout << endl; return 0; }

16 16 The qsort() Function (continued) OUTPUT: 345 90000 1 56 4000 1 56 345 4000 90000 The sizeof() function returns the number of bytes of memory allocated for the variable. int x; int y[10]; int size; char lastName[17]; cout << sizeof( x ); // displays 4 cout << sizeof( y ); // displays 40 size = sizeof( y[2] ); // sets size to 4 cout << sizeof( lastName ); // displays 17 cout << sizeof( int ); // displays 4 cout << sizeof( double ); // displays 8

17 17 Strings C/C++ does not have a string data type. In C/C++, a string is defined as a character array terminated by a null (‘\0’). Because of this null terminator, you will need to take this into consideration when calculating the number of characters it will take to store the string. Examples: The last name field can contain a maximum of 15 characters. You can NOT do a direct assignment of strings like: You can NOT do a direct assignment of strings like: You can initialize a string variable at the declaration: SamSmith\0?????? Always add 1 to the total number of characters to account for the NULL terminator. char Lname[16]; Lname = "Sam Smith"; // this is illegal char Lname[16] = "Sam Smith";

18 18 Strings (continued) What does this create? This creates an empty string or null string. Since strings are implemented as a one-dimensional array of char’s, we can access any individual character in the string by using an index value. \o??????? char str[] = “ABCDEFGHI”; cout << str[3];// displays D char Str[8] = “”;

19 19 Reading a String from the Keyboard The easiest way is to define a string variable and use the cin statement to input data from the keyboard into it. #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; cin >> str; // read string from keyboard cout << "Here is your string: "; cout << str; return 0; } Sample run: Enter a string: This is a test Here is your string: This

20 20 Reading a String from the Keyboard Do you see the problem? Only the word This is inputted by the cin statement. The reason for this is that the cin statement STOPS inputting characters from the keyboard buffer when a whitespace character is encountered. Whitespace characters are: spaces spaces tabs tabs newlines newlines One solution to this problem is to use the gets() library function. Its format is: gets(array-name);

21 21 Reading a String from the Keyboard Here is the same program from above, but it uses the gets() function instead of the cin statement. #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); // read a string from the keyboard cout << "Here is your string: "; cout << str; return 0; } Sample run: Enter a string: This is a test Here is your string: This is a test

22 22 Reading a String from the Keyboard For reasons we will cover later, the name of a character array that holds a string can be used any place that a string literal can be used. Remember: Neither the cin or the gets() performs any boundary checking on the array.

23 23 String Library Functions The cstring library holds the functions for string manipulation. Some of the more common string library functions are: strcpy( dest, source ); strncpy( dest, source, number ); strcat( dest, source ); strncat( dest, source, number ); strcmp( str1, str2 ); strncmp( str1, str2, number ); strlen( source );

24 24 The strcpy() Function Format: This function makes a copy of source string and places it into the dest string. strcpy(dest, source); #include using namespace std; int main() { char str[80]; strcpy(str, "hello"); cout << str; return 0; } OUTPUT: hello

25 25 The strncpy() Function Format: This function makes a copy of a specific number of characters in source string and places it into the dest string. strncpy(dest, source, number); #include using namespace std; int main() { char s1[20]; char s2[] = "ABCDEFGHIJKLM"; strncpy(s1, s2, 6); cout << s1; return 0; } OUTPUT: ABCDEF

26 26 The strcat() Function Format: This function appends the source string to the end of the dest string. This is called concatenation. strcat(dest, source); #include using namespace std; int main() { char s1[20], s2[10]; strcpy(s1, "hello"); strcpy(s2, " there"); strcat(s1, s2); cout << s1; return 0; } OUTPUT: hello there

27 27 The strncat() Function Format: This function appends a specific number of characters in source string to the end of the dest string. strncat( dest, source, number ); #include using namespace std; int main() { char s1[20] = "ABCDEF" char s2[10] = "WXYZ"; strncat( s1, s2, 2 ); cout << s1; return 0; } OUTPUT: ABCDEFWX

28 28 The strcmp() Function Format: This function compares two strings. It returns the following: a negative value if str1 < str2 a negative value if str1 < str2 a zero if the strings are equal a zero if the strings are equal a positive value if str1 > str2. a positive value if str1 > str2. strcmp( str1, str2 );

29 29 #include using namespace std; bool password(); int main() { if( password() ) cout << "Logged on.\n"; else cout << "Access denied.\n"; return 0; } bool password() { // Return true if password accepted; false otherwise. char s[80]; cout << "Enter password: "; gets(s); if( strcmp( s, "password" ) ) { // strings differ cout << "Invalid password.\n"; return false; } return true; }

30 30 The strncmp() Function Format: This function compares a specified number of characters in str1 to the same number of characters in str2. It returns the same values as strcmp(). strncmp( str1, str2, number ); #include using namespace std; int main() { char str1[] = "ABCJKL"; if( strncmp( str, "ABC", 3 ) ) cout << "Found a match.\n"; else cout << "Match not found.\n"; return 0; } OUTPUT: Found a match

31 31 The strlen() Function Format: This function returns the number of characters a string currently contains excluding the NULL character. strlen(source); #include using namespace std; int main() { char str[80]; cout << "Enter a string: "; gets(str); cout << "Length is: " << strlen(str) << endl;; return 0; } SAMPLE EXECUTION: Enter a string: Hello world Length is: 11

32 32 The strlen() Function (continued) The strlen() function is a very useful function for processing the individual characters of a string variable. Here is an example: #include using namespace std; int main() { // Print a string backwards. char str[80]; int i; cout << "Enter a string: "; gets(str); for( i = strlen(str) - 1; i >= 0; i-- ) cout << str[i]; return 0; }

33 33 Another strlen() Function Example #include using namespace std; int main() { // Scan a string to see if it contains only digits. char str[80]; int i; bool good = true; cout << "Enter a string: "; gets(str); for( i = 0; i < strlen(str); i++ ) if( str[i] ‘9’ ) { good = false; break; } if( good ) cout << “String contains only numbers” << endl; else cout << “Illegal character found: ” << str[i] << endl; return 0; }

34 34 Another strlen() Function Example #include using namespace std; int main() { // Test for an overflow condition. char str[80]; cout << "Enter a string: "; gets(str); if( strlen( str ) > 79 ) { cout << “Overflow condition has occurred” << endl; // this statement terminates the program exit( EXIT_FAILURE ); } return 0; }

35 35 Sample Code #include using namespace std; char Source1[6] = "Hello"; char Source2[12] = "Hello Tommy"; char Dest[15] = "\0"; int main() { cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strcpy to copy Source1 to Dest\n"; strcpy( Dest, Source1); cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strncpy to copy Source2 to Dest\n"; strncpy( Dest, Source2, 9); cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strcat to append ' again' to Dest\n"; strcat( Dest, " again "); cout << "The contents of Dest is " << Dest << ".\n\n";

36 36 cout << "Use strncat to append Source2 to Dest\n"; strncat( Dest, Source2, 9); cout << "The contents of Dest is " << Dest << ".\n\n"; cout << "Use strcmp to compare Source1 to Source2\n"; if (strcmp( Source1, Source2) == 0) cout << "The two strings are equal.\n\n"; else if (strcmp( Source1, Source2) < 0) cout << "Source1 should pre ceed Source2.\n\n"; else if (strcmp( Source1, Source2) > 0) cout << "Source2 should preceed Source1.\n\n"; cout << "Use strncmp to compare Source1 to Source2\n"; if (strncmp( Source1, Source2, 5) == 0) cout << "The two strings are equal.\n\n"; else if (strncmp( Source1, Source2, 5) < 0) cout << "Source1 should preceed Source2.\n\n"; else if (strncmp( Source1, Source2, 5) > 0) cout << "Source2 should preceed Source1.\n\n"; }

37 37 OUTPUT: The contents of Dest is. Use strcpy to copy Source1 to Dest The contents of Dest is Hello. Use strncpy to copy Source2 to Dest The contents of Dest is Hello Tom. Use strcat to append ' again' to Dest The contents of Dest is Hello Tom again. Use strncat to append Source2 to Dest The contents of Dest is Hello Tom again Hello Tom. Use strcmp to compare Source1 to Source2 Source1 should preceed Source2. Use strncmp to compare Source1 to Source2 The two strings are equal. Press any key to continue

38 38 Arrays Of Strings You can create a two dimensional array of strings. #include using namespace std; int main() { // The first number is how many rows the array has and the // second number is how many characters each record contains. char Days[7][10] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday“ }; for( int loop = 0; loop < 7; loop++ ) cout << Days[loop] << "\n"; return 0; }

39 39 Arrays Of Strings (continued) OUTPUT: Monday Tuesday Wednesday Thursday Friday Saturday Sunday Press any key to continue

40 40 Arrays Of Strings (continued) MONDAY\0 TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY [0][0] [0][1][0][2][0][3][0][4][0][5][0][6][0][7][0][8][0][9] [1][0] [2][0] [3][0] [4][0] [5][0] [6][0] char Days[7][10] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday“ }; char Days[7][] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday“ }; These are equivalent:

41 41 Arrays You can initialize an array when it is declared. Example: You can also create an array and initialize it without giving the number of elements it will be. Example: int X[4] = {2, 5, 23, 177}; int X[] = {2, 5, 23, 177};

42 42 Sample Array #include using namespace std; void main() { char monthNames[12][] = { “January”, “February“, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; int month; cout << “Enter month number ( 1 – 12 ): “; cin >> month; cout << “The month is: “ << monthNames[month] << endl; return 0; } SAMPLE EXECUTION: Enter month number ( 1 – 12 ): 10 The month is: November What is the problem?

43 43 Corrected Sample Array #include using namespace std; void main() { char monthNames[12][] = { “January”, “February“, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” }; int month; cout << “Enter month number ( 1 – 12 ): “; cin >> month; cout << “The month is: “ << monthNames[month - 1] << endl; return 0; } SAMPLE EXECUTION: Enter month number ( 1 – 12 ): 10 The month is: October


Download ppt "Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved."

Similar presentations


Ads by Google