Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 42: Arrays in C++ – One-Dimensional Arrays | Class 11

Learn about one-dimensional arrays in C++ in Day 42 of Class 11 Computer Science. Covers declaration, initialization, and examples.

Day 42 – Chapter 5: Arrays in C++

Class 11 Computer Science

What is an Array? (Array क्या है?)

An array in C++ is a collection of elements of the same data type stored in contiguous memory locations. It allows multiple values to be stored and accessed using a single identifier.
C++ में Array समान डेटा प्रकार के तत्वों का एक संग्रह है जो निरंतर मेमोरी स्थानों में संग्रहीत होता है। यह एक ही पहचानकर्ता का उपयोग करके कई मानों को संग्रहीत और एक्सेस करने की अनुमति देता है।

Advantages of Arrays (Array के लाभ)

  • Efficient way to store multiple values of the same type.
  • Allows easy access to elements using indices.
  • Facilitates complex data manipulations like sorting and searching.
  • एक ही प्रकार के कई मानों को संग्रहीत करने का कुशल तरीका।
  • इंडेक्स का उपयोग करके तत्वों तक आसान पहुंच प्रदान करता है।
  • सॉर्टिंग और सर्चिंग जैसे जटिल डेटा हेरफेर को सक्षम करता है।

Declaration of Arrays (Array की घोषणा)

To declare an array in C++, specify the data type, array name, and size.
C++ में Array घोषित करने के लिए, डेटा प्रकार, Array का नाम, और आकार निर्दिष्ट करें।

Syntax:


 array_name[size];

Example:


int numbers[5]; // Array of 5 integers

float marks[10]; // Array of 10 floating-point numbers

char vowels[5]; // Array of 5 characters

Initialization of Arrays (Array का प्रारंभ)

Arrays can be initialized at the time of declaration or later.
Array को घोषणा के समय या बाद में प्रारंभ किया जा सकता है।

Example:


int numbers[5] = {1, 2, 3, 4, 5}; // Initialized during declaration

numbers[0] = 10; // Initialized later

Accessing Array Elements (Array तत्वों का उपयोग)

Elements of an array are accessed using their indices, starting from 0.
Array के तत्वों का उपयोग उनके इंडेक्स का उपयोग करके किया जाता है, जो 0 से शुरू होता है।

Example:


int numbers[5] = {1, 2, 3, 4, 5};

cout << numbers[0]; // Outputs 1

cout << numbers[4]; // Outputs 5

Practical Example of One-Dimensional Array (एक-आयामी Array का व्यावहारिक उदाहरण)

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};
    
    cout << "Array elements are:" << endl;
    for (int i = 0; i < 5; i++) {
        cout << "Element at index " << i << " is " << numbers[i] << endl;
    }

    return 0;
}

Output:

Array elements are:
Element at index 0 is 10
Element at index 1 is 20
Element at index 2 is 30
Element at index 3 is 40
Element at index 4 is 50

Practice Questions

Multiple Choice Questions (MCQs)

  1. What is the starting index of an array in C++?
    (a) 0 | (b) 1 | (c) -1 | (d) None
  2. Which of the following correctly declares an array?
    (a) int numbers[]; | (b) int numbers[5]; | (c) int numbers(5); | (d) None
  3. How are array elements accessed in C++?
    (a) Using indices | (b) Using loops only | (c) Using the array name | (d) None
  4. What will be the output of the following code?
    
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
        int numbers[3] = {1, 2, 3};
    
        cout << numbers[2];
    
        return 0;
    
    }
    
            
    (a) 1 | (b) 2 | (c) 3 | (d) Error
  5. What does the size of an array represent?
    (a) The total elements in the array | (b) The last element's value | (c) The data type | (d) None
  6. What happens if you access an element outside the array size?
    (a) Compilation error | (b) Garbage value | (c) 0 | (d) None
  7. What is the index of the last element in an array of size 10?
    (a) 9 | (b) 10 | (c) 8 | (d) None
  8. What is the output of this code?
    
    int arr[3] = {5, 10, 15};
    
    cout << arr[1];
    
            
    (a) 5 | (b) 10 | (c) 15 | (d) None
  9. Which header file is required to work with arrays?
    (a) iostream | (b) array | (c) stdlib | (d) None
  10. What is the advantage of arrays in C++?
    (a) Store multiple values | (b) Easy access | (c) Both (a) and (b) | (d) None

Answers to MCQs:

1: (a), 2: (b), 3: (a), 4: (c), 5: (a), 6: (b), 7: (a), 8: (b), 9: (a), 10: (c)

Short Answer Questions

  1. What is an array in C++?
    Answer: An array is a collection of elements of the same type stored in contiguous memory locations.
  2. Write the syntax for declaring and initializing an array in C++.
    Answer:
    
    int arr[5] = {1, 2, 3, 4, 5};
    
    
  3. How are elements in an array accessed?
    Answer: Elements are accessed using their indices, starting from 0.
  4. What are the advantages of using arrays?
    Answer: Efficient storage, easy access using indices, and suitable for bulk data manipulation.
  5. Write a program to declare and print a one-dimensional array in C++.
    Answer:
    
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
        int numbers[5] = {10, 20, 30, 40, 50};
    
        for (int i = 0; i < 5; i++) {
    
            cout << numbers[i] << endl;
    
        }
    
        return 0;
    
    }
    
    

Long Answer Questions

  1. Explain arrays in C++ and their importance with examples.
  2. Write a program to demonstrate the initialization and traversal of an array.
  3. Discuss common errors encountered when using arrays in C++.

Post a Comment