(Numerical Arrays of Multiple Dimensions)
Traversing the Elements of a 2D Array / Traversing the Elements of a 2D Array /* 3 METHODS OF MANIPULATING OF ELEMENTS OF A MATRIX*/ /* PROGRAM #80-B */ /* 3 METHODS OF MANIPULATING OF ELEMENTS OF A MATRIX*/ #include<stdio.h> #define row 3 #define column 3 main( ){ int Matrix[row][column]={ {1, 3, 5}, {7, 9, 11}, {13, 15, 17} }; int i, j, *ptr; int *FirstElement=&Matrix[0][0]; int *LastElement=&Matrix[row-1][column-1]; }
/* METHOD 1 */ /* Use of two FOR loops, the outer loop for moving along rows, */ /* the inner loop for moving along columns */ for(i = 0; i < row; i++) for( j = 0; j < column; j++) printf(“%5d”, Matrix[i][j]); puts(“”);
/* METHOD 2 */ /* PTR is a pointer pointing to the1st element of the matrix. This pointer gets incremented by one through the For loop */ for(ptr = FirstElement; ptr <= LastElement; ptr++) printf(“%5d”, *ptr);
/* METHOD 3 */ /* Showing &MATRIX[i][0] and MATRIX[i] are the same ie, any matrix is a collection of row vectors */ /* For MATRIX[i][j], vectors can be accessed using MATRIX[i] */ printf(“\n%d, %d\n”, &Matrix[0][0], &Matrix[1][0]); printf(“\n%d, %d\n”, Matrix[0], Matrix[1]); for(i = 0; i < row; i++) for( j = 0; j < column; j++) printf(“%5d”, *(Matrix[i]+j) );
STRINGS STRINGS IN C FUNCTIONS DEALING WITH CHARACTERS FUNCTIONS DEALING WITH STRINGS
Strings There is no string type in C. Instead, to approximate strings, we will use arrays of characters. To transform a simple array of characters into a string, it must contain the '\0‘ character in the last cell. Note that '\0' is one character. It is called the null character or the end-of-string character.
STRINGS IN C String and array Similarities: Both represent a collection of a fixed number of elements of the same type Elements of both array & string are located consecutively in the memory Name of the array is a pointer that points to the first element of the array Name of the string is a pointer that points to the first element of the string
STRINGS IN C String and array Differences: Array represents a collection of numerical values, Data types include int, float, double String represents a collection of characters Data type include only characters
String & Character: Character: a byte of data (ASCII CODE) char Letter='B'; /* Letter is a variable of data type char */ String: a collection of characters char Str[ ]="BIRDS"; /* Str is a string */ NOTE: 'A': a single character "A": a string containing 2 characters, 'A' and '\0'
How to Declare a String? 1. Need to have a name for the string 2. Need to know the length of the string 3. Actual memory space needed is length of the string + 1 (to take care of the NULL character) 4. Data type is char Example: char Str[20];
To declare a string and initialize it we could do it the same way as seen before: char city[ ] = {'T', 'o', 'r', 'o', 'n', 't', 'o', '\0'}; Notice that the size of the array can be omitted here, since the computer can deduce it. However, declaring the size (in this case 8), is always good form. But there is a simpler way: char city[ ] = “Toronto”; If you specify the size here, do not forget the invisible '\0'! Size must be 8 not 7! The result is exactly the same. By using the simpler way, the '\0' is added automatically at the end. 'T' ' o' 'r' 'o' 'n' 't' 'o' '\0‘ 0 1 2 3 4 5 6 7
“U" and ‘U' Double quotes " " represent a string, A single quote ' ', one character. ‘U' is the single character U But “U" is an array size 2 containing the ‘U' character and the '\0' character. All string constants are represented in double quotes (remember "This is my first C program." ?).
How to Initialize a String? /*PROGRAM # 94*/ /*DEMONSTRATES INITIALIZING STRING*/ /* length of the string is 5, need 5+1 memory space */ /* char Str[6]=”BIRDS”; */ #include <stdio.h> main( ){ /* Declare and array with type char */ char str[ ] = "BIRDS"; printf("The string is %s.", str); } Note: The format specifier for a string variable is %s. char str [ ]="BIRDS": declare an array named str capable of storing characters initialize it to "BIRDS"
Inside the Memory char Str[6]=”BIRDS”; char Str[ ]=”BIRDS”; These 2 statements are equal.
Memory Map – Placement of a string within the memory Address memory Array element 2400 ‘B’ Str[0] 2401 ‘I’ Str[1] 2402 ‘R’ Str[2] 2403 ‘D’ Str[3] 2404 ‘S’ Str[4] 2405 ‘\0’ Str[5]
NOTES!!! Str and &str[0] both refer to the top of the string (ie, address 2400). ‘\0’ is NULL character. ‘\0’ lets ‘C’ knows where the string ends. When we calculate the length of the string, '\0' is NOT counted. The length of the string is 5 but we need 6 bytes for the string (one extra for ‘\0’). Therefore, when declaring the string we need (LengthOfString+1) bytes.
Inside the Memory char Str[100]="BIRDS"; is acceptable, it means that we set aside 100 bytes in the memory for Str. Right now we only initialize the first 6 bytes but we expect that later on there will be some usage for the rest of them. Furthermore, we do not have any idea about the content of the Str[6] to str[99].
Placement of a string within the memory Address memory Array element 2400 ‘B’ Str[0] 2401 ‘I’ Str[1] 2402 ‘R’ Str[2] 2403 ‘D’ Str[3] 2404 ‘S’ Str[4] 2405 ‘\0’ Str[5] 2406 ? Str[6] 2407 Str[7] 2408 Str[8] …
/*DEMONSTRATES DISPLAYING STRING*/ /* PROGRAM # 95 */ #include <stdio.h> #define ssize 6 main( ){ char str[ ] = "BIRDS"; int i; printf("Our string is %s. \n", str); /*Display characters string contains*/ puts(“Our string is made out of the following characters:”); for(i=0; i<ssize; i++) printf("%c\n ", str[i]); puts(“”); } AFTER EXECUTION: Our string is BIRDS. Our string is made out of the following characters: B I R D S