Correct answer but Uri does not accept

Asked

Viewed 867 times

0

I’m starting to study pointer and decided to change my codes to use pointers, only I tried to send this solution to Uri and it gave a message - In Queue -,I don’t know where the bug is,.

question link

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

int i;
int *vetor(int v[]);

 int main(int argc, char** argv)
{
  int vet[10],*ponte;
  for(i=0;i<10;i++)
{
    scanf("%d",&vet[i]);
}
  ponte=vetor(vet);
    for(i=0;i<10;i++)
    {
        printf("X[%d] = %d\n",i,ponte[i]);
    }
free(ponte);
return 0;
}

int *vetor(int v[])
{
   int *igual=(int*)malloc(sizeof(int)*10);
   for(i=0;i<10;i++)
   {
      if(v[i]<=0)
      {
        igual[i]=1;
      }
      else
      {
        igual[i]=v[i];
      }
}
  return igual;
}
  • The problem asks for what? That you return the same vector, except that non-positive numbers are exchanged for 1?

1 answer

3


The code looks good - if it works for examples you did, it should work for any case.

The problem may be that although the statement says nothing, there may be large numbers that do not fit into a simple 32-bit integer. Try to replace all occurrences of int for long long - this will make your program work with 64bit integers, and it may be enough.

Another thing is that the "in Queue" status you say is not necessarily a mistake, but that your program is still in line to be tested. Are you sure you have an error message? What is it?

And finally, and in truth, most important of this answer, since its code is essentially correct:

Beware of the indentation!

It’s optional in C, but it’s designed for us humans to read your code. Its code has no logical flaws and is very straight, but is essentially illegible by people, because the indentation is almost random.

Choose a simple rule and follow it: be sure to enter an indentation level "just because it is a function key" - it should not go in column 0. Either put the key in the same row as the block, or put the key in an extra indentation level.

Similarly, the command for that prints the result is not inside a separate block - it must be aligned with the previous line (ponte = ... ) .

They may seem irrelevant details, but start doing so and you will see that even for you the code is much easier to follow.

  • 1

    I’ll follow your tips on indentation, follow your long long tips and now it’s accepted

  • I think the most complete place to read about indentation is the wikipedia article about it: https://en.wikipedia.org/wiki/Indentation_style Try some of your own programs, see what you think is best: it’s about consistency. And, remember, whenever you participate in some other project, it is worth the summer already in use in the project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.