a Write program that declares a structure to store country name and population in millions. It defines two structure variables and inputs values. It passes both variables to a function that shows the record of the country with more population.
To write a program that accomplishes the given task, you will need to follow these steps:
1. Declare a structure to store the country's name and its population in millions. Let's call this structure "Country".
```c
struct Country {
char name[50];
int population;
};
```
2. Define two structure variables, let's call them "country1" and "country2", of the newly declared "Country" structure.
```c
struct Country country1, country2;
```
3. Input values for both of the structure variables using something like `scanf` or any other method you prefer.
```c
printf("Enter the name of the first country: ");
scanf("%s", country1.name);
printf("Enter the population of the first country (in millions): ");
scanf("%d", &country1.population);
printf("Enter the name of the second country: ");
scanf("%s", country2.name);
printf("Enter the population of the second country (in millions): ");
scanf("%d", &country2.population);
```
4. Create a function, let's call it "showCountryWithMorePopulation", that takes the two country structure variables as arguments and compares their populations. The function will display the record of the country with the higher population.
```c
void showCountryWithMorePopulation(struct Country country1, struct Country country2) {
if (country1.population > country2.population) {
printf("%s has a higher population with %d million people.\n", country1.name, country1.population);
} else if (country2.population > country1.population) {
printf("%s has a higher population with %d million people.\n", country2.name, country2.population);
} else {
printf("Both countries have the same population of %d million people.\n", country1.population);
}
}
```
5. Finally, call the "showCountryWithMorePopulation" function, passing the two country structure variables as arguments.
```c
showCountryWithMorePopulation(country1, country2);
```
Here's the complete program:
```c
#include <stdio.h>
struct Country {
char name[50];
int population;
};
void showCountryWithMorePopulation(struct Country country1, struct Country country2) {
if (country1.population > country2.population) {
printf("%s has a higher population with %d million people.\n", country1.name, country1.population);
} else if (country2.population > country1.population) {
printf("%s has a higher population with %d million people.\n", country2.name, country2.population);
} else {
printf("Both countries have the same population of %d million people.\n", country1.population);
}
}
int main() {
struct Country country1, country2;
printf("Enter the name of the first country: ");
scanf("%s", country1.name);
printf("Enter the population of the first country (in millions): ");
scanf("%d", &country1.population);
printf("Enter the name of the second country: ");
scanf("%s", country2.name);
printf("Enter the population of the second country (in millions): ");
scanf("%d", &country2.population);
showCountryWithMorePopulation(country1, country2);
return 0;
}
```
This program will display the country with the higher population.