🔹 Array Examples in C++

 


C++ arrays store multiple values of the same data type in contiguous memory locations.


1️⃣ Display Marks using Array

📌 Store and display student marks using an array.

#include <iostream>
using namespace std;

int main() {
    int marks[5] = {85, 90, 78, 88, 76};

    cout << "Student Marks: ";
    for (int i = 0; i < 5; i++)
        cout << marks[i] << " ";

    return 0;
}

🔹 Output

Student Marks: 85 90 78 88 76

2️⃣ Find Largest Number using Array

📌 Find the maximum number in an array.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {12, 45, 23, 78, 56};
    int n = sizeof(arr) / sizeof(arr[0]);

    int max = arr[0];
    for (int i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];

    cout << "Largest Number: " << max;
    return 0;
}

🔹 Output

Largest Number: 78

3️⃣ Find Second Largest Number using Array

📌 Finding the second largest number in an array.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {34, 78, 23, 56, 89};
    int n = sizeof(arr) / sizeof(arr[0]);

    int largest = arr[0], secondLargest = -1;
    for (int i = 1; i < n; i++) {
        if (arr[i] > largest) {
            secondLargest = largest;
            largest = arr[i];
        } else if (arr[i] > secondLargest && arr[i] != largest)
            secondLargest = arr[i];
    }

    cout << "Second Largest Number: " << secondLargest;
    return 0;
}

🔹 Output

Second Largest Number: 78

4️⃣ Find Second Smallest Number using Array

📌 Finding the second smallest number.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {12, 45, 23, 78, 56};
    int n = sizeof(arr) / sizeof(arr[0]);

    int smallest = arr[0], secondSmallest = INT_MAX;
    for (int i = 1; i < n; i++) {
        if (arr[i] < smallest) {
            secondSmallest = smallest;
            smallest = arr[i];
        } else if (arr[i] < secondSmallest && arr[i] != smallest)
            secondSmallest = arr[i];
    }

    cout << "Second Smallest Number: " << secondSmallest;
    return 0;
}

🔹 Output

Second Smallest Number: 23

5️⃣ Find Smallest Number using Array

📌 Finding the minimum number in an array.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {12, 45, 23, 78, 56};
    int n = sizeof(arr) / sizeof(arr[0]);

    int min = arr[0];
    for (int i = 1; i < n; i++)
        if (arr[i] < min)
            min = arr[i];

    cout << "Smallest Number: " << min;
    return 0;
}

🔹 Output

Smallest Number: 12

6️⃣ Print Values using Array

📌 Printing values using a loop.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};

    for (int i = 0; i < 5; i++)
        cout << arr[i] << " ";

    return 0;
}

🔹 Output

1 2 3 4 5

7️⃣ Print Array Values using While Loop

📌 Using a while loop to print array values.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int i = 0;

    while (i < 5) {
        cout << arr[i] << " ";
        i++;
    }

    return 0;
}

🔹 Output

10 20 30 40 50

8️⃣ Sum of Array Average using Array

📌 Calculating the sum and average of array elements.

#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int sum = 0, n = 5;

    for (int i = 0; i < n; i++)
        sum += arr[i];

    double avg = sum / (double)n;
    cout << "Sum: " << sum << ", Average: " << avg;
    return 0;
}

🔹 Output

Sum: 150, Average: 30

9️⃣ Transpose Matrix using Array

📌 Transposing a 2D array (matrix).

#include <iostream>
using namespace std;

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int transpose[3][2];

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 3; j++)
            transpose[j][i] = matrix[i][j];

    cout << "Transpose Matrix:\n";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++)
            cout << transpose[i][j] << " ";
        cout << endl;
    }

    return 0;
}

🔹 Output

Transpose Matrix:
1 4
2 5
3 6

🔟 Two Dimensional Array Addition

📌 Adding two 2D arrays (matrices).

#include <iostream>
using namespace std;

int main() {
    int A[2][2] = {{1, 2}, {3, 4}};
    int B[2][2] = {{5, 6}, {7, 8}};
    int C[2][2];

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            C[i][j] = A[i][j] + B[i][j];

    cout << "Matrix Addition:\n";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            cout << C[i][j] << " ";
        cout << endl;
    }

    return 0;
}

🔹 Output

Matrix Addition:
6 8
10 12

📌 Summary

Arrays store multiple values in a single variable.
One-dimensional arrays help store lists of numbers.
Two-dimensional arrays (matrices) help store tabular data.
✔ Use loops (for, while) for easy iteration.
✔ Use nested loops for 2D arrays (matrices).

🚀 Let me know if you need more examples! 🔥

Post a Comment

0 Comments