# Detecting an empty line in stdin

Discussion in 'C Programming' started by Albert, Jan 13, 2010.

1. ### AlbertGuest

My current code to this problem:

Problem E: Jolly Jumpers
A sequence of n > 0 integers is called a jolly jumper if the absolute
values of the difference between successive elements take on all the
values 1 through n-1. For instance,

1 4 2 3

is a jolly jumper, because the absolutes differences are 3, 2, and 1
respectively. The definition implies that any sequence of a single
integer is a jolly jumper. You are to write a program to determine
whether or not each of a number of sequences is a jolly jumper.
Input

Each line of input contains an integer n <= 3000 followed by n integers
representing the sequence.
Output
For each line of input, generate a line of output saying "Jolly" or "Not
jolly".
Sample Input

4 1 4 2 3
5 1 4 2 -1 6

Sample Output

Jolly
Not jolly

is:

#include <stdio.h>

#define MAXN 2999
#define MAXDIFF 2999

{
int i;

for (i = 0; i < n; i++)
scanf("%d", a + i);
}

int absdiff(int a, int b)
{
return a>b ? a-b : b-a;
}

int isjolly(int n, int a[])
{
if (n == 1)
return 1;
else {
int takenon[MAXDIFF], i;
for (i = 1; i <= n-1; i++)
takenon = 0;

for (i = 0; i < n-1; i++)
takenon[absdiff(a, a[i+1])] = 1;
for (i = 1; i <= n-1 && takenon; i++)
;
return i == n ? 1 : 0;
}
}

int main()
{
int n, num[MAXN];
int i;

while (scanf("%d", &n)) {
if (n) {
if (isjolly(n, num))
puts("Jolly");
else
puts("Not jolly");
}
}

return 0;
}

How do I get my program to detect an empty line of input? That is, where
just enter is pressed?

TIA,
Albert

Albert, Jan 13, 2010

2. ### Chris McDonaldGuest

Albert <> writes:

>My current code to this problem:

An ACM Prog. Comp. problem??

>How do I get my program to detect an empty line of input? That is, where
>just enter is pressed?

First read in the line with fgets(), check its return value and line read
for end of file or an empty line, then use sscanf() on the line read.

--
Chris.

Chris McDonald, Jan 13, 2010

3. ### Michael TsangGuest

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Albert wrote:

> My current code to this problem:
>
> Problem E: Jolly Jumpers
> A sequence of n > 0 integers is called a jolly jumper if the absolute
> values of the difference between successive elements take on all the
> values 1 through n-1. For instance,
>
> 1 4 2 3
>
> is a jolly jumper, because the absolutes differences are 3, 2, and 1
> respectively. The definition implies that any sequence of a single
> integer is a jolly jumper. You are to write a program to determine
> whether or not each of a number of sequences is a jolly jumper.
> Input
>
> Each line of input contains an integer n <= 3000 followed by n integers
> representing the sequence.
> Output
> For each line of input, generate a line of output saying "Jolly" or "Not
> jolly".
> Sample Input
>
> 4 1 4 2 3
> 5 1 4 2 -1 6
>
> Sample Output
>
> Jolly
> Not jolly
>

OI problem?! Where do you find it?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAktNsrwACgkQm4klUUKw07C6wwCePVtwvz2JiKIh+gWVBTf/O/oi
MoYAn1bW2MWBJKppS9sDKvvPdqHDrJ/h
=kF5R
-----END PGP SIGNATURE-----

Michael Tsang, Jan 13, 2010
4. ### Ben BacarisseGuest

Albert <> writes:
<snip>
> Problem E: Jolly Jumpers
> A sequence of n > 0 integers is called a jolly jumper if the absolute
> values of the difference between successive elements take on all the
> values 1 through n-1. For instance,
>
> 1 4 2 3
>
> is a jolly jumper, because the absolutes differences are 3, 2, and 1
> respectively. The definition implies that any sequence of a single
> integer is a jolly jumper. You are to write a program to determine
> whether or not each of a number of sequences is a jolly jumper.
> Input
>
> Each line of input contains an integer n <= 3000 followed by n
> integers representing the sequence.

<snip>
> int main()
> {
> int n, num[MAXN];
> int i;
>
> while (scanf("%d", &n)) {
> if (n) {
> if (isjolly(n, num))
> puts("Jolly");
> else
> puts("Not jolly");
> }
> }
>
> return 0;
> }
>
> How do I get my program to detect an empty line of input? That is,
> where just enter is pressed?

question. The problem statement says that there will be no blank
lines. What probably need is a test for the end of the input or, more
helpfully, a test for the input having succeeded. Did this not come
up before in another such problem?

You will be better off writing:

while (scanf("%d", &n) == 1) {...}

