Please help me out with this problem...i have tried all the suggestion mentioned in this thread already...still getting a WA
Code: Select all
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
void ins_alpha(char *, char [2000][75], int [2000], int&);
void sort(char [2000][75], int [2000], int);
int main(void)
{
int n, number = 0;
char countries[2000][75] = {{}};
int frequency[2000] = {};
cin>>n;
getchar();
for(int x=0; x<n; x++)
{
char ch[75] = "";
char country[75] = "";
cin.getline(ch, 76);
char* found = strchr(ch, ' ');
if(found)
strncpy(country, ch, found-ch);
else
strcpy(country, ch);
ins_alpha(country, countries, frequency, number);
}
sort(countries, frequency, number);
for(int i=0; i<number; i++)
{
cout<<countries[i]<<" "<<frequency[i];
if(i!=number-1)
cout<<"\n";
}
return 0;
}
void ins_alpha(char* country, char countries[2000][75], int frequency[2000], int& number)
{
for(int n=0; n<2000; n++)
if(strcmp(countries[n], country)==0)
{
frequency[n]++;
return;
}
strcpy(countries[number], country);
frequency[number]++;
number++;
}
void sort(char countries[2000][75], int frequency[2000], int number)
{
for(int i=0; i<number-1; i++)
{
int min = i;
for(int j=i+1; j<number; j++)
if(strcmp(countries[j], countries[min]) < 0)
min = j;
int temp_f = frequency[i];
frequency[i] = frequency[min];
frequency[min] = temp_f;
char temp[75];
strcpy(temp, countries[i]);
strcpy(countries[i], countries[min]);
strcpy(countries[min], temp);
}
}
Any help/hints will be greatly appreciated !