1. How to Use This Page
1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.
2. Key Concepts (Quick Recap)
One-Dimensional Arrays
An array is a collection of elements of the same data type stored in contiguous memory locations, accessed by a common name and an index.
// Declaration
int marks[5]; // Uninitialised array of 5 ints
int marks[5] = {90, 85, 78, 92, 88}; // Initialised array
int marks[] = {90, 85, 78}; // Size inferred as 3
| Property | Detail |
|---|---|
| Index range | 0 to size-1 (e.g., for arr[5], indices 0 to 4) |
| Access | arr[index] — e.g., marks[2] gives 78 |
| Default values | Uninitialised local arrays have garbage values |
| Size | Must be a constant or literal, not a variable (in standard C++) |
Two-Dimensional Arrays
A 2D array is an array of arrays — arranged in rows and columns.
// Declaration
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// matrix[0][0] = 1, matrix[1][2] = 6
| Concept | Detail |
|---|---|
| Declaration | type name[rows][cols]; |
| Access | matrix[row][col] |
| Memory layout | Row-major order (row 0 first, then row 1, ...) |
| Total elements | rows x cols |
Structures
A structure groups variables of different data types under a single name.
struct Student {
int rollNo;
char name[20];
float marks;
}; // Note: semicolon is mandatory
// Creating variables
Student s1 = {101, "Kumar", 85.5};
// Accessing members
cout << s1.rollNo; // 101
cout << s1.name; // Kumar
cout << s1.marks; // 85.5
Nested Structures
struct Date {
int day, month, year;
};
struct Student {
int rollNo;
char name[20];
Date dob; // Structure within a structure
};
Student s1 = {101, "Kumar", {15, 8, 2006}};
cout << s1.dob.year; // 2006
Array of Structures
Student classList[3] = {
{101, "Kumar", 85.5},
{102, "Selvi", 92.0},
{103, "Arun", 78.3}
};
cout << classList[1].name; // Selvi
Key Differences: Array vs Structure
| Feature | Array | Structure |
|---|---|---|
| Data types | All elements same type | Members can be different types |
| Access | By index (arr[0]) | By member name (s.name) |
| Purpose | Store collection of similar items | Group related items of different types |
| Memory | Contiguous, equal-sized elements | Contiguous, but member sizes may differ |
| Example | int marks[5]; | struct Student { ... }; |
3. I. Choose the Best Answer (MCQs)
int arr[5] = {10, 20, 30};, what is the value of arr[4]?int a[3] = {5, 10, 15};
cout << a[1];
int m[2][3], how many elements are there?4. II. Fill in the Blanks
1. An array stores elements of the same data type in contiguous memory locations. 2. The first element of an array has an index of 0. 3. In int arr[5], the valid index range is 0 to 4. 4. A structure groups variables of different data types under one name. 5. The dot (.) operator is used to access structure members. 6. A 2D array with 3 rows and 4 columns has 12 total elements. 7. When an array is partially initialised, the remaining elements are set to 0. 8. The keyword used to define a structure is struct. 9. A structure declared inside another structure is called a nested structure. 10. An array of structures stores multiple structure variables of the same type in contiguous memory. 11. In row-major order, elements of the first row are stored first in memory. 12. The size of an array must be a constant (compile-time value) in standard C++. 13. struct Student { ... }; must end with a semicolon.
5. III. True or False (with corrections)
1. All elements of an array must be of the same data type.
True. An array is a homogeneous collection. Every element must be of the type specified in the declaration (e.g., all int, all float).
2. Array indices in C++ start from 1.
False. C++ uses zero-based indexing. The first element is arr[0], not arr[1].
3. A structure can contain members of different data types.
True. A structure is a heterogeneous collection. It can contain int, float, char, and even other structures as members.
4. Accessing arr[5] in an array int arr[5] is valid.
False. In int arr[5], valid indices are 0 to 4. Accessing arr[5] is out of bounds and causes undefined behaviour.
5. The semicolon after the closing brace of a structure definition is optional.
False. The semicolon after }; is mandatory in C++. Omitting it causes a compilation error.
6. A 2D array is stored in row-major order in C++.
True. C++ stores 2D arrays in row-major order — all elements of row 0 come first, then row 1, and so on.
6. IV. Match the Following
| Column A | Column B |
|---|---|
| 1. Array | a. Groups different data types |
| 2. Structure | b. Stores same data type elements |
3. arr[0] | c. First element of an array |
4. . operator | d. Accesses structure members |
| 5. Row-major order | e. 2D array memory layout |
| 6. Nested structure | f. Structure within a structure |
7. sizeof(arr) / sizeof(arr[0]) | g. Calculates number of elements |
7. V. Answer Briefly (2-mark questions)
8. VI. Answer in Detail (5-mark questions)
Student with roll number, name, and marks in three subjects. Write a program to read data for one student, calculate total and average, and display the results.#include <iostream>
using namespace std;
struct Student {
int rollNo;
char name[20];
float marks1, marks2, marks3;
};
int main() {
Student s;
// Reading student data
cout << "Enter roll number: ";
cin >> s.rollNo;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter marks in 3 subjects: ";
cin >> s.marks1 >> s.marks2 >> s.marks3;
// Calculating total and average
float total = s.marks1 + s.marks2 + s.marks3;
float average = total / 3;
// Displaying results
cout << "\n--- Student Report ---" << endl;
cout << "Roll Number: " << s.rollNo << endl;
cout << "Name: " << s.name << endl;
cout << "Marks: " << s.marks1 << ", " << s.marks2 << ", " << s.marks3 << endl;
cout << "Total: " << total << endl;
cout << "Average: " << average << endl;
return 0;
}
9. Common Mistakes to Avoid
1. Off-by-one errors — In an array int arr[5], valid indices are 0-4, not 1-5. Accessing arr[5] is out of bounds.
2. Forgetting the semicolon after struct definition — struct Student { ... }; must end with ;. This is a very common error.
3. Mixing up rows and columns in 2D arrays — matrix[i][j] means row i, column j. The first index is the row, the second is the column.
4. Using = instead of == when comparing arrays — Arrays cannot be directly compared with ==. Use a loop to compare element by element.
5. Array assignment — You cannot assign one array to another with arr1 = arr2. Use a loop or memcpy() to copy arrays.
6. Partial initialisation confusion — int arr[5] = {10}; sets arr[0] = 10 and arr[1] through arr[4] to 0, not all elements to 10.
7. Accessing structure members incorrectly — Use the dot operator s.marks for structure variables, and arrow operator s->marks for pointers to structures.
8. Uninitialised arrays — Local arrays that are not initialised contain garbage values. Always initialise or assign values before reading.
10. Exam Checklist
- [ ] I can declare, initialise, and access 1D arrays
- [ ] I can declare, initialise, and access 2D arrays
- [ ] I can write programs to traverse arrays using loops
- [ ] I know that array indices start at 0 and end at size-1
- [ ] I can define a structure with members of different types
- [ ] I can access structure members using the dot operator
- [ ] I can create and use nested structures
- [ ] I can create and use arrays of structures
- [ ] I can differentiate between arrays (homogeneous) and structures (heterogeneous)
- [ ] I can write programs for sum, average, search, and display using arrays
- [ ] I understand row-major storage order for 2D arrays
- [ ] I remember the semicolon after struct definitions
