User gives the program how many people competed in one competition, what were their names and points and does that for three competitions for example:
4
John 49.8
Martin 51.0
Harry 49.5
William 47.7
4
Martin 35.2
John 33.7
Peter 32.9
William 37.1
5
John 56.6
Peter 59.2
Tim 53.3
Arthur 54.2
Meelis 55.4
I need to return the names of those who competed (names can't repeat) so from my example the list would be:
John, Martin, Harry, William, Peter, Tim, Arthur.
How should I do this? I have tried to use strcmp, but I haven't been able to get it work. I tried to copy one array into a fourth one and then check one by one, if the name is not in the fourth array then add it, but this doesn't work (with some arrays it gives wrong names and with others it gives right ones, but it can print some extra characters) and it adds an barrier because I can't check strings that have more than 8 names.
void participants(int n, int m, int s, char i1[][NAMELEN], char i2[][NAMELEN], char i3[][NAMELEN], char i4[][NAMELEN], float t1[], float t2[], float t3[], float t4[]){
int i, j, l;
int k = 1;
for(i = 0; i < m; i++){
strcpy(i4[i], i2[i]); //copy one array of names into the fourth array
t4[i]=t2[i];}
i++;
for(j = 0; j < s; j++){ //check if the name is in fourth array
if (strcmp(i4[0], i3[j]) != 0){
if (strcmp(i4[1], i3[j]) != 0){
if (strcmp(i4[2], i3[j]) != 0){
if (strcmp(i4[3], i3[j]) != 0){
if (strcmp(i4[4], i3[j]) != 0){
if (strcmp(i4[5], i3[j]) != 0){
if (strcmp(i4[6], i3[j]) != 0){
if (strcmp(i4[7], i3[j]) != 0){
strcpy(i4[i], i3[j]); //if the name isn't in fourth array add it there
t4[i] = t3[j];
i++;
for(j = 0; j < s; j++){
if (strcmp(i4[0], i1[j]) != 0){
if (strcmp(i4[1], i1[j]) != 0){
if (strcmp(i4[2], i1[j]) != 0){
if (strcmp(i4[3], i1[j]) != 0){
if (strcmp(i4[4], i1[j]) != 0){
if (strcmp(i4[5], i1[j]) != 0){
if (strcmp(i4[6], i1[j]) != 0){
if (strcmp(i4[7], i1[j]) != 0){
strcpy(i4[i], i1[j]);
t4[i] = t1[j];
i++;
I also tried something to replace numbers with variables (in my code), but this also doesn't work.
void participants(int n, int m, int s, char i1[][NAMELEN], char i2[][NAMELEN], char i3[][NAMELEN], char i4[][NAMELEN], float t1[], float t2[], float t3[], float t4[]){
int i, l, j = -1;
int c;
c = 0;
for(l = 0; l < s; l++){ //copy one array into the fourth one
strcpy(i4[l], i3[l]);
t4[l]=t3[l];
}
while(j < s){
j++;
c=0;
for(i = 0; i < m; i++){
char *ptr = strstr(i3[i], i2[j]);
if (ptr != NULL) /* Substring found */
{
t4[i] = t4[i] + t2[j]; //add points ( I need that in the next task)
}
else /* Substring not found */
{
c++;
}
}
if((c==(m))){
strcpy(i4[l], i2[j]);
t4[l] = t2[j];
l++;
c=0;
}
}
j = -1;
while(j < l){
j++;
c=0;
for(i = 0; i < n; i++){
char *ptr = strstr(i4[i], i1[j]);
if (ptr != NULL) /* Substring found */
{
t4[i] = t4[i] + t1[j];
//c++;
}
else /* Substring not found */
{
c++;
//if(c = )
}
}
if((c==(n))){
strcpy(i4[l], i1[j]);
t4[l] = t1[j];
//l++;
c=0;
}
}
I know the code is not good, but I hope someone can help me.