I apologize if this is a long question I would love any help you can provide me.
ID: 3915280 • Letter: I
Question
I apologize if this is a long question I would love any help you can provide me. I have gotten a chunk of this done however I am still running into some problems. Would like to see how you guys would tackle this problem. I am having some sorting issues. It has to be done in C. Thank you.
With the 2018 FIFA World Cup tournament under way, let us do a project based on it. There are 32 competing nations in the tournament, divided into 8 groups, A through H, with each group having 4 countries. So far, the group stage games have been played, with each country playing exactly 3 matches. A CSV file provided for this project, worldcup.csv ,contains 32 records, one for each country. Each record has 4 fields-the name of the country, the group the country belongs to, goals scored, and points earned. In this project, your will be reading in the CSV file into an array of structure and implementing functions that will search this array based on the country name, group, or points earned (input by user). For your convenience, I have provided three base files (country.h, country.c and, worldcup.c) to start with, study them and complete this project by adding your code to these base files. country.h: This file contains definitions and prototypes of functions that are defined in country. c. The structure country is also defined to store information about a country. You do not require to modify this file country.c: This file contains the function definitions that you need to complete. There are three functions, search_by_ country, search_by group, and search _by_points that you need to complete. In each of these function, prompt the user to enter country/group/points and print out the records that match. If there is no match, you can simply not print anything. See example execution to get an idea. worldcup.c: This file has the main function. You can see an array of structure country here Complete the main function by reading in the records from the CSV file (name is passed from command line) into this array IMPORTANT 1. Take input filename (argv[1D form the command line 2. Do not modify the lines (source code) that are provided in the base files, only add to the files You do not need to modify country.h at all, just complete main function in worldcup.c and search by country, search by group. search by points functions in country.c. You need to also have a makefile (for easy compilation) to create an executable program called worldcup along with the three source/header file Submit a zip file containing country.h, country.c, worldcup.c, Makefile 3. 4. 5. andExplanation / Answer
It would have been nice if you had given copyable code i.e plain text rather than image... All three files will be difficult to be re-typed.
I am giving you the steps to follow ... its just copy-paste the code given below into appropriate files at correct locations....
please let me know in case of any help... I will help. Please remember, you will need to pass the input filename i.e. worldcup.csv after ./a.out when executing the program.
Step 1: In the file country.c, copy the body of each of these functions
void search_by_country(struct country countries[]){
char name[NAME_LEN+1];
int i;
printf("Enter name of country: ");
gets(name);
printf("%-25s %-10s %-10s %-10s ", "Country", "Group", "Goals", "Points");
for(i = 0; i < NUM_COUNTRIES; i++){
if(strcmp(countries[i].name, name) == 0)
printf("%-25s %-10c %-10d %-10d ", countries[i].name, countries[i].group, countries[i].goals, countries[i].points);
}
printf(" ");
}
void search_by_group(struct country countries[]){
char group;
int i;
printf("Enter group: ");
scanf("%c", &group);
printf("%-25s %-10s %-10s %-10s ", "Country", "Group", "Goals", "Points");
for(i = 0; i < NUM_COUNTRIES; i++){
if(countries[i].group == group)
printf("%-25s %-10c %-10d %-10d ", countries[i].name, countries[i].group, countries[i].goals, countries[i].points);
}
printf(" ");
}
void search_by_points(struct country countries[]){
int points
int i;
printf("Enter points: ");
scanf("%d", &points);
printf("%-25s %-10s %-10s %-10s ", "Country", "Group", "Goals", "Points");
for(i = 0; i < NUM_COUNTRIES; i++){
if(countries[i].points == points)
printf("%-25s %-10c %-10d %-10d ", countries[i].name, countries[i].group, countries[i].goals, countries[i].points);
}
printf(" ");
}
void selection_sort(struct country countries[], int n){
int i, j, minIdx;
struct country temp;
for(i = 0; i < n; i++){
minIdx = i;
for(j = i+1; j < n; j++){
if(countries[j].goals < countries[minIdx].goals)
minIdx = j;
}
//swap?
if(i != minIdx){
temp = countries[i];
countries[i] = countries[minIdx];
countries[minIdx] = temp;
}
}
}
void print_sorted_by_goals(struct country countries[]){
selection_sort(countries, NUM_COUNTRIES);
printf("%-25s %-10s %-10s %-10s ", "Country", "Group", "Goals", "Points");
for(i = 0; i < NUM_COUNTRIES; i++){
printf("%-25s %-10c %-10d %-10d ", countries[i].name, countries[i].group, countries[i].goals, countries[i].points);
}
printf(" ");
}
Step 2: In the file worldcup.c, change the initial part of main() to the one given below.... up to the line for(;;) {.... Beyond that line the code given by instructor will remain same.
int main(int argc, char *argv[]){
struct country countries[NUM_COUNTRIES];
char code;
int i;
if(argc != 2){
printf("Please pass the input filename as command line argument ");
printf("Usage: %s <input filename> ", argv[0]);
return 1;
}
FILE *fp = fopen(argv[1], "r");
char line[256];
char *token;
if(fp == NULL){
printf("ERROR: could not read input file %s ", argv[1]);
return 1;
}
fgets(line, 256, fp); //read the column names
for(i = 0; i < NUM_COUNTRIES; i++){
fgets(line, 256, fp);
token = strtok(line, ", ");
strcpy(countries[i].name, token);
token = strtok(line, NULL);
countries[i].group = token[0];
token = strtok(line, NULL);
countries[i].goals = atoi(token);
token = strtok(line, NULL);
countries[i].points = atoi(token);
}
fclose(fp);
print_sorted_by_goals(countries);
////////////////////////////REST OF THE CODE AS IS FROM HERE ONWARDS ................
for(;;){
printf("Search by country: c "); //....................
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.