Multidimensional Array
• In case we want to store data as a tabular form, like a table with rows
and columns, we need to get familiar with multidimensional arrays.
• A multidimensional array is basically an array of arrays.
Two-Dimensional Arrays
• A 2D array is also known as a matrix (a table of rows and columns). •
To create a 2D array of integers, take a look at the following example: •
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
• The first dimension represents the number of rows [2], while the
second dimension represents the number of columns [3]. The values
are placed in row-order, and can be visualized like this:
Access the Elements of a 2D Array
• To access an element of a two-dimensional array, you must specify the index
number of both the row and column.
• This statement accesses the value of the element in the first row (0) and third
column (2) of the matrix array.
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
printf("%d", matrix[0][2]);
return 0;
}
Change Elements in a 2D Array
• To change the value of an element, refer to the index number of the
element in each of the dimensions:
• The following example will change the value of the element in the
first row (0) and first column (0):
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printf("%d", matrix[0][0]); // Now outputs 9 instead of 1
return 0;
}
Loop Through a 2D Array
• To loop through a multi-dimensional array, you need one loop for
each of the array's dimensions.
• The following example outputs all elements in the matrix array:
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8}
}; int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}
return 0;
}
C Strings
• Strings are used for storing text/characters.
• For example, "Hello World" is a string of characters.
• Unlike many other programming languages, C does not have a
String type to easily create string variables. Instead, you must
use the char type and create an array of characters to make a
string in C:
• char greetings[] = "Hello World!";
• Note that you have to use double quotes ("").
• To output the string, you can use the printf() function together with
the format specifier %s to tell C that we are now working with strings:
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
Access Strings
• Since strings are actually arrays in C, you can access a string by referring to
its index number inside square brackets [].
• This example prints the first character (0) in greetings:
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%c", greetings[0]);
return 0;
}
Modify Strings
• To change the value of a specific character in a string,
refer to the index number, and use single quotes:
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
greetings[0] = 'J';
printf("%s", greetings);
return 0;
}
Loop Through a String
• We can also loop through the characters of a string, using a for loop:
#include <stdio.h>
int main() {
char carName[] = "Volvo";
int i;
for (i = 0; i < 5; ++i) {
printf("%c\n", carName[i]);
}
return 0;
}
#include <stdio.h>
int main() {
char carName[] = "Volvo";
int length = sizeof(carName) /
sizeof(carName[0]); int i;
for (i = 0; i < length; ++i) {
printf("%c\n", carName[i]);
}
return 0;
}
#include <stdio.h>
int main() {
char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!',
'\0'}; char greetings2[] = "Hello World!";
printf("%s\n", greetings);
printf("%s\n", greetings2);
return 0;
}
Why do we include the \0 character at
the end?
• This is known as the "null terminating character", and must be
included when creating strings using this method. It tells C that this is
the end of the string.
#include <stdio.h>
int main()
{
char str1[] = "To be or not to be";
char str2[] = ",that is the question";
unsigned int count = 0; // Stores the string length while
(str1[count] != '\0') // Increment count till we reach the ++count; //
terminating character.
printf("The length of the string \"%s\" is %d characters.\n", str1, count);
count = 0; // Reset count for next string while (str2[count] != '\0')
// Count characters in second string ++count;
printf("The length of the string \"%s\" is %d characters.\n", str2,
count); return 0;
}