Arrays and Strings (PDF)
Document Details
Uploaded by NourishingAlgebra
Tags
Summary
This document explains arrays and strings in C++. It covers concepts such as declaring, initializing, and using arrays, including one-dimensional and two-dimensional arrays. It also introduces string handling, such as string functions, and declaring and initializing strings in C++.
Full Transcript
# 5 Arrays and Strings ## 5.1.1 Concept of an Array An array is a collection of the same type of elements stored in contiguous memory locations. For example, if we want to store marks of six subjects in computer memory, we have to declare six variables, one for each subject. Instead of using six v...
# 5 Arrays and Strings ## 5.1.1 Concept of an Array An array is a collection of the same type of elements stored in contiguous memory locations. For example, if we want to store marks of six subjects in computer memory, we have to declare six variables, one for each subject. Instead of using six variables we can declare one array variable called marks that can store marks of six subjects. This array could be represented as shown in Figure 5-1. | Array marks | marks[0] | |---|---| | 45 | marks[1] | | 67 | marks[2] | | 50 | marks[3] | | 79 | marks[4] | | 58 | marks[5] | | 36 | | **Figure 5-1: An array having six elements** Array consists of contiguous memory locations and each cell represents an element of the array. In the array named marks, each element of the array represents marks of a subject. The number within the square brackets is called index and it is used to access a specific element of the array. The first element of the array always has the index 0. Therefore, the index of the last element is one less than the size of the array. In the marks array, there are 6 elements so the indexes are from 0 to 5. Index of array is always an integer value. ## 5.1.2 Declaring an Array To declare an array in C++, the type of the elements, the name of array and the number of elements it is required to store need to be mentioned in the declaration statement. The following is the general form of declaration of array. ``` datatype arrayname [arraysize]; ``` Here, datatype is a valid data type (such as integer, float, etc.), "arrayname" is the name of the array which is a valid variable name and "arraysize" is enclosed within square brackets and it specifies the number of elements that can be stored in the array. This type of array is known as a one dimensional array. For example, the following statement declares an array called "marks" of type integer that can store marks of six subjects: ```c++ int marks[6]; ``` ## 5.1.3 Initialization of Array An array can be initialized in the declaration statement. The following statement declares the marks array as integer of size 6 and assigns marks to each element of the array. ```c++ int marks[6] = (45, 67, 50, 79, 58, 36); ``` When initializing an array, it is not necessary to mention the array size in the declaration statement since the compiler can find out the array size by counting the values in the curly brackets. Therefore, the above statement can also be written as: ```c++ int marks[] = (45, 67, 50, 79, 58, 36); ``` This statement is equal to the following six statements. ```c++ marks[0] = 45; marks[1] = 67; marks[2] = 50; marks[3] = 79; marks[4] = 58; marks[5] = 36; ``` ## 5.1.4 Using Arrays in Programs ## 5.1.5 The sizeof() Function The sizeof() function provides the number of bytes occupied to store values for data type named within the parenthesis. It is used to determine the amount of storage reserved for int, float, double, char, etc. data types. # 5.2 Two Dimensional Arrays ## 5.2.1 Introduction to Two Dimensional Arrays A two dimensional array uses a single variable name to represent a collection of the same type of data that is in the form of a table or matrix. It has two dimensions i.e. vertical and horizontal dimensions. Vertical dimension represents rows and horizontal dimension represents columns. Two dimensional arrays provide an easy way of handling data that is stored in the form of a table. A two dimensional array "a", that has three rows and five columns is shown below. | | Column 0 | Column 1 | Column 2 | Column 3 | Column 4 | |---|---|---|---|---|---| | Row 0 | a[0][0] | a[0][1] | a[0][2] | a[0][3] | a[0][4] | | Row 1 | a[1][0] | a[1][1] | a[1][2] | a[1][3] | a[1][4] | | Row 2 | a[2][0] | a[2][1] | a[2][2] | a[2][3] | a[2][4] | **Figure 5-2: A two dimensional array** Each cell in the table represents an element of the array in the form of `a[row][col]`, where "row" and "col" written within square brackets are indices. The first index, "row", represents the row number and second index, "col", represents the column number that uniquely identify each element of "a". The indices "row" and "col" are always integer values and start from zero. For example "a[1][3]" will identify the element in the second row and fourth column. ## 5.2.2 Defining and Initializing A Two Dimensional Array To define a two dimensional array in C++, the type of the elements, the name of the array and the number of elements it is required to store in rows and columns is mentioned in the declaration statement. The following is the general form of declaration of two dimensional array. ```c++ datatype arrayname[rowsize][columnsize]; ``` Here, datatype is a valid data type (such as integer, float, etc.), "arrayname" is the name of array and "rowsize" and columnsize" enclosed within square brackets specify the number of rows and columns in the two dimensional array. For example, the following statement declares an array called "a", that can store marks of 3 students of 5 tests: ```c++ int a[3][5]; ``` The `rowsize`, which is 3, represents the number of students and `columnsize`, which is 5, represents the number of tests. In C++, indices always start from zero. Therefore, the student number will be in the range of 0 to 2 and test number will be in the range of 0 to 4. The following statement declares a two dimensional array that stores floating-point values. ```c++ float height[8][10]; ``` ## 5.2.3 Accessing and Filling A Two Dimensional Array Data can be written in any element of a two dimensional array as if it was a normal variable by specifying the index of row and column. For example the following assignment statement will store the value 15 in the element at row 2 and column 4 of two dimensional array named "k". ```c++ k[1][3] = 15; ``` Two dimensional arrays are generally accessed row by row using nested loop. Therefore, the row index is used as outer loop variable and column index as inner loop variable. **Note**: It may also be accessed column by column. # 5.3 Strings ## 5.3.1 Introduction String is a sequence of characters. In C++, character string is stored in a one dimensional array of char data type. Each element of a character string holds one character. All the strings end with a special character, known as a null character and it is represented by `'\0'`. The null character is automatically appended at the end of string. String is most commonly used in computer programming to represent a name, address, object, book title, etc. ## 5.3.2 A String To define a string in C++, the data type char, the name of string and the number of characters it is required to store is mentioned in the declaration statement. The following is the general form of declaration of string. ```c++ char stringname [stringsize]; ``` The char keyword lets the compiler know that variable of type character is declared. The "stringname" is the name of the string variable. It follows the same name rules of variables. The stringsize enclosed within square brackets specifies the number of other type of characters that can be stored in the string. **Note:** Since the null character is appended at the end of string, if a string has n characters then the string size should be at least n+1. For example, the following statement declares a string called "weekday" that can hold maximum 10 characters including one for the null character. ```c++ char weekday[10]; ``` As another example the following statements declaes a string called "studentname" that can hold 20 characters including one for the null character. ```c++ char studentname[20]; ``` ## 5.3.3 Initializing Strings Just like arrays of integer and floating-point numbers, strings arrays can be initialized in the declaration statement. For example, the following statement declares and initializes the string variable "weekday" to "Sunday". ```c++ char weekday[10] = {'S', 'u', 'n', 'd', 'a', 'y'}; ``` The next statement provides another easy way for the same declaration and initialization. ```c++ char weekday[10] = "Sunday"; ``` # 5.4 Commonly Used String Functions To use strings in computer programs, it is essential to learn how string functions are used. The header file `<string.h>` is used when string functions are used in the program. C++ supports a large number of string handling functions in the standard library `<string.h>`. The most commonly used string functions are described as follows. ## cin.get() Function This function is used to read a string from the keyboard that may contain blank spaces. The general form of cin.get() function is: ```c++ cin.get(strvar, strsize); ``` It has two arguments. The first argument, "strvar", is the name of the string variable and the second argument, "strsize", is the maximum size of the string or character array.