C++ Arrays – Real-Life Examples

πŸ“¦ C++ Arrays – Real-Life Examples

Arrays are used when we need to store and process multiple related values of the same type.
Below are practical, real-life examples with clear C++ code.


1. Student Marks System

Store marks of students in a class.

int marks[5] = {78, 85, 90, 66, 88};

for (int i = 0; i < 5; i++) {
cout << "Student " << i + 1 << ": " << marks[i] << endl;
}

Use case: Schools, colleges, exams.


2. Daily Temperature Record

Store temperatures for 7 days.

float temp[7] = {32.5, 33.1, 34.0, 35.2, 36.0, 34.8, 33.9};

for (int i = 0; i < 7; i++) {
cout << "Day " << i + 1 << ": " << temp[i] << "Β°C" << endl;
}

Use case: Weather apps.


3. Employee Salaries

int salary[] = {25000, 30000, 28000, 40000};

int size = sizeof(salary) / sizeof(salary[0]);

for (int i = 0; i < size; i++) {
cout << "Employee " << i + 1 << " Salary: " << salary[i] << endl;
}

Use case: Payroll systems.


4. Product Prices in a Shop

double prices[] = {199.99, 349.50, 99.00, 499.00};

double total = 0;

for (double p : prices) {
total += p;
}

cout << "Total Bill: " << total;

Use case: Billing and e-commerce systems.


5. Attendance System (Present / Absent)

bool attendance[5] = {true, false, true, true, false};

for (int i = 0; i < 5; i++) {
cout << "Student " << i + 1 << ": "
<< (attendance[i] ? "Present" : "Absent") << endl;
}

Use case: Schools, offices.


6. Phone Contact Numbers

long long contacts[3] = {9876543210, 9123456789, 9988776655};

for (int i = 0; i < 3; i++) {
cout << "Contact " << i + 1 << ": " << contacts[i] << endl;
}

Use case: Contact management.


7. Monthly Expenses Tracker

int expenses[6] = {5000, 4500, 5200, 4800, 5100, 6000};

int total = 0;

for (int i = 0; i < 6; i++) {
total += expenses[i];
}

cout << "Total Expenses: " << total;

Use case: Personal finance apps.


8. Inventory Stock Count

int stock[4] = {120, 85, 60, 150};

for (int i = 0; i < 4; i++) {
cout << "Item " << i + 1 << " Stock: " << stock[i] << endl;
}

Use case: Warehouse management.


9. Exam Result – Pass or Fail

int marks[] = {35, 67, 89, 28, 50};

for (int i = 0; i < 5; i++) {
if (marks[i] >= 40)
cout << "Student " << i + 1 << ": Pass" << endl;
else
cout << "Student " << i + 1 << ": Fail" << endl;
}


10. Sensor Readings (IoT Example)

int sensor[5] = {45, 48, 50, 47, 46};

for (int value : sensor) {
cout << value << " ";
}

Use case: IoT, embedded systems.


βœ… Key Takeaways

  • Arrays store multiple related values

  • Ideal for marks, prices, salaries, attendance

  • Index starts from 0

  • Size is fixed

  • Use loops to process data efficiently

You may also like...