An interesting C problem

G

gteddy923

I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
....
n
n-1
....
i

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it's feasible without loosing any points?

Thanks,
 
A

Army1987

I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it's feasible without loosing any points?

Thanks,

#include <stdio.h>
void idiotic_thing(int i, int n);
void idiotic_thing(int i, int n)
{
idiotic_thing1(i, n);
}

void idiotic_thing1(int i, int n)
{
int j;
if (i < n) return;
for (j = i; j <= n; j++)
printf("%d\n", j);
for (j = n-1; j >= i; j++)
printf("%d\n", j);
return;
}

The function idiotic_thing itself uses only one statement and no loop.
 
S

santosh

Army1987 said:
#include <stdio.h>
void idiotic_thing(int i, int n);

ITYM, void idiotic_thing1(int i, int n);
void idiotic_thing(int i, int n)
{
idiotic_thing1(i, n);
}

void idiotic_thing1(int i, int n)
{
int j;
if (i < n) return;

This check squashes the purpose of this routine.
 
R

Richard Heathfield

(e-mail address removed) said:
The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it's feasible without loosing any points?

A loop is an iteration-statement, which is one of the several different
kinds of statement defined by the grammar, so I see no reason why it
should lose any points. When your questioner can produce this:

*
* *
* *
* *
* *
* *
*

in a single bodyless iteration-statement (where the height of the
diamond is configurable at runtime), I'll think about thinking about
his problem. Until then, just tell him he's behind the curve.
 
M

Malcolm McLean

I had a written test of a company the other day, there's an
interesting problem. it's as below:
Write a function with input parameters i and n, then use only one line
of statement to produce the formatted results as below:
i
i+1
...
n
n-1
...
i

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.
The problem is 10 points, if you use one loop, 4 points will be
minused, if you use one other statement, 2 points will be minused.

Can anybody figure out if it's feasible without loosing any points?
Think recursion.
 
L

Lauri Alanko

For example, let i = 1, n = 4, then the output should be:
1
2
3
4
3
2
1

The trickest thing is that you should USE ONLY ONE STATEMENT to
accomplish that.

Here are the main ingredients:

expression statement
comma operator
conditional expression
function call expression
the standard function 'printf'

If you aren't familiar with some of them, look them up. Then figure
out how to combine them to get the desired result.


Lauri
 
A

Army1987

Lauri Alanko said:
Here are the main ingredients:

expression statement
comma operator
conditional expression
function call expression
the standard function 'printf'

If you aren't familiar with some of them, look them up. Then figure
out how to combine them to get the desired result.

Also, think about the behaviour of && and || when the second operand has
side effects.
 
B

Bill Pursell

There is more than one way to do that. I used && and ||, but used no ternary
operator.


But did you check the return value of printf and handle
errors gracefully?
 
A

army1987

But did you check the return value of printf and handle
errors gracefully?

Ehm... no, but if that matters, you can include the right headers, set
errno to 0, call the function, and then check errno...

Anyway, I never used printf as the left operator of &&, if you meant
*that*. In the function I wrote, it is always used as the left
"operand" of a comma.
 
C

CBFalconer

Barry said:
No && or || are required.

Neither is the comma operator nor printf (for n < 10). putc will
suffice.

void doit(int n) {
if (n) {
putc(n + '0'); putc('\n');
doit(n - 1);
if (n > 1) {
putc(n + '0'); putc('\n');
}
}
}
 
K

Keith Thompson

Ehm... no, but if that matters, you can include the right headers, set
errno to 0, call the function, and then check errno...
[...]

Or, better yet, you can check the value returned by printf. The standard
doesn't say that printf set error on errors.
 
K

Keith Thompson

Keith Thompson said:
Ehm... no, but if that matters, you can include the right headers, set
errno to 0, call the function, and then check errno...
[...]

Or, better yet, you can check the value returned by printf. The standard
doesn't say that printf set error on errors.

Whoops, I meant that the standard doesn't say that printf sets *errno*
on errors.
 
G

gteddy923

Here are the main ingredients:

expression statement
comma operator
conditional expression
function call expression
the standard function 'printf'

If you aren't familiar with some of them, look them up. Then figure
out how to combine them to get the desired result.

Lauri

Thanks, i think i've figured out how to do that.
int func(int i, int n)
{

return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}

The func works as required :)
 
L

Laurent Deniau

Thanks, i think i've figured out how to do that.
int func(int i, int n)
{

return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}

I guess that 'p' must be read as 'func'.
The func works as required :)

But according to the norm, this is not a statement (6.8) but a
function-definition (6.9.1). While

for (int i = 1; i < 2*n; i++)
printf("%d\n", i < n ? i : 2*n-i);

is a statement. And there is many other ways to write it with a single
statement.

a+, ld.
 
L

Laurent Deniau

Laurent said:
I guess that 'p' must be read as 'func'.


But according to the norm, this is not a statement (6.8) but a
function-definition (6.9.1). While

for (int i = 1; i < 2*n; i++)
printf("%d\n", i < n ? i : 2*n-i);

Sorry, I hadn't read the first post where you say that 'i' may not
necessary start at 1. So here a better version:

for (int j = 2*n-i; i <= j; i++)
printf("%d\n", i < n ? i : 2*n-i);
is a statement. And there is many other ways to write it with a single
statement.

a+, ld.
 
C

Chris Dollin

Sorry, I hadn't read the first post where you say that 'i' may not
necessary start at 1. So here a better version:

for (int j = 2*n-i; i <= j; i++)
printf("%d\n", i < n ? i : 2*n-i);

It's a statement, but it isn't ONE statement: I see two.

--
Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.
"Go not to the Drazi for counsel, Unsaid /Babylon 5/
for they will answer both 'green' and 'purple'."

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
 
B

Barry

Thanks, i think i've figured out how to do that.
int func(int i, int n)
{

return printf("%d\n", i) && i<n && ( p(i + 1, n) || 1) && printf("%d
\n", i);
}

The func works as required :)

When I replied no && or || was required I was thinking:

void doitr(int i, int n)
{
i<n?printf("%d\n",i), doitr(i+1,n), printf("%d\n",i):printf("%d\n",i);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top