This program has to be written in C language since this is a C programming class
ID: 3677898 • Letter: T
Question
This program has to be written in C language since this is a C programming class. We are using DEV - C program. Please not C++.
Once you have the data initialized into the data structure, have the program do the following:
1) Ask the user for a country code:
2) If its a valid code, print the country
3) If its an invaild code, Print error message.
4) if the code is 0, exit teh program, other wise ask for another code.
5) If it is a -1, sort the structure by country code and then print the list.
Thank you for your Help with this.
Explanation / Answer
Answer:)
#include <stdio.h>
#include <string.h>
struct country{
char name[22];
int code;
};
int main(){
struct country c[33];
int i,j;
int co;
int count=0;
char temp_name[22];
int temp_code;
for(i=0;i<5;++i)
{
printf("Enter country name: ");
scanf("%s",c[i].name);
printf("Enter code: ");
scanf("%d",&c[i].code);
printf(" ");
}
printf("Displaying country and its code ");
printf("-----Country----------CODE------------ ");
for(i=0;i<33;++i)
{
printf(" ");
printf("%s",c[i].name);
printf(" ");
printf("%d",c[i].code);
printf(" ");
}
printf("Enter the country code :");
scanf("%d", &co);
for(i=0;i33;++i)
{
if(co == c[i].code)
{
printf("The country name for this code is::%s",c[i].name);
break;
}
else
{
if(co == 0)
{
break;
}
if(co == -1)
{
for(i=0;i<33;++i)
{
for(j=0;j<33;j++)
{
if(c[i].code > c[j].code)
{
temp_code=c[i].code;
strcpy(temp_name,c[i].name);
c[i].code=c[j].code;
strcpy(c[i].name,c[j].name);
c[j].code=temp_code;
strcpy(c[j].name,temp_name);
}
}
}
printf("-----Country----------CODE------------ ");
for(i=0;i<33;++i)
{
printf(" ");
puts(c[i].name);
printf(" ");
printf("%d",c[i].code);
printf(" ");
}
break;
}
}
printf(" ");
count++;
}
if(count == 33)
{
printf("The entered code is invalid. ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.