As you have it the loop keeps going even if scanf returns EOF. When
it does, the test on n later is not helpful.

--
Ben.

Ben Bacarisse, Jan 13, 2010
5. ### Ben BacarisseGuest

Michael Tsang <> writes:

> Albert wrote:
>
>> Problem E: Jolly Jumpers
>> A sequence of n > 0 integers is called a jolly jumper if the absolute
>> values of the difference between successive elements take on all the
>> values 1 through n-1. For instance,
>>
>> 1 4 2 3
>>
>> is a jolly jumper, because the absolutes differences are 3, 2, and 1
>> respectively. The definition implies that any sequence of a single
>> integer is a jolly jumper. You are to write a program to determine
>> whether or not each of a number of sequences is a jolly jumper.
>> Input
>>
>> Each line of input contains an integer n <= 3000 followed by n integers
>> representing the sequence.
>>

> OI problem?! Where do you find it?

http://uva.onlinejudge.org/index.ph...d=8&category=30&page=show_problem&problem=979

--
Ben.

Ben Bacarisse, Jan 13, 2010
6. ### Peter NilssonGuest

On Jan 13, 6:30 pm, Albert <> wrote:
> My current code to this problem:
>
> Problem E: Jolly Jumpers
> A sequence of n > 0 integers is called a jolly jumper if
> the absolute values of the difference between successive
> elements take on all the values 1 through n-1. For instance,
>
> 1 4 2 3
>
> is a jolly jumper, because the absolutes differences are 3,
> 2, and 1 respectively. The definition implies that any
> sequence of a single integer is a jolly jumper. You are to
> write a program to determine whether or not each of a number
> of sequences is a jolly jumper.
> Input
>
> Each line of input contains an integer n <= 3000 followed
> by n integers representing the sequence.

Unfortunately, it doesn't say what the range of those integers
are.

> Output
> For each line of input, generate a line of output saying
> "Jolly" or "Not jolly".

....
> #include <stdio.h>
>
> #define MAXN 2999

There may be 3000 integers!

> #define MAXDIFF 2999

MAXDIFF is based on MAXN.

Also note, the difference may be outside that, e.g.

3 2 1 32767

