-4

I was trying to solve a problem( https://www.codechef.com/problems/COOK82A) on an online judge. Following is the code for the problem.When i submit the judge gives runtime error.Can anybody help me

#include <stdio.h>


int main(void) 

{

    int a,i,j,flag,pos,flag1,pos1;
    char team[15];
    int score[4];
    scanf("%d",&a);
    while(a--)
    {

        for(i=0;i<4;i++)
        {
            scanf("%s",&team[i]);
            scanf("%d",&score[i]);
        }
        for(i=0;i<4;i++)
        {

            if(team[i]=="Barcelona")
            pos=i;
            if(team[i]=="Eibar")
            flag=i;
            if(team[i]=="RealMadrid")
            pos1=i;
            if(team[i]=="Malaga")
            flag1=i;

        }
            if((score[pos]>score[flag]) && (score[flag1]>score[pos1]))
            printf("Barcelona\n");
            else
            printf("RealMadrid\n");
        }
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ May 25 '17 at 11:05
  • 2
    Please refrain asking questions about online code judge engines here. It's very unlikely that anyone could tell you where you failed from their test cases, as these aren't disclosed usually. Even if what you tested was running at your local environment, you may have missed to test some edge cases which are applied in the online challenge. Be creative and try to find them. Additionally there's probably no value for such questions in the long term, other than cheating the online contest, and nothing is learned. – πάντα ῥεῖ May 25 '17 at 11:05
  • 1
    `team[i]` is a `char`. `"Barcelona"` is a `char*`. – Barmar May 25 '17 at 11:06
  • You seem to have lots of misudnerstandings about the difference between strings and chars. – Barmar May 25 '17 at 11:07
  • 2
    The cause is a failure to debug. – ThingyWotsit May 25 '17 at 11:10

2 Answers2

2

In your code

  scanf("%s",&team[i]);

is wrong, you don't have space to store a string. You can store a char in the memory pointed by &team[i], but not a null-terminated char array.

To put it in other way, your team is an array of chars, whereas, you seem to be in need of array of char arrays, something like

#define NAMESIZ 32
#define TEAMSIZ 15

char team[TEAMSIZ][TEAMSIZ] = {0};

and then, properly using team[i] (boils to an address itself) can help.


After this error, there's one more logical error which must be fixed. You are using == operator to compare strings which is not possible. You need to use strcmp() to compare strings (i.e., contents of null-terminated char arrays).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

In your code,

scanf("%s",&team[i]); is wrong.

you declared a team[15] as a character array, but are storing the no. of strings in it. if you want to do so,

scanf("%s",&team[i])

then declare a two dimensional character array like.

char arrayOfWords[NUMBER_OF_WORDS][MAX_SIZE_OF_WORD];

then you need to enter the string this way: int i;

for (i=0; i<NUMBER_OF_WORDS; i++) {
    scanf ("%s" , arrayOfWords[i]);
}

and to print or process this array, you need to do:

for (i=0; i<NUMBER_OF_WORDS; i++) {
    printf ("%s" , arrayOfWords[i]);
}
suraj
  • 403
  • 4
  • 15