c - 48
better write
c-'0' - and you do not need your comment
where do you use the num? You need it to decide that the line is over...
Also shouldn't it be 2 empty lines between tests?
Printable View
c - 48
better write
c-'0' - and you do not need your comment
where do you use the num? You need it to decide that the line is over...
Also shouldn't it be 2 empty lines between tests?
vart
I'm not using it, should I? I'm determining when line ends with this comparisson:Quote:
where do you use the num? You need it to decide that the line is over...
while ((c = getc(stdin)) != '\n')
But it could be altered if you think that's the problem...
I beleive the spaces are correct.Quote:
Also shouldn't it be 2 empty lines between tests?
Are you guaranteed that the numbers will all be on one line?
And in any event, using the num as your loop guard will allow you to not have to read in numbers character by character.
Also, are you sure you want to do a break in your loop? You'll have to keep reading to read in the rest of the numbers. --Also a reason to use num as your loop guard.
Tabstop
I understand the problem now. Just couldn't solve it....
I came up with this, but it enters into an infinite loop:
Code:#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int start = 1, numero = 0, n, c, posicao = 1, teste = 1;
scanf("%d", &n);
while (n > 0)
{
posicao = 1;
while (posicao <= n + 1)
{
c = getc(stdin);
if (c == 32) // if c equals space
{
if (numero == posicao++)
printf("Teste %d\n%d\n\n", teste++, posicao - 1);
numero = 0;
} else
numero = numero*10 + (c - '0');
}
scanf("%d", &n);
}
return 0;
}
Since you only process a number when you see a space, this will only work if every number (even the last one on a line) has a space after it. Hint: there is no good (or even neutral) reason to use getc in this program.
If you're given a fixed number for N, use a for loop!
Keep in mind though, you can't just break out of the for loop when you find the answer or you won't read in the rest of the line.Code:while (1)
// read in N
for (i = 1; i <= N; i++)
// read in a number (you don't need to use getc)
// is that number equal to i?
// yes? - print answer
// no? - ignore it
I got it! Thank you so much guys, you really helped me out here.
Here's the solution in case anyone wants it, it's so dam simple =P
Code:#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int start = 1, numero = 0, n, i, posicao = 1, teste = 1;
scanf("%d", &n);
while (n > 0)
{
posicao = 1;
for (i = 0; i < n; i++)
{
scanf("%d", &numero);
if (numero == posicao++)
printf("Teste %d\n%d\n\n", teste++, numero);
}
scanf("%d", &n);
}
return 0;
}