Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i need to include names of countries

+3 votes
asked Jul 17 by Ridwan Bin Shamsurie (170 points)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
//data given in question
int data[5][12]={{130626,54790,18736,7,16,63,131,166,155,223,438,887}
            ,{329047,6222,1488,18,23,118,958,2163,2422,3169,4007,7657},
            {99643,52946,17848,87,79,130,634,581,394,428,500,2251},
            {282143,115351,46499,208,196,301,793,1027,1428,2424,3121,4204},
            {90720,40829,14887,96,157,464,626,718,1063,1413,1197,1250}};

//this function take month as input and return avg tourist of that mounth.
int avg_tourist_month(int n){
    int avg_tourist=0;
    for(int i=0;i<5;i++){
        avg_tourist+=data[i][n-1]; //sum all tourist of the given mounth
    }
    avg_tourist/=5; //calculate avg.
    return avg_tourist; //return avg
    
}
//this function ask for country return avg tourist of that country.
int avg_tourist_country(){
    int avg_tourist=0,n;
    printf("Enter the selected country:");
    scanf("%d",&n);
    for(int i=0;i<12;i++){
        avg_tourist+=data[n-1][i]; //sum all tourist of the given country
    }
    avg_tourist/=12; //calculate avg.
    return avg_tourist; //return avg
    
}
//this function prints highest and lowest tourist of india.
void india_highest_lowest(){
    int max=0,min=INT_MAX;
    for(int i=0;i<12;i++){
        if(data[2][i]>max) //if max is less than current data than update max value
            max=data[2][i];
        if(data[2][i]<min) //if min is more than current data than update min value
            min=data[2][i];
    }
    printf("highest tourist arrival in india is:%d\nlowest tourist arrival in India is:%d\n",max,min); //print the max and min
}
//this function prints lowest monthly average tourist and its corresponding month.
void lowset_monthly_avg(){
    int low=INT_MAX,month,avg;
    for(int i=1;i<13;i++){
        avg=avg_tourist_month(i); //get monthly avg by calling avg_tourist_month function for every month.
        if(avg<low){ //if current month avg is lower than low value.
            low=avg; //update low.
            month=i; //update corresponding month too.
        }
    }
    printf("Lowest monthly average tourist arrival of five country is:%d for mounth:%d\n",low,month); //print low and month.
}

//main function
int main()
{int choice,n;

    while(1){
    //print the msg.
    printf("********************    Arrival of Tourist in 2020    ********************\n");
    printf("1: Enter the month to display the average tourist of the five countries.\n");
    printf("2: Display the average tourist arrival of a selected countries.\n");
    printf("3: Display the highest and lowest tourist arrival for India.\n");
    printf("4: Display the lowest monthly average tourist and its corresponding month.\n");
    printf("5: Exit.\n");
    printf("*****************************************************************************\n");
    printf("Please select your choice:");
    scanf("%d",&choice);//take choice form user
    switch(choice){
        case 1:
        
        printf("Enter the selected month:");
        scanf("%d",&n);
        printf("%d\n",avg_tourist_month(n)); //take value of month and print avg tourist of that mounth
        break;
        
        case 2:
        printf("%d\n",avg_tourist_country()); //print avg tourist of the selected country
        break;
        
        case 3:
         india_highest_lowest(); //print highest and lowest tourist for india.
         break;
        
        case 4:
         lowset_monthly_avg(); //print lowest monthly average tourist and its corresponding month.
         break;
        
        case 5:
            exit(0); //for exit from program.
        
        default:
            printf("Please Enter Vaild choice\n");
    }
    }
}

1 Answer

+2 votes
answered Jul 19 by Peter Minarik (34,450 points)

You can do it in many way.

Using an Array

You can just add an array for the country names and index it just like you index your data.

#define NUM_COUNTRIES 5

const char * countryNames[NUM_COUNTRIES] =
{
    "Country1",
    "Country2",
    "India",
    "Country3",
    "Country4"
};

//data given in question
const int data[NUM_COUNTRIES][12] = 
{
    {130626,  54790, 18736,   7,  16,  63, 131,  166,  155,  223,  438,  887},
    {329047,   6222,  1488,  18,  23, 118, 958, 2163, 2422, 3169, 4007, 7657},
    { 99643,  52946, 17848,  87,  79, 130, 634,  581,  394,  428,  500, 2251},
    {282143, 115351, 46499, 208, 196, 301, 793, 1027, 1428, 2424, 3121, 4204},
    { 90720,  40829, 14887,  96, 157, 464, 626,  718, 1063, 1413, 1197, 1250}
};

Using Structures

With the usage of structures, you can group belonging data together, you do not need to reference multiple individual, independent variables.

#define NUM_COUNTRIES  5
#define NUM_MONTHS    12

typedef struct
{
    const char * countryName;
    int montlyVisitorNumber[NUM_MONTHS];
} TourismData;

const TourismData tourismData[NUM_COUNTRIES] =
{
//   CountryName,      Jan,    Feb,   Mar, Apr, Maj, Jun,  Jul,  Aug,  Sep,  Oct, Nov,  Dec    
// --------------------------------------------------------------------------------------------
    { "Country1", { 130626,  54790, 18736,   7,  16,  63, 131,  166,  155,  223,  438,  887 } },
    { "Country2", { 329047,   6222,  1488,  18,  23, 118, 958, 2163, 2422, 3169, 4007, 7657 } },
    {    "India", {  99643,  52946, 17848,  87,  79, 130, 634,  581,  394,  428,  500, 2251 } },
    { "Country3", { 282143, 115351, 46499, 208, 196, 301, 793, 1027, 1428, 2424, 3121, 4204 } },
    { "Country4", {  90720,  40829, 14887,  96, 157, 464, 626,  718, 1063, 1413, 1197, 1250 } }
};

Later, in your code your can refer to the country names and the visitor numbers as below:

printf("%s had %d visitors\n", tourismData[countryIndex].countryName, tourismData[countryIndex].montlyVisitorNumber[monthIndex]);

Of course, you can use whatever names you like, it just makes sense to use talkative names. Also, there could be other options, but usually arrays and structures (or classes if you're using C++) are the most popular.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...