Current Affairs updated daily
KalviGavaniKalviGavani
Class 11 · Tamil Nadu State Board

Arrays & Structures Exercise Solutions | KalviGavani

TN Class 11 Computer Science Arrays and Structures exercise solutions — 1D/2D arrays, struct definition, nested structures with worked examples.

Updated 15 June 202613 min read

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
PropertyDetail
Index range0 to size-1 (e.g., for arr[5], indices 0 to 4)
Accessarr[index] — e.g., marks[2] gives 78
Default valuesUninitialised local arrays have garbage values
SizeMust 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
ConceptDetail
Declarationtype name[rows][cols];
Accessmatrix[row][col]
Memory layoutRow-major order (row 0 first, then row 1, ...)
Total elementsrows 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

FeatureArrayStructure
Data typesAll elements same typeMembers can be different types
AccessBy index (arr[0])By member name (s.name)
PurposeStore collection of similar itemsGroup related items of different types
MemoryContiguous, equal-sized elementsContiguous, but member sizes may differ
Exampleint marks[5];struct Student { ... };

3. I. Choose the Best Answer (MCQs)

1. In C++, array indices start from:
Answer

(a) 1 (b) 0 (c) -1 (d) Any value

Answer(b) 0 C++ arrays use zero-based indexing. The first element is arr[0], the second is arr[1], and the last element in an array of size n is arr[n-1].
2. If int arr[5] = {10, 20, 30};, what is the value of arr[4]?
Answer

(a) 30 (b) Garbage value (c) 0 (d) Error

Answer(c) 0 When an array is partially initialised, the remaining elements are automatically set to zero. So arr[3] and arr[4] are both 0.
3. Which of the following correctly declares a structure in C++?
Answer

(a) struct Student { int id; }; (b) structure Student { int id; }; (c) struct Student(int id); (d) Student struct { int id; }

Answer(a) struct Student { int id; }; The correct syntax uses the keyword struct, followed by the structure name, member declarations in braces, and ends with a semicolon.
4. What is the output of the following?
int a[3] = {5, 10, 15};
cout << a[1];
Answer

(a) 5 (b) 10 (c) 15 (d) Error

Answer(b) 10 Array index 1 refers to the second element. a[0] = 5, a[1] = 10, a[2] = 15.
5. In a 2D array int m[2][3], how many elements are there?
Answer

(a) 2 (b) 3 (c) 5 (d) 6

Answer(d) 6 A 2D array m[2][3] has 2 rows and 3 columns, so the total number of elements is 2 x 3 = 6.
6. Which operator is used to access structure members?
Answer

(a) [] (b) () (c) . (d) ->

Answer(c) . The dot operator (.) is used to access structure members through a variable. The arrow operator (->) is used with pointers to structures.

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 AColumn B
1. Arraya. Groups different data types
2. Structureb. Stores same data type elements
3. arr[0]c. First element of an array
4. . operatord. Accesses structure members
5. Row-major ordere. 2D array memory layout
6. Nested structuref. Structure within a structure
7. sizeof(arr) / sizeof(arr[0])g. Calculates number of elements
Answer1-b, 2-a, 3-c, 4-d, 5-e, 6-f, 7-g

7. V. Answer Briefly (2-mark questions)

Q1. What is an array? How is it declared?
Answer

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.

// Syntax: type name[size];
int marks[5] = {80, 75, 90, 85, 70};

Advantages: avoids declaring separate variables for each element, enables efficient access by index, and supports loops for bulk processing.

Q2. Differentiate between a 1D array and a 2D array.
Answer
Feature1D Array2D Array
DimensionOne (linear)Two (rows and columns)
Declarationint a[5];int a[2][3];
Accessa[index]a[row][col]
ExampleList of marksMatrix, table
Elementssizerows x cols
Q3. What is a structure? Write the syntax.
Answer

A structure is a user-defined data type that groups variables of different data types under a single name.

struct Student {
    int rollNo;
    char name[20];
    float marks;
};  // semicolon is mandatory

Members are accessed using the dot operator: s1.rollNo, s1.name, s1.marks.

Q4. What is an array of structures? Give an example.
Answer

An array of structures is an array where each element is a structure variable. It is used to store a collection of similar records.

struct Student {
    int rollNo;
    char name[20];
    float marks;
};
Student classList[3] = {
    {101, "Kumar", 85.5},
    {102, "Selvi", 92.0},
    {103, "Arun", 78.3}
};
// Access: classList[0].name gives "Kumar"
Q5. What is a nested structure? Give an example.
Answer

A nested structure is a structure that contains another structure as one of its members.

struct Date {
    int day, month, year;
};
struct Student {
    int rollNo;
    Date dob;   // Date is nested inside Student
};
// Access: s1.dob.year

8. VI. Answer in Detail (5-mark questions)

Q1. Explain one-dimensional arrays with declaration, initialisation, and a program to find the sum and average of array elements.
Answer

A one-dimensional array is a linear collection of elements of the same data type stored in contiguous memory.

Declaration: type name[size]; Initialisation: type name[size] = {val1, val2, ...};

#include <iostream>
using namespace std;
int main() {
    int marks[5] = {80, 75, 90, 85, 70};
    int sum = 0;
    float average;

    // Calculate sum
    for (int i = 0; i < 5; i++) {
        sum = sum + marks[i];
    }

    // Calculate average
    average = (float)sum / 5;

    cout << "Marks: ";
    for (int i = 0; i < 5; i++) {
        cout << marks[i] << " ";
    }
    cout << endl;
    cout << "Sum = " << sum << endl;
    cout << "Average = " << average << endl;

    return 0;
}
// Output:
// Marks: 80 75 90 85 70
// Sum = 400
// Average = 80

Key points:

  • Array size must be specified at declaration (or inferred from initialiser list).
  • Indices range from 0 to size-1.
  • Partial initialisation sets remaining elements to 0.
  • Use a for loop to traverse all elements.
Q2. Explain two-dimensional arrays with a program to read and display a matrix.
Answer

A 2D array is organised in rows and columns. It is declared as type name[rows][cols]; and accessed using name[row][col].

#include <iostream>
using namespace std;
int main() {
    int matrix[2][3];
    int rows = 2, cols = 3;

    // Reading elements
    cout << "Enter 6 elements for a 2x3 matrix:" << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cin >> matrix[i][j];
        }
    }

    // Displaying the matrix
    cout << "Matrix:" << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }

    return 0;
}

Key points:

  • The outer loop iterates over rows, the inner loop over columns.
  • In memory, elements are stored in row-major order: matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], etc.
  • Total elements = rows x cols.
Q3. Define a structure 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;
}
Answer

Key points:

  • struct keyword defines the structure template.
  • Members are accessed using the dot operator (.).
  • Structure variables are declared like any other variable: Student s;.
  • All members of a structure can be different data types.

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

Frequently Asked Questions

An array stores multiple elements of the same data type under one name. A structure groups variables of different data types under one name. Arrays are homogeneous; structures are heterogeneous.
← Browse all Class 11 Exercise Solutions

Other question types — same chapter