Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
2.3 kB
3
Indexable
Never
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
#define N 10

typedef struct City
{
    string name;
    int population;
    float area;
    int numOfCOVIDPatients;
}City;

void takeCitiesInfoFromKeyboard(City arr[],int n)
{

    for(int i=0;i<n;i++)
    {
        cout<<"Enter City name,population,area,number of covid cases:";
        cin>>arr[i].name>>arr[i].population>>arr[i].area>>arr[i].numOfCOVIDPatients;
    }
}

float computeAverageNumOfCOVIDPatients(City arr[],int n)
{
    float sum=0;
    for(int i=0;i<n;i++)
    {
        sum+=arr[i].numOfCOVIDPatients;
    }
    float average=sum/n;
    return average;
}

void findAboveAvgCOVIDPateintCities(City arr[],int n)
{
    float avg=computeAverageNumOfCOVIDPatients(arr,n);
    cout<<"City Name - City Population - City area - Num of City COVID Patients\n";
    for(int i=0;i<n;i++)
    {
        if(arr[i].numOfCOVIDPatients>avg)
        {
            cout<<arr[i].name<<"\t\t"<<arr[i].population<<"\t\t"<<arr[i].area<<"\t\t"<<arr[i].numOfCOVIDPatients<<endl;
        }
    }
}

int main()
{
    City arr[N];
    int n=0;
    char ch;
    cout<<"Enter 'a' to take number of COVID patients of all cities into the City Structure array in size "<<N<<endl;
    cout<<"ENter 'b',to find the average number of patients of all the cities."<<endl;
    cout<<"Enter 'c' to display the info of all the Cities that have above average COVID pateints."<<endl;
    cout<<"Enter 'q' to quit/exit the menu system"<<endl;
    do
    {
        cout<<"Enter  your Option:";
        cin>>ch;
        switch (ch)
        {
            case 'a':
                cout<<"\nEnter How many cities you want to add:";
                cin>>n;
                takeCitiesInfoFromKeyboard(arr,n);
                break;
            case 'b':
                cout<<"\nThe average of Cases:"<<computeAverageNumOfCOVIDPatients(arr,n)<<endl;
                break;
            case 'c':
                findAboveAvgCOVIDPateintCities(arr,n);
                break;
            case 'q':
                cout<<"Buy\n";
                break;
            default:
                cout<<"Invalid Input\n";
                break;
        }
    } while (ch!='q');
    
}