A simple 'C' problem

A

Aravindh

A 'C' program that takes two numbers and produces two other numbers.
None of the four numbers must be similar.

printf ("%d%d", & num1, & num2) ;
if (num1 > num2) {
num1 ++ ; num2 -- ;
}
else {
num1 --; num2 ++ ;
}
printf ("%d%d\n", num1, num2) ;

Any better ideas ?
 
E

Eric Sosman

Aravindh wrote On 03/02/06 13:04,:
A 'C' program that takes two numbers and produces two other numbers.
None of the four numbers must be similar.

printf ("%d%d", & num1, & num2) ;
if (num1 > num2) {
num1 ++ ; num2 -- ;
}
else {
num1 --; num2 ++ ;
}
printf ("%d%d\n", num1, num2) ;

Any better ideas ?

Almost any idea would be better. "Jellybean pizza"
would be better.

(If you want suggestions about a program, post a
program and not a couple randomly-chosen lines of out-
of-context code. Even in the little you've provided
I can see two guaranteed mistakes; who knows how many
the complete program holds?)
 
M

Micah Cowan

Aravindh said:
A 'C' program that takes two numbers and produces two other numbers.
None of the four numbers must be similar.

printf ("%d%d", & num1, & num2) ;
if (num1 > num2) {
num1 ++ ; num2 -- ;
}
else {
num1 --; num2 ++ ;
}
printf ("%d%d\n", num1, num2) ;

Any better ideas ?

The above code is not compileable. Please post complete examples.

I think you meant to use scanf() rather than printf() for the first
one.

Your state problem is *very* vague... the solutions are infinite. You
could even ignore the input numbers and produce the results of rand()
or something.
 
R

Richard Heathfield

Micah Cowan said:
Your state problem is *very* vague... the solutions are infinite. You
could even ignore the input numbers and produce the results of rand()
or something.

No, he can't (if my understanding of the problem is correct), in case the
result of rand() produces one or other (or even both) of the two input
numbers.

This should do it:

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

void silly(unsigned int m, unsigned int n)
{
unsigned int x = UINT_MAX - m;
unsigned int y = UINT_MAX - n;
if(m == n) { error("input error - numbers are duplicates"); }

/* x is definitely different from m (UINT_MAX is odd) */
/* y is definitely different from n (UINT_MAX is odd) */
/* m is definitely different from n (checked already) */
/* x is definitely different from y (because m != n) */
/* m might equal y, in which case x will equal n */

if(m == y)
{
m += 2; /* still != x, no longer == y, might be == n */
n += 2; /* still != y, no longer == x, no longer == n */
}

printf("%u %u %u %u\n", m, n, x, y);
}
 
V

Vladimir S. Oka

Aravindh said:
A 'C' program that takes two numbers and produces two other numbers.
None of the four numbers must be similar.

Could you define "similar"? Or is my non-native English failing me?
printf ("%d%d", & num1, & num2) ;

You either meant `scanf`, or you really have no clue...
if (num1 > num2) {
num1 ++ ; num2 -- ;
}
else {
num1 --; num2 ++ ;
}
printf ("%d%d\n", num1, num2) ;

Any better ideas ?

Define "better". Unless you mean "correct, compilable code"?

Heed other comments given elsethread as well.

--
BR, Vladimir

Marriage is not merely sharing the fettucine, but sharing the
burden of finding the fettucine restaurant in the first place.
-- Calvin Trillin
 
M

Micah Cowan

Richard Heathfield said:
Micah Cowan said:



No, he can't (if my understanding of the problem is correct), in case the
result of rand() produces one or other (or even both) of the two input
numbers.

This should do it:

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

void silly(unsigned int m, unsigned int n)
{
unsigned int x = UINT_MAX - m;
unsigned int y = UINT_MAX - n;
if(m == n) { error("input error - numbers are duplicates"); }

/* x is definitely different from m (UINT_MAX is odd) */
/* y is definitely different from n (UINT_MAX is odd) */
/* m is definitely different from n (checked already) */
/* x is definitely different from y (because m != n) */
/* m might equal y, in which case x will equal n */

if(m == y)
{
m += 2; /* still != x, no longer == y, might be == n */
n += 2; /* still != y, no longer == x, no longer == n */
}

You've forgotten that you're now changing the inputs; you ought to
modify x and y instead... also, I'm assuming that last comment was
meant to be ... no longer == m ?

if (x == n)
{
x += 2; /* still != m, no longer == n, might be == y */
y += 2; /* still != n, no longer == m, no longer == x */
}

However, I don't think you need the second one, as the "might be == y"
s/b false. Because, x will only equal n if x != y modulo 2, which
means that also m != n modulo 2, and therefore adding 2 to x will
never let it be equal to y for the same reason that it's still not
equal to m.

I could be having a brain-fart, but ATM I think that's right.
 
R

Richard Heathfield

Micah Cowan said:
You've forgotten that you're now changing the inputs; you ought to
modify x and y instead...

Oops, oh yeah...
also, I'm assuming that last comment was
meant to be ... no longer == m ?
Probably.

I could be having a brain-fart, but ATM I think that's right.

I find myself in 100% agreement with you. ;-)

(btw: wb, ltns, hand)
 

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

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top