Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To be coded in C With the 2018 FIFA World Cup tournament under way, let us do a

ID: 3911657 • Letter: T

Question

To be coded in C

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.csv1 , 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[1]) form the command line.

2. Do not modify the lines (source code) that are provided in the base files, only add to the files.

3. 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.

4. You need to also have a makefile (for easy compilation) to create an executable program called worldcup along with the three source/header file.

5. Submit a zip file containing country.h, country.c, worldcup.c, and Makefile

6. Your project will be graded in a similar fashion as shown below in example executions.

Explanation / Answer

country.h

#include<stdio.h>
#include<string.h>

struct country {

    char name[50];
    int group;
    int goalsscored;
    int points;
};

void search_by_country(struct country data[],char *name, int n);
void search_by_group(struct country data[],int g, int n);
void search_by_points(struct country data[],int p, int n);

country.c

#include "country1.h"

void search_by_country(struct country data[],char *name, int n){
    int i;
    int found = 1;
     for (i = 0; i<n; i++){
         //printf("%s %s ",data[i].name, name);
         int match = 1;
         int j;
         for (j = 0; j<strlen(data[j].name); j++){
            if (data[i].name[j] != name[j]){
               match = 0;
               break;
            }
         }
         if (match == 1){
            found = 1;
            printf("%s ",data[i].name);
         }
     }
     if (found == 0)
        printf("Not Found ");

}
void search_by_group(struct country data[],int g, int n){

    int i;
    int found = 0;
     for (i = 0; i<n; i++){
         if (data[i].group == g){
            found = 1;
            printf("%s ",data[i].name);
         }
     }
     if (found == 0)
        printf("Not Found ");

}
void search_by_points(struct country data[],int p, int n){
    int i;
    int found = 0;
     for (i = 0; i<n; i++){
         if (data[i].points == p){
            found = 1;
            printf("%s ",data[i].name);
         }
     }
     if (found == 0)
        printf("Not Found ");
}

worldcup.c

#include<stdio.h>
#include<stdlib.h>
#include "country1.h"

int main(){

    FILE *fp;
    struct country data[100];
    fp = fopen("worldcup.csv1","r");
    if (fp == NULL){
       printf("Error opening file ");
       return 0;
    }
    char line[100];
    char name[50];
    int count = 0;
    while (fgets(line,100,fp) != NULL){
         char *ch = strtok(line,",");
         strcpy(data[count].name,ch);
         ch = strtok(NULL,",");
         data[count].group = atoi(ch);
         ch = strtok(NULL,",");
         data[count].goalsscored = atoi(ch);
         ch = strtok(NULL,",");
         data[count].points = atoi(ch);
         count++;
    }
    while (1) {
        printf("1.Search by name ");
        printf("2.Search by group ");
        printf("3.Search by points ");
        printf("4.Exit ");
        printf("Enter choice: ");
        int n;
        fgets(name,50,stdin);
        n = atoi(name);
        if (n == 4)
           break;
        if (n == 1){
           printf("Enter name: ");
           fgets(name,50,stdin);
           //printf(name);
           search_by_country(data,name,count);
        }
        if (n == 2){
           printf("Enter group: ");
           int m;
           scanf("%d",&m);
           search_by_group(data,m,count);
        }
        if (n == 3){
           printf("Enter points: ");
           int m;
           scanf("%d",&m);
           search_by_points(data,m,count);
        }
    }
}

Makefile:

gcc country.c worldcup.c

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote