Reputation: 441
I have this Project. I have arrays with scores, match1, match2 and i have names of the players and their countries abbreviations. then i have those abbreviations and the long names of the country.
I can put together the players name and the country abbreviations, also i can put toggether the country abbreviations and the country long names.
But I don't know how to put together in one array the country long names and the players names.
So, I created the array (char name_and_country[PLAYERS][LENGTH_NAME]) and I want in this array to be names and countries ; so if i printed to say:
char name_and_country[PLAYERS][LENGTH_NAME]={ "David Beckham England", "Wayne Rooney England "....etc.
Can anyone help me with that? Thank you in advance!
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20
int main (void)
{ int match1[PLAYERS] = { 0,1,3,2,4};
int match2[PLAYERS] = { 0,4,0,0,1};
int goals[PLAYERS] ;
char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
char name_and_country_code[PLAYERS][LENGTH_NAME];
char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
char name_and_country[PLAYERS][LENGTH_NAME];
int i, first =1, second= 2;
for(i=0; i < PLAYERS; i++)
{
strcpy (name_and_country_code[i], name[i]);
strcat (name_and_country_code[i], " " );
strcat (name_and_country_code[i], country_abbreviations[i]);
goals[i]= match1[i] + match2[i];
printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
}
Upvotes: 0
Views: 626
Reputation: 44
Use this Code. I have added my code into your code and here is the result. I ran it , it works fine..
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include<string.h>
#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20
int main (void)
{ int match1[PLAYERS] = { 0,1,3,2,4};
int match2[PLAYERS] = { 0,4,0,0,1};
int goals[PLAYERS] ;
char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
char name_and_country_code[PLAYERS][LENGTH_NAME];
char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
char name_and_country[PLAYERS][LENGTH_NAME];
int i, first =1, second= 2;
for(i=0; i < PLAYERS; i++)
{
strcpy (name_and_country_code[i], name[i]);
strcat (name_and_country_code[i], " " );
strcat (name_and_country_code[i], country_abbreviations[i]);
goals[i]= match1[i] + match2[i];
printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
}
for(i=0; i < PLAYERS; i++)
{
strcpy (name_and_country[i], name[i]);
strcat (name_and_country[i], " " );
char country[LENGTH_COUNTRY];
strcpy(country,"DEFAULT COUNTRY"); // Used when player has a invalid country code
int j;
for(j=0;j<NUM_COUNTRIES;j++)
{
if(strcmp(country_abbreviations[i],country_code[j])==0)
strcpy(country,country_name[j]);
}
strcat(name_and_country[i],country);
printf("%s\n",name_and_country[i]);
}
}
Upvotes: 1