Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
0
Indexable
Never
#include <iostream>
#include <iomanip>
#include <limits> // Include the limits header for numeric_limits

// Define the structure for household data
struct Household {
    int id;          // Identification number
    double income;   // Annual income
    int members;     // Number of family members
};

const int MAX_HOUSEHOLDS = 13; // Maximum number of households

int main() {
    using namespace std;

    Household households[MAX_HOUSEHOLDS]; // Array to store household data

    // Input data for each household
    for (int i = 0; i < MAX_HOUSEHOLDS; i++) {
        // Prompt and input identification number
        cout << "Please enter the identification number for household " << i + 1 << ": ";
        cin >> households[i].id;

        // Prompt and input annual income
        cout << "Please enter the annual income for household " << i + 1 << " (RM): ";
        cin >> households[i].income;

        // Prompt and input number of family members
        cout << "Please enter the number of family members for household " << i + 1 << ": ";
        cin >> households[i].members;

        // Clear the input buffer
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    // Calculate the average household income
    double totalIncome = 0.0;
    for (int i = 0; i < MAX_HOUSEHOLDS; i++) {
        totalIncome += households[i].income;
    }
    double averageIncome = totalIncome / MAX_HOUSEHOLDS;

    // Display the household data
    cout << "\nHousehold Data:\n";
    cout << "ID\tIncome\tMembers\n";
    for (int i = 0; i < MAX_HOUSEHOLDS; i++) {
        cout << households[i].id << "\tRM" << fixed << setprecision(2) << households[i].income << "\t" << households[i].members << "\n";
    }

    // Display households with income above average
    cout << "\nHouseholds with income above average:\n";
    cout << "ID\tIncome\n";
    for (int i = 0; i < MAX_HOUSEHOLDS; i++) {
        if (households[i].income > averageIncome) {
            cout << households[i].id << "\tRM" << fixed << setprecision(2) << households[i].income << "\n";
        }
    }

    // Calculate and display the percentage of households below the poverty level
    int householdsBelowPoverty = 0;
    for (int i = 0; i < MAX_HOUSEHOLDS; i++) {
        double povertyLevel = 6500.00 + 950.00 * (households[i].members - 2);
        if (households[i].income < povertyLevel) {
            householdsBelowPoverty++;
        }
    }
    double percentageBelowPoverty = (static_cast<double>(householdsBelowPoverty) / MAX_HOUSEHOLDS) * 100.0;

    // Display the percentage of households below the poverty level
    cout << "\nPercentage of households below the poverty level: " << fixed << setprecision(2) << percentageBelowPoverty << "%" << endl;

    return 0;
}