> void read(int n, int a[])
> {
>         int i;
>
>         for (i = 0; i < n; i++)
>                 scanf("%d", a + i);
>
> }
>
> int absdiff(int a, int b)
> {
>         return a>b ? a-b : b-a;

Note that absdiff(0, INT_MIN) can potentially overflow.

> }
>
> int isjolly(int n, int a[])
> {
>         if (n == 1)
>                 return 1;
>         else {
>                 int takenon[MAXDIFF], i;
>                 for (i = 1; i <= n-1; i++)
>                         takenon = 0;

You do this with an initialiser...

int takenon[MAXDIFF] = { 0 }, i;

Though I don't like allocating automatic (local) variables
larger than 256 bytes.

>
>                 for (i = 0; i < n-1; i++)
>                         takenon[absdiff(a, a[i+1])] = 1;

What if the absolute difference is not in the range 0..2999?

>                 for (i = 1; i <= n-1 && takenon; i++)
>                         ;
>                 return i == n ? 1 : 0;
>         }
>
> }
>
> int main()
> {
>         int n, num[MAXN];
>         int i;
>
>         while (scanf("%d", &n)) {

while (scanf("%d", &n) == 1) {

>                 if (n) {
>                         if (isjolly(n, num))
>                                 puts("Jolly");
>                         else
>                                 puts("Not jolly");
>                 }
>         }
>
>         return 0;
>
> }

#include <limits.h>
#include <stdio.h>

#define MAX_N 3000
#define MAX_D (MAX_N - 1)

int diff(int a, int b)
{
int d;

if (INT_MIN + 1 == -INT_MAX) /* 2's complement? */
{
if ( (a == 0 && b == INT_MIN)
|| (a == INT_MIN && b == 0) )
return 0;
}

d = (a < b) ? b - a : a - b;
return (MAX_D < d) ? 0 : d;
}

int main(void)
{
int N, i, jolly;
static int n[MAX_N];
static char d[MAX_D + 1];

while (scanf("%d", &N) == 1 && 1 <= N && N <= MAX_N)
{
for (i = 0; i < N; i++)
if (scanf("%d", &n) != 1)
return 0;

for (i = 0; i < MAX_D + 1; i++)
d = 0;

for (i = 0; i < N - 1; i++)
d[ diff(n, n[i + 1]) ] = 1;

jolly = (d[0] == 0);
for (i = 1; i <= N - 1; i++)
jolly = (jolly && d == 1);

puts(jolly ? "Jolly" : "Not jolly");
}

return 0;
}

--
Peter

Peter Nilsson, Jan 13, 2010
7. ### Ike NaarGuest

In article <>,
Peter Nilsson <> wrote:
>On Jan 13, 6:30 pm, Albert <> wrote:
>> #define MAXN 2999

>
>There may be 3000 integers!

3000 numbers have 2999 differences.

Ike Naar, Jan 13, 2010
8. ### AlbertGuest

Ben Bacarisse wrote:
> Albert <> writes:
> <snip>
>> Problem E: Jolly Jumpers
>> A sequence of n > 0 integers is called a jolly jumper if the absolute
>> values of the difference between successive elements take on all the
>> values 1 through n-1. For instance,
>>
>> 1 4 2 3
>>
>> is a jolly jumper, because the absolutes differences are 3, 2, and 1
>> respectively. The definition implies that any sequence of a single
>> integer is a jolly jumper. You are to write a program to determine
>> whether or not each of a number of sequences is a jolly jumper.
>> Input
>>
>> Each line of input contains an integer n <= 3000 followed by n
>> integers representing the sequence.

> <snip>
>> int main()
>> {
>> int n, num[MAXN];
>> int i;
>>
>> while (scanf("%d", &n)) {
>> if (n) {
>> if (isjolly(n, num))
>> puts("Jolly");
>> else
>> puts("Not jolly");
>> }
>> }
>>
>> return 0;
>> }
>>
>> How do I get my program to detect an empty line of input? That is,
>> where just enter is pressed?

>
> question. The problem statement says that there will be no blank
> lines. What probably need is a test for the end of the input or, more
> helpfully, a test for the input having succeeded. Did this not come
> up before in another such problem?
>
> You will be better off writing:
>
> while (scanf("%d", &n) == 1) {...}
>
> <snip>

Alright: but why after redirecting an input file
0
4 1 4 2 3
5 1 4 2 -1 6
1 1052
to what I believe is a working program do I get a runtime error from
the judge?

My code:
#include <stdio.h>

#define MAXN 2999
#define MAXDIFF 2999

int takenon[MAXDIFF];

{
int i;

for (i = 0; i < n; i++)
scanf("%d", a + i);
}

int absdiff(int a, int b)
{
return a>b ? a-b : b-a;
}

int isjolly(int n, int a[])
{
int i;

for (i = 0; i < n-1; i++)
takenon[absdiff(a, a[i+1])] = 1;
for (i = 1; i <= n-1 && takenon; i++)
;
return i == n ? 1 : 0;
}

int main()
{
int n, num[MAXN];
int i;

while (scanf("%d", &n) == 1) {
if (n <= 0)
puts("Not jolly");
else {
if (isjolly(n, num))
puts("Jolly");
else
puts("Not jolly");
}
}
return 0;
}

Albert, Jan 14, 2010
9. ### Peter NilssonGuest

(Ike Naar) wrote:
> Peter Nilsson  <> wrote:
> > Albert <> wrote:
> > > #define MAXN 2999

> >
> > There may be 3000 integers!

>
> 3000 numbers have 2999 differences.

Which is why the OP had the next line...

> #define MAXDIFF 2999

Which doesn't alter the fact that the OP only
allocates storage for 2999 input integers in
the subsequent code, despite the specs saying
there may be up to 3000 such integers.

--
Peter

Peter Nilsson, Jan 14, 2010
10. ### Ben BacarisseGuest

Albert <> writes:

> Ben Bacarisse wrote:

<snip>
>> You will be better off writing:
>>
>> while (scanf("%d", &n) == 1) {...}
>>
>> <snip>

>
> Alright: but why after redirecting an input file
> 0
> 4 1 4 2 3
> 5 1 4 2 -1 6
> 1 1052
> to what I believe is a working program do I get a runtime error from
> the judge?

I don't think your code misbehaves on this data. I suspect the judge
is providing other data that does cause a problem misbehave. See

BTW:
<snip>
> while (scanf("%d", &n) == 1) {
> if (n <= 0)
> puts("Not jolly");

This special case is not needed.

> else {
> if (isjolly(n, num))
> puts("Jolly");
> else
> puts("Not jolly");
> }
> }
> return 0;

--
Ben.

Ben Bacarisse, Jan 14, 2010
11. ### Ike NaarGuest

In article <>,
Peter Nilsson <> wrote:
> (Ike Naar) wrote:
>> 3000 numbers have 2999 differences.

>
>Which is why the OP had the next line...
>
> > #define MAXDIFF 2999

>
>Which doesn't alter the fact that the OP only
>allocates storage for 2999 input integers in
>the subsequent code, despite the specs saying
>there may be up to 3000 such integers.

You're right; I should have read the OP's code more carefully before posting.

Ike Naar, Jan 14, 2010