Arrays and Structures
Chapter 12 · Tamil Nadu State Board
- 11 min read
- 6-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson teaches how arrays group multiple values of the same type under one name, and how their elements are accessed using subscripts. It explains one-dimensional and two-dimensional arrays, strings, and ways to initialize and use them. It also introduces structures, which group data items of different types, and shows how to access and assign their members. These ideas help organize related data and process it in C++ programs.
Written by AI from this lesson. How we check it
Key points
- An array stores multiple values of one type under a shared name.
From your textbook
“An array is a collection of variables of the same type that are referenced by a common name”
- Array indexing begins at 0, so subscript 3 identifies the fourth element.
From your textbook
“The subscript always starts with 0”
- A string is represented in C++ as a one-dimensional character array that ends with a null character.
From your textbook
“it implements a string as an one-dimensional character array.”
- A two-dimensional array arranges similar elements in rows and columns and uses two indices to reach an element.
From your textbook
“the first index specifies the row value and second index specifies the column value.”
- A structure groups data items of different types into one logical unit.
From your textbook
“This allows to group variables of mixed data types together into a single unit.”
- Structure members are accessed by writing the object name, a dot, and the member name.
From your textbook
“The syntax for that is using a dot (.) between the object name and the member name.”
Formulas in this chapter
- Array declaration<data type><array_name> [<array_size>];Explained here
- Array storageNumber of bytes allocated for type of array × Number of elementsExplained here
- Character array declarationchar array_name[size];Explained here
- Two-dimensional array declarationdata-type array_name[row-size][col-size];Explained here
Arrays and Their Types
A single variable stores one value. An array stores several values of the same type under one common name. Its elements are kept in a fixed number of places, one after another in memory.
Definition
- Array
- An array is a collection of variables of the same type that are referenced by a common name.
Types of arrays
- One-dimensional arrays
- Two-dimensional arrays
- Multi-dimensional arrays
A one-dimensional array stores values in a single row or column. The size of an array is called its dimension.
Declaring and Accessing a One-Dimensional Array
<data type><array_name> [<array_size>];
data_type is the type of each element, array_name is the common name, and array_size is the number of elements.
For example, int num[10]; declares an integer array called num with 10 elements. Each element has an index called a subscript. Subscripts start at 0, so num[3] refers to the 4th element.
Examples of array declarations
- char emp_name[25]; declares a character array with size 25.
- float salary[20]; declares a floating-point array with size 20.
- int a[5], b[10], c[15]; declares three integer arrays.
An array element is accessed by writing the array name followed by its subscript in square brackets. The subscript can be a variable, a constant, or an expression that evaluates to an integer.
Worked example
For int num[5] = {10, 20, 30, 40, 50}; and int t=2;, what do the following statements display: cout<<num[2]; cout<<num[3+1]; cout<<num[t=t+1];
Try it first, then show the solutionSolution
- num[2] refers to the 3rd element, which is 30.
- 3+1 equals 4, so num[3+1] refers to the 5th element, which is 50.
- t=t+1 changes t to 3, so num[t=t+1] refers to the 4th element, which is 40.
Answer: The statements display 30, 50, and 40.
Array Memory and Initialization
The memory used by an array depends on its type and its number of elements. The formula for the total storage is the number of bytes allocated for the array type multiplied by the number of elements.
Number of bytes allocated for type of array × Number of elements
Multiply the bytes used by one element by the total number of elements.
Bytes allocated for each data type
| Datatype | Turbo C++ | Dev C++ |
|---|---|---|
| char | 1 | 1 |
| int | 2 | 4 |
| float | 4 | 4 |
| long | 4 | 4 |
| double | 8 | 8 |
| long double | 10 | 10 |
An array can receive values when it is declared. If it is not initialized, its elements contain garbage values. Values are written inside curly braces. When values are provided during declaration, the size can be left out and is determined by the number of values.
Worked example
What values are stored in int age[5]={19,21,16,1,50};?
Try it first, then show the solutionSolution
- The first value is stored in age[0].
- The next values are stored in order in age[1], age[2], age[3], and age[4].
Answer: age[0]=19, age[1]=21, age[2]=16, age[3]=1, and age[4]=50.
More initialization examples
- float x[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
- char vowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
Using Loops, Counting, and Searching in Arrays
A for loop can visit array elements one at a time. The loop index often starts at 0 because the first array subscript is 0. During runtime, cin can read a value into an element such as num[i].
To count even and odd values, check each number using num[i] % 2 == 0. If the condition is true, increase the even count; otherwise, increase the odd count. In the given sample, the 10 entered numbers produce 6 even numbers and 4 odd numbers.
Definition
- Linear search
- Linear search checks each array element against the value being searched for until the elements have been traversed and compared.
A search program reads the array and the value to find. It compares the value with array elements and records the index when it finds a match. If the value is not found, the program reports that it is not in the array.
Remember: Array subscripts start at 0, so the element at subscript 3 is the 4th element.
Character Arrays and Strings in C++
Definition
- String
- A string is a sequence of characters, and each character can be a letter, number or symbol.
char array_name[size];
array_name is the array's name, and size is its number of memory locations.
Rules for character arrays
- The size must be an unsigned integer value.
- The array's length is fixed after creation and cannot be changed during run time.
- A character array must have room for the null character at the end of the string.
- If an array's size is not written during initialization, the compiler calculates it from the elements.
Worked example
What does char country[6]; allow the array to store?
Try it first, then show the solutionSolution
- The array reserves 6 bytes.
- One location is needed for the null character.
Answer: The string can contain up to 5 characters.
A character array can be initialized with a string or with individual characters. If some elements are left uninitialized in an initialized array, the remaining elements are filled with NULL. You cannot initialize an array with more elements than its size.
Ways to initialize an array with INDIA
- char country[6]="INDIA";
- char country[6]={'I', 'N', 'D', 'I', 'A', '\0'};
- char country[]="INDIA";
- char country[]={'I', 'N', 'D', 'I', 'A', '\0'};
Remember: Count the null character when choosing the size of a character array.
Checking Whether a String Is a Palindrome
The palindrome program compares characters from opposite ends of the string. It keeps a flag set to 1 unless it finds a pair that does not match. The output says whether the string is a palindrome.
How the check works
- Find the string length by counting characters until the null character.
- Compare the first character with the last, then move inward.
- Set the flag to 0 if a compared pair is different.
- If the flag remains 1, the program reports that the string is a palindrome.
Worked example
Is madam a palindrome?
Try it first, then show the solutionSolution
- Compare the characters from opposite ends.
- The matching pairs are m with m and a with a.
- The middle character does not need a matching partner.
Answer: The String is palindrome
Two-Dimensional Arrays
Definition
- Two-dimensional array
- A two-dimensional array is a collection of similar elements arranged in rows and columns.
data-type array_name[row-size][col-size];
data-type is a valid C++ data type, array_name is the array's name, row-size is its number of rows, and col-size is its number of columns.
For int A[3][4];, A has 3 rows and 4 columns, so it can hold 12 elements. Array sizes must be unsigned integer values greater than 0. The column size is compulsory, but the row size can be left out.
What the two indexes mean
| Index | Meaning |
|---|---|
| First index | Specifies the row. |
| Second index | Specifies the column. |
| matrix[0][0] | Accesses the first element of the first row. |
A two-dimensional array can be initialized with each row shown separately, or with all the values in one list. If the row size is omitted, the column size must still be given.
Examples of accessing elements
- matrix[0][0]=10; assigns 10 to the first element of the first row.
- matrix[0][1]=20; assigns 20 to the second element of the first row.
- matrix[1][2]=60; assigns 60 to the third element of the second row.
- matrix[3][0]=100; assigns 100 to the first element of the fourth row.
Remember: In a two-dimensional array, the first index is the row and the second index is the column.
Adding Two Matrices
The matrix addition program reads the row and column counts, then reads the elements of two matrices. It adds the elements at matching row and column positions and displays the resulting matrix.
Steps in matrix addition
- Read the number of rows and columns.
- Read the elements of the first matrix.
- Read the elements of the second matrix.
- For each position, calculate sum[i][j]=m1[i][j]+m2[i][j].
- Display each result.
Worked example
Add two 2 by 2 matrices when every element in both matrices is 1.
Try it first, then show the solutionSolution
- Add the elements in each matching position: 1+1=2.
- Repeat this for all four positions.
Answer: The output is 2 2 2 2.
Memory Layout of Two-Dimensional Arrays
A two-dimensional array is arranged in rows and columns. In int A[4][3], A has 4 rows and 3 columns. Its elements occupy continuous memory locations.
Two ways to store a 2-D array
| Row-Major order | Column-Major order |
|---|---|
| Elements are stored across the first row, then across the next row, and so on. | Elements are stored down the first column, then down the next column, and so on. |
| For the given values: 8 6 5 2 1 9 3 6 4 4 3 2. | For the given values: 8 2 3 4 6 1 6 3 5 9 4 2. |
Arrays of Strings
Definition
- Array of strings
- A two-dimensional character array used to store several strings.
The first index gives the number of strings. The second index gives the maximum length of each string, including space for the null character at the end. In char Name[6][10], there are 6 rows and 10 columns, so each string can contain up to 9 characters before the null character.
The declaration char Name[6][10] = {"Mr. Bean", "Mr.Bush", "Nicole", "Kidman", "Arnold", "Jodie"}; initializes the array with 6 strings. The strings are stored in continuous locations.
A loop can print the strings one by one by using each row of the character array. In the example, colour [4][10] stores Blue, Red, Orange and yellow; the output shows Yellow with a capital Y.
Structures and Their Members
Definition
- Structure
- A user-defined type that groups data items of different data types into one unit.
An array is suitable when its elements have the same data type. A structure groups different data types together as one logical element in adjacent memory locations.
Declaring and using a structure
- Use the keyword struct to declare a structure.
- The variables inside it are called members or fields.
- A structure variable allocates memory for its members.
- Use a dot (.) between the structure object name and a member name, such as balu.age.
A structure can be declared with a name, such as Student, or without a name. A structure without a name or tag is called an anonymous structure. Its members can be initialized separately or by placing their values in braces.
For example, balu.rollno= “702016”; balu.age= 18; and balu.weight= 48.5; assign values separately. The values can also be assigned together as balu={702016, 18, 48.5};.
A structure object can be copied to another object of the same structure type. For example, praveen =mahesh; copies Mahesh's age, height and weight to Praveen.
Remember: Access a structure member with object name, dot (.), and member name. Direct structure assignment is possible only when both objects are of the same structure type.
Other Structure Uses
Structure features to remember
- A structure declared inside another structure is called a nested structure.
- A structure can contain an array as a member.
- An array of structure variables can be created.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is an array in C++?
An array is a collection of variables of the same type that are referenced by one common name. Its elements are stored in a fixed number of places, sequentially in memory. This lets a program handle multiple values of the same type using one array name.
How do array subscripts work in C++?
An array subscript is the index written inside square brackets after the array name. Subscripts start at 0, so num[0] refers to the first element and num[3] refers to the 4th element. A subscript can be a variable, constant, or integer expression.
How do you initialize an array in C++?
Give the array values inside curly braces when declaring it. For example, int age[5]={19,21,16,1,50}; stores the values in order from age[0] through age[4]. If values are supplied during declaration, the size can be left out.
How does linear search work in an array?
Linear search compares the value being searched for with each array element. It continues through the elements until a match is found or all elements have been checked. A program can record the matching index or report that the value was not found.
What is a string in C++?
A string is a sequence of characters, such as letters, numbers or symbols. C++ stores a string in a one-dimensional character array. Each character takes one byte, and a null character marks the end of the string.
Why does a character array need one extra location?
A character array used for a string must store the null character at the end. So the array needs one more location than the number of visible characters. For example, char country[6]; can store a string of up to 5 characters.
Show 7 more questions
How do you access an element in a two-dimensional array?
Use two index values inside square brackets. The first index specifies the row, and the second specifies the column. For example, matrix[1][2] accesses the element in the second row and third column.
How does a C++ program add two matrices?
The program reads both matrices and adds elements that have the same row and column indexes. It stores each result using sum[i][j]=m1[i][j]+m2[i][j], then displays the resulting matrix.
What is row-major order in a 2-D array?
In row-major order, the elements are stored in continuous memory locations one row at a time. All the elements of the first row come first, followed by all the elements of the second row, and then the remaining rows.
What do the two indices in char Name[6][10] mean?
The first index, 6, gives the number of strings, or rows. The second index, 10, gives the number of character positions in each row. One position is used for the null character, so each string can contain up to 9 characters.
What is a structure in C++?
A structure is a user-defined type that groups data items of different data types into one unit. The data items inside a structure are called members or fields, and they are accessed using the structure object's name, a dot, and the member name.
How do you access a structure member?
Write the structure object's name, followed by a dot (.), followed by the member name. For example, balu.age accesses the age member of the structure object balu.
When can one structure object be assigned to another?
One structure object can be assigned directly to another when both objects are of the same structure type. The assignment copies the values of the members, as in praveen =mahesh;.
Book back answers
16 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
SECTION – A — Choose the correct answer
Which of the following is the collection of variables of the same type that an referenced by a common name ?
- a) int
- b) float
- c) Array
- d) class
Try it, then show the answerAnswer
c) Array
An array is a collection of same-type variables referenced by a common name.
From the book: “An array is a collection of variables of the same type that are referenced by a common name”
int age[]={6,90,20,18,2}; How many elements are there in this array?
- a) 2
- b) 5
- c) 6
- d) 4
Try it, then show the answerAnswer
b) 5
The initialized list contains five values, so the array has five elements.
From the book: “the size is determined by compiler which depends on the total number of values.”
cin>>n[3]; To which element does this statement accept the value?
- a) 2
- b) 3
- c) 4
- d) 5
Try it, then show the answerAnswer
c) 4
Array subscripts start at 0, so subscript 3 refers to the fourth element.
From the book: “num[3] refers to the 4th element in the array.”
Structure definition is terminated by
- (a) :
- (b) }
- (c) ;
- (d) ::
Try it, then show the answerAnswer
(c) ;
A structure definition ends with a semicolon after its closing brace.
From the book: “struct Student long rollno; int age; float weight; } ;”
Practice
What kind of values does an array group under a common name?
Exam questions
Practice questions with model answers. These are not from an official paper.
Define an array and state the types of arrays described in this lesson.2 marks
An array is a collection of variables of the same type referenced by a common name. The types described are one-dimensional, two-dimensional, and multi-dimensional arrays.
How are elements of a two-dimensional array accessed?2 marks
Use two index values: the first gives the row and the second gives the column. For example, matrix[1][2] refers to an element in the second row and third column.
What is a structure, and how do you access one of its members?2 marks
A structure is a user-defined type that groups data items of different types into one unit. Access a member using the structure object's name, followed by a dot and the member name, such as balu.age.
Watch out
- Do not confuse the array subscript with the element's position: subscript 0 refers to the first element.
- Do not assume arrays and structures group the same kinds of data: arrays group values of one type, while structures can combine different types.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →