473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Can anyone help me with this code

I have written this in C#.

I just need to convert it into C.

da problem comes when handling the String. In C the String doesn't
behave the same way as in C#.

it would be great help if smeone cn help me out to make the string
work on the same way as in C# (for this code.).
private void Caller()
{
Int32 a = 5;
partation(a, a, "");
}

private void partation(Int32 n, Int32 m, String s)
{
Int32 i;
if(n>0)
{
for (i = Math.Min(n, m); i 0; i--)
partation(n - i, i, s + i.ToString()); // the
problem comes here.....

}
else
{
Console.WriteLine(s); //This can be put as a Printf
}
}
I tried using sprintf, strcat, but no use, it prints some garbage
values (may be im not using them correctly). pls pls help me..........
Insira
Sep 17 '08 #1
33 1719
On 2008-09-17 07:43:41 +0200, in********@gmail.com said:
I have written this in C#.

I just need to convert it into C. [...]
Use int (or less likely long) for Int32, char* for String, and the str*
functions to handle strings. And by the way, C has no classes: get rid
of private.

Since you come from C#/Java/..., you may need some time to figure out
the "right conversion".

One of your challenges will be (and is right now since you mention
"garbage") learning that memory isn't something "magic": you have to
allocate (malloc and similia) and free it. Anyway you must NOT be
afraid of this: in other languages *before* using a class like String
you need to use "new", and I see no real difference between malloc and
new (apart from some sugar).

And don't be afraid to post idiotic code here, I learned a lot even
from stupid sources.

Cheers!
--

Sensei*<Sensei's e-mail is at Mac-dot-com>

It is a very sad thing that nowadays there is so little useless information.
(Oscar Wilde)

Sep 17 '08 #2
in********@gmail.com said:
I have written this in C#.

I just need to convert it into C.
Instead of showing C# code, why not describe, in English, the problem you
are trying to solve, and show us your best shot at a C solution?

Once we know what you're trying to do (in English) and how you're trying to
do it (in C), we have a fighting chance of being able to help you achieve
your objective.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 17 '08 #3
On Wed, 17 Sep 2008 07:01:46 +0000, Richard Heathfield posted:
in********@gmail.com said:
>I have written this in C#.

I just need to convert it into C.

Instead of showing C# code, why not describe, in English, the problem you
are trying to solve, and show us your best shot at a C solution?

Once we know what you're trying to do (in English) and how you're trying to
do it (in C), we have a fighting chance of being able to help you achieve
your objective.
I don't think english helps much when you see:

private void partation(Int32 n, Int32 m, String s)

I think the next mutation will be C flat. I'm frequently wrong, though.
--
A prohibitionist is the sort of man one couldn't care to drink with, even
if he drank.
H. L. Mencken
Sep 17 '08 #4
On Sep 17, 12:01*pm, Richard Heathfield <r...@see.sig.invalidwrote:
insiraw...@gmail.com said:
I have written this in C#.
I just need to convert it into C.

Instead of showing C# code, why not describe, in English, the problem you
are trying to solve, and show us your best shot at a C solution?

Once we know what you're trying to do (in English) and how you're trying to
do it (in C), we have a fighting chance of being able to help you achieve
your objective.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Im trying to do the Partation (number theory) to print the results.

this is the closest got using C.

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

void partation(int n, int m, char str[]);
int main()
{
int no;
printf("Enter a positive integer: ");
scanf("%d",&no);

if(no < 0)
{
printf("%d is not a positive integer.\n",no);
printf("Please enter a positive integer and retry...\n");
return -1;
}

char result[10]="";
partation(no,no,result);
return 0;
}

void partation(int n, int m, char str[])
{
int i;
if(n<m)
i=n;
else
i=m;

if(n>0)
{
for(;i>0;i--)
{
char s[2];
sprintf(s,"%i",i);
strcat(str,s);
partation(n-i,i,str);
}
}
else
{
printf("%s\n",str);
sprintf(str,"");
}
}
In this code when i input the value 4, i get the result as
4
31
22
11
1111

but the result should be
4
31
22
211
1111

P.S.: Thnkx for ur replies...
Sep 17 '08 #5
On Sep 17, 1:44 pm, insiraw...@gmail.com wrote:
Im trying to do the Partation (number theory) to print the results.
it will be good if you can explain what Partation (number theory)
is ??
if we know the steps we need to implement, we may find the bug, if
any, in your code.

--
vIpIn
Sep 17 '08 #6
On Sep 17, 12:18 pm, sh.vi...@gmail.com wrote:
On Sep 17, 1:44 pm, insiraw...@gmail.com wrote:
Im trying to do the Partation (number theory) to print the results.

it will be good if you can explain what Partation (number theory)
is ??
It will be better if you search before you ask someone what something
is.
Luckily, google is typo-friendly:
<http://google.com/search?q=partation+number+theory>
Sep 17 '08 #7
in********@gmail.com writes:
I have written this in C#.

I just need to convert it into C.

da problem comes when handling the String. In C the String doesn't
behave the same way as in C#.
No, but I think you have a (small) advantage here. String is not a
good type for this problem even in C# so you can kill two birds with
one stone: re-write in C and do it better!

I'd use an array of ints.
partation(n - i, i, s + i.ToString());
Have you tried this with 10 or, indeed, any number >= 10?

--
Ben.
Sep 17 '08 #8
Sensei wrote:
On 2008-09-17 07:43:41 +0200, in********@gmail.com said:
>I have written this in C#.

I just need to convert it into C. [...]

Use int (or less likely long) for Int32,
Int32 is (probably) a typedef; whatever it's a typedef for probably can
be used in C, too, so I don't see any reason to change it. If he does
change it, I'd recommend int_fast32_t, int_least32_t, or int32_t,
depending upon precisely why the current code uses Int32.

Int32 could also be a user-defined type, in which case he's got a LOT
more work to do, and he should provide us with the definition of that type.
Sep 17 '08 #9
On 2008-09-17, in********@gmail.com <in********@gmail.comwrote:
>
Im trying to do the Partation (number theory) to print the results.
Do you mean "partition"? In any case it is still not totally
clear what you are trying to achieve. I don't think it's too
important from a C perspective, though.
this is the closest got using C.

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

void partation(int n, int m, char str[]);
int main()
{
int no;
printf("Enter a positive integer: ");
scanf("%d",&no);
scanf() returns the number of matches - so you can check to see
if it returns 1 (meaning it got the integer you requested) or 0
(meaning it failed), and add that to your if statement below.
>
if(no < 0)
{
printf("%d is not a positive integer.\n",no);
printf("Please enter a positive integer and retry...\n");
return -1;
I can't think of a platform running C# where this isn't fine,
but since the rest of your program is standard vanilla C, I
think you should return EXIT_FAILURE instead. That way, your
program (and future C programs) can be ported to anything
anywhere without worrying that your program's return value
will be interpreted as electrocute-the-operator or anything
else.

(In mathematics, you may find that your programs are being
compiled for supercomputers and the like with return code
conventions you haven't thought of. This advice isn't just
me being silly.)
}

char result[10]="";
You aren't allocating enough space here. I'm not 100% sure
what your algorithm does so I'm not sure how much you actually
need. See my note later on.
partation(no,no,result);
return 0;
}

void partation(int n, int m, char str[])
{
int i;
if(n<m)
i=n;
else
i=m;
There's a shortcut here that exists in C# as well, which
is IMHO much easier to read:

int i = (n < m) ? n : m;

Now I can see clearly you are selecting the minimum of
the two. Before it was too spread out.
if(n>0)
{
for(;i>0;i--)
{
char s[2];
Here's your biggest problem: in C, all strings end in
0. Therefore, your "string" array only has room for
one character plus that 0.

Declare it instead as
char s[64]
Or whatever you think the widest integer that you will
encounter is.
sprintf(s,"%i",i);
%i isn't the right specifier for int - use %d instead.
strcat(str,s);
partation(n-i,i,str);
}
}
else
{
printf("%s\n",str);
sprintf(str,"");
}
}
In this code when i input the value 4, i get the result as
4
31
22
11
1111

but the result should be
4
31
22
211
1111

P.S.: Thnkx for ur replies...

--
Andrew Poelstra ap*******@wpsoftware.net
That was a joke. Jokes in mathematics, are sometimes not funny.
-Veselin Jungic
Sep 17 '08 #10
The problem with your translation fron C# to C is that in the original
version the "String c" argument was passed by value, passing a char*
is similar to passing by reference. You need to manage the "pass by
value" part somehow. Something like this, although I'm sure you can do
better:

void partition(int n, int m, char* str) {

if (0<n) {
int i = min(n,m);
for (;0<i;--i) {
char acc[100];
strcpy(acc,str);
char s[2];
sprintf(s,"%i",i);
strcat(acc,s);
partition(n-i,i,acc);
}
return;
}
printf("%s\n",str);
}

Good luck with the rest of your assignment.

Adam
Sep 17 '08 #11
ab****@ieee.org writes:
The problem with your translation fron C# to C is that in the original
version the "String c" argument was passed by value, passing a char*
is similar to passing by reference. You need to manage the "pass by
value" part somehow.
Not really. If I understand the algorithm the strings behave like a
stack. Yes, they are passed by copying in C# but that is not an
essential requirement of the algorithm. I think in C one could just
keep a local note of the where it ends -- no need to copy the whole
thing.

But, I repeat, (to the OP) why use a string at all? It is the wrong
data type!

--
Ben.
Sep 17 '08 #12
On Sep 17, 12:14*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
abu...@ieee.org writes:
The problem with your translation fron C# to C is that in the original
version the "String c" argument was passed by value, passing a char*
is similar to passing by reference. You need to manage the "pass by
value" part somehow.

Not really. *If I understand the algorithm the strings behave like a
stack. *Yes, they are passed by copying in C# but that is not an
essential requirement of the algorithm. *I think in C one could just
keep a local note of the where it ends -- no need to copy the whole
thing.

But, I repeat, (to the OP) why use a string at all? *It is the wrong
data type!

--
Ben.
Agreed, it is essentially a stack. Agreed, it could be done with a
pointer to the "end" of the string (top of stack). Agreed, a stack of
ints would make sense.

Adam
Sep 17 '08 #13
On 17 Sep 2008 at 13:39, Andrew Poelstra wrote:
On 2008-09-17, in********@gmail.com <in********@gmail.comwrote:
> for(;i>0;i--)
{
char s[2];

Here's your biggest problem: in C, all strings end in
0. Therefore, your "string" array only has room for
one character plus that 0.

Declare it instead as
char s[64]
Or whatever you think the widest integer that you will
encounter is.
> sprintf(s,"%i",i);

%i isn't the right specifier for int - use %d instead.
> strcat(str,s);
Or avoid using a fixed-length buffer:

s=malloc(snprintf(NULL, 0, "%d%s", i, str)+1);
if(s) {
sprintf(s, "%d%s", i, str);
partation(n-i, i, s);
free(s);
} else
handle_error();

Sep 17 '08 #14
Andrew Poelstra wrote:
insiraw...@gmail.com <insiraw...@gmail.comwrote:
* * * * * * * *for(;i>0;i--)
* * * * * * * *{
* * * * * * * * * * * *char s[2];
Here's your biggest problem: in C, all strings end in
0. Therefore, your "string" array only has room for
one character plus that 0.

Declare it instead as
* char s[64]
Or whatever you think the widest integer that you will
encounter is.
* * * * * * * * * * * *sprintf(s,"%i",i);
%i isn't the right specifier for int
Why not?
- use %d instead.
<snip>

Whilst %d is more common, %i has identical semantics. The
difference is in scanf, not printf.

Antoninus Twink <nos...@nospam.invalidwrote:
Or avoid using a fixed-length buffer:
Why avoid it?
s=malloc(snprintf(NULL, 0, "%d%s", i, str)+1);
char s[(sizeof(int) * CHAR_BIT + 2) / 3 + 2];

--
Peter
Sep 17 '08 #15
Peter Nilsson wrote:
>Andrew Poelstra wrote:
>>insiraw...@gmail.com <insiraw...@gmail.comwrote:
for(;i>0;i--)
{
char s[2];
Here's your biggest problem: in C, all strings end in
0. Therefore, your "string" array only has room for
one character plus that 0.

Declare it instead as
char s[64]
Or whatever you think the widest integer that you will
encounter is.

sprintf(s,"%i",i);
%i isn't the right specifier for int

Why not?
>>- use %d instead.
<snip>

Whilst %d is more common, %i has identical semantics. The
difference is in scanf, not printf.

Antoninus Twink <nos...@nospam.invalidwrote:
>Or avoid using a fixed-length buffer:

Why avoid it?
>s=malloc(snprintf(NULL, 0, "%d%s", i, str)+1);

char s[(sizeof(int) * CHAR_BIT + 2) / 3 + 2];
I use [(sizeof(int) * CHAR_BIT + 1) / 3 + 2] for int,
and [(sizeof(int) * CHAR_BIT + 2) / 3 + 1] for unsigned.

--
pete
Sep 17 '08 #16
On 2008-09-17 14:15:34 +0200, James Kuyper <ja*********@verizon.netsaid:
Sensei wrote:
>On 2008-09-17 07:43:41 +0200, in********@gmail.com said:
>>I have written this in C#.

I just need to convert it into C. [...]

Use int (or less likely long) for Int32,

Int32 is (probably) a typedef; whatever it's a typedef for probably can
be used in C, too, so I don't see any reason to change it. If he does
change it, I'd recommend int_fast32_t, int_least32_t, or int32_t,
depending upon precisely why the current code uses Int32.

Int32 could also be a user-defined type, in which case he's got a LOT
more work to do, and he should provide us with the definition of that
type.
Typedefs should be the other way around, i.e., "int" is an alias for a
specific platform: the standard should not mention how many bits a type
must have (*).

My point was on standard though, "int" is ok, "BYTE" or "long_int32_t" aren't.

(*) there are lower limits, afair.

--

Sensei*<Sensei's e-mail is at Mac-dot-com>

It is a very sad thing that nowadays there is so little useless information.
(Oscar Wilde)

Sep 18 '08 #17
On Sep 18, 1:55 am, pete <pfil...@mindspring.comwrote:
Peter Nilsson wrote:
Antoninus Twink <nos...@nospam.invalidwrote:
Or avoid using a fixed-length buffer:
Why avoid it?
s=malloc(snprintf(NULL, 0, "%d%s", i, str)+1);
char s[(sizeof(int) * CHAR_BIT + 2) / 3 + 2];

I use [(sizeof(int) * CHAR_BIT + 1) / 3 + 2] for int,
and [(sizeof(int) * CHAR_BIT + 2) / 3 + 1] for unsigned.
I think it would be more ideal and correct if INT_MAX was somehow used
instead of sizeof to calculate the required characters for the decimal
representation of a value, since int might have padding bits.

At first, my solution was this, (DON'T USE THIS, it doesn't work)

#define <limits.h>
#define XSTR(x) #x
#define STR(x) XSTR(x)

char s[sizeof STR(INT_MAX) + 1]; /* +1 for the sign +- */

However this has some issues: INT_MAX can be (2 * somenumber) for
example.
In my implementation, INT_MIN it's defined as (-INT_MAX - 1).
I guess in an implementation without any padding bits in int and sign-
and-magnitude representation, INT_MIN may be defined as (~0), which
would allocate only 5 bytes (-1 for the zero byte). 4 bytes are not
sufficient even for 32767.
So that solution is a bad one (and not really a solution).

I'm sure there is a way given a number N to find out how many digits
it has in decimal representation, but I don't know of any.
Can anyone help?

Also, are there any problems I have not thought of? Because it seems
absurd to me that people post solutions with sizeof instead of the
_MAX constants.
Sep 19 '08 #18
vi******@gmail.com wrote:
>
.... snip ...
>
However this has some issues: INT_MAX can be (2 * somenumber) for
example.
No, to satisfy other provisions in the C standard INT_MAX must be
of the form 2**n - 1, where ** means exponentiation. Similarly the
other _MAXs.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 19 '08 #19
CBFalconer wrote:
vi******@gmail.com wrote:
... snip ...
>However this has some issues: INT_MAX can be (2 * somenumber) for
example.

No, to satisfy other provisions in the C standard INT_MAX must be
of the form 2**n - 1, where ** means exponentiation. Similarly the
other _MAXs.
The standard explicitly says precisely that for the unsigned types
(6.2.6.2p1). This implies that the fact that it does not say so for
signed types means that it's not a requirement for signed types.

It has been argued that what the standard says about the value bits in
(6.2.6.2p2) implies such a requirement, even though it's not
specifically stated. However, the standard also allows for the
possibility that some bit patterns can be trap representations (6.2.6.1p5).

It's been argued that sign and value bits cannot play a role in
determining whether or not a given representation is a trap
representation, but the standard explicitly allows for one particular
case where the only difference between a trap representation and a
normal one is determined by the value and sign bits (6.2.6.2.p2). For
one's complement and sign/magnitude types, this case is called _negative
zero_.

It has been argued that the one bit pattern mentioned in 6.2.6.2p2 is
the only one that an implementation is allowed to treat as a trap
representation. However, there's no wording in the standard which says
so. Special mention was made of negative zero, because it's the only
case where the rules would otherwise require two different bit patterns
to represent the same integer; not because it's the only permitted trap
representation.

The standard says nothing to prohibit a implementation from providing,
for example, an 'int' type containing 1 padding bit, 1 sign bit, and 30
value bits, where every bit pattern that would otherwise represent a
value outside the range [-1000000000,1000000000] is considered to be a
trap representation.
Sep 19 '08 #20
On Sep 19, 4:44 pm, CBFalconer <cbfalco...@yahoo.comwrote:
vipps...@gmail.com wrote:

... snip ...
However this has some issues: INT_MAX can be (2 * somenumber) for
example.

No, to satisfy other provisions in the C standard INT_MAX must be
of the form 2**n - 1, where ** means exponentiation. Similarly the
other _MAXs.
The standard requires INT_MAX to expand to some integer constant,
correct?
It could be (2 * somenumber + 1) then.
Assuming 16-bit int, INT_MAX could be (2 * 16383 + 1), and my STR()
trick wouldn't work correctly. (that's why I mentioned it)
I think you didn't take the time to read all of my message.
Sep 19 '08 #21
On Fri, 19 Sep 2008 15:22:21 +0000, James Kuyper wrote:
the standard explicitly allows for one particular
case where the only difference between a trap representation and a
normal one is determined by the value and sign bits (6.2.6.2.p2). For
one's complement and sign/magnitude types, this case is called _negative
zero_.
Nit: it's only called negative zero when it's not a trap representation.
Sep 19 '08 #22
Harald van Dijk wrote:
On Fri, 19 Sep 2008 15:22:21 +0000, James Kuyper wrote:
>the standard explicitly allows for one particular
case where the only difference between a trap representation and a
normal one is determined by the value and sign bits (6.2.6.2.p2). For
one's complement and sign/magnitude types, this case is called _negative
zero_.

Nit: it's only called negative zero when it's not a trap representation.
Agreed. Also, it's a different bit pattern for 1's complement types than
it is for sign/magnitude types; but for any one choice of representation
of signed integers, it's a single bit pattern (modulo padding bits). All
of which makes it very hard to talk about.
Sep 19 '08 #23
James Kuyper wrote, On 19/09/08 19:33:
Harald van Dijk wrote:
>On Fri, 19 Sep 2008 15:22:21 +0000, James Kuyper wrote:
>>the standard explicitly allows for one particular
case where the only difference between a trap representation and a
normal one is determined by the value and sign bits (6.2.6.2.p2). For
one's complement and sign/magnitude types, this case is called _negative
zero_.

Nit: it's only called negative zero when it's not a trap representation.

Agreed. Also, it's a different bit pattern for 1's complement types than
it is for sign/magnitude types; but for any one choice of representation
of signed integers, it's a single bit pattern (modulo padding bits). All
of which makes it very hard to talk about.
Also note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation. I don't know of any systems
that do this, but it is allowed.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 19 '08 #24
vipps...@gmail.com wrote:
pete <pfil...@mindspring.comwrote:
Peter Nilsson wrote:
* char s[(sizeof(int) * CHAR_BIT + 2) / 3 + 2];
I use [(sizeof(int) * CHAR_BIT + 1) / 3 + 2] for int,
and * [(sizeof(int) * CHAR_BIT + 2) / 3 + 1] for unsigned.

I think it would be more ideal and correct if INT_MAX was
somehow used instead of sizeof to calculate the required
characters for the decimal representation of a value, since
int might have padding bits.
As is, the formula doesn't calculate the number of decimal
digits, it takes the simpler option of determining octal
digits. So it's already producing a too many bytes (or two)
for large integers. But too much is better than too little;
and the formula is easy to derive.

Assuming unpadded integers, it is possible to reduce the
wastage (to 1 byte in rare and unlikely cases) since you
can approximate log10(2) fairly accurately with integer
arithmetic alone. [Ask anyone who's worked with original
FORTH. :-]

<snip>
... it seems absurd to me that people post solutions
with sizeof instead of the _MAX constants.
All you need to do is calcualte log10(INT_MAX) as a
constant expression in C90. Good luck with that.

With C99, variable length arrays might be the go, if
you could be bothered resorting to <math.hfor the
sake of a byte or two.

--
Peter
Sep 21 '08 #25
Flash Gordon <s...@spam.causeway.comwrote:
... note that on 2's complement systems the bit pattern
with all bits set is allowed to be a trap representation.
Not unless there are padding bits.
I don't know of any systems that do this, but it is
allowed.
Are you really thinking of a sign bit of 1 and all value
bits zero? That is allowed to be a trap representation
in two's complement, under C99.

--
Peter
Sep 23 '08 #26
Peter Nilsson wrote:
Flash Gordon <s...@spam.causeway.comwrote:
>... note that on 2's complement systems the bit pattern
with all bits set is allowed to be a trap representation.

Not unless there are padding bits.
No padding bits are required.
In any complement system,
(-INT_MAX) can be equal to INT_MIN.

--
pete
Sep 23 '08 #27
On Tue, 23 Sep 2008 07:41:43 -0400, pete wrote:
Peter Nilsson wrote:
>Flash Gordon <s...@spam.causeway.comwrote:
>>... note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation.

Not unless there are padding bits.

No padding bits are required.
Padding bits are required.
In any complement system,
(-INT_MAX) can be equal to INT_MIN.
Yes, but in two's complement, the trap representation that could have been
-INT_MAX-1 isn't the bit pattern with all bits set. This is what Peter
Nilsson pointed out in the rest of his message, which you snipped. In
two's complement, the bit pattern with all bits set is -1, which must be
representable.
Sep 23 '08 #28
Peter Nilsson wrote, On 23/09/08 03:50:
Flash Gordon <s...@spam.causeway.comwrote:
>... note that on 2's complement systems the bit pattern
with all bits set is allowed to be a trap representation.

Not unless there are padding bits.
>I don't know of any systems that do this, but it is
allowed.

Are you really thinking of a sign bit of 1 and all value
bits zero? That is allowed to be a trap representation
in two's complement, under C99.
Yes, I was thinking of the situation where INT_MIN == -INT_MAX, I just
wasn't thinking hard enough about it.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 23 '08 #29
Harald van Dijk wrote:
On Tue, 23 Sep 2008 07:41:43 -0400, pete wrote:
>Peter Nilsson wrote:
>>Flash Gordon <s...@spam.causeway.comwrote:
... note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation.
Not unless there are padding bits.
No padding bits are required.

Padding bits are required.
>In any complement system,
(-INT_MAX) can be equal to INT_MIN.

Yes, but in two's complement, the trap representation that could have been
-INT_MAX-1 isn't the bit pattern with all bits set. This is what Peter
Nilsson pointed out in the rest of his message, which you snipped. In
two's complement, the bit pattern with all bits set is -1, which must be
representable.
Thank you;
and my apologies to Peter Nilsson.

--
pete
Sep 23 '08 #30
On Sep 17, 1:44*am, insiraw...@gmail.com wrote:
On Sep 17, 12:01*pm, Richard Heathfield <r...@see.sig.invalidwrote:


insiraw...@gmail.com said:
I have written this in C#.
I just need to convert it into C.
Instead of showing C#code, why not describe, in English, the problem you
are trying to solve, and show us your best shot at a C solution?
Once we know what you're trying to do (in English) and how you're trying to
do it (in C), we have a fighting chance of being able tohelpyou achieve
your objective.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Im trying to do the Partation (number theory) to print the results.

this is the closest got using C.

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

void partation(int n, int m, char str[]);
int main()
{
* * * * int no;
* * * * printf("Enter a positive integer: ");
* * * * scanf("%d",&no);

* * * * if(no < 0)
* * * * {
* * * * * * * * printf("%d is not a positive integer.\n",no);
* * * * * * * * printf("Please enter a positive integer and retry...\n");
* * * * * * * * return -1;
* * * * }

* * * * char result[10]="";
* * * * partation(no,no,result);
* * * * return 0;

}

void partation(int n, int m, char str[])
{
* * * * int i;
* * * * if(n<m)
* * * * * * * * i=n;
* * * * else
* * * * * * * * i=m;

* * * * if(n>0)
* * * * {
* * * * * * * * for(;i>0;i--)
* * * * * * * * {
* * * * * * * * * * * * char s[2];
* * * * * * * * * * * * sprintf(s,"%i",i);
* * * * * * * * * * * * strcat(str,s);
* * * * * * * * * * * * partation(n-i,i,str);
* * * * * * * * }
* * * * }
* * * * else
* * * * {
* * * * * * * * printf("%s\n",str);
* * * * * * * * sprintf(str,"");
* * * * }

}

In thiscodewhen i input the value 4, i get the result as
4
31
22
11
1111

but the result should be
4
31
22
211
1111

P.S.: Thnkx for ur replies...

/*
Modified from some Hungarian code I found.
*/
#include <stdio.h>
#include <assert.h>

#define MAX 100

int t[MAX];

void integer_partition(const int n, const int e)
{
assert((e - 1) >= 0);
assert(e < MAX);

if (n <= t[e - 1]) {
int s,
i;

t[e] = n;

printf("%i=", t[0]);
for (i = 1, s = 0; s + t[i] < t[0]; s += t[i++]) {
printf("%i+", t[i]);
assert(i + 1 < MAX);
}
printf("%i\n", t[i]);
} else {
t[e] = t[e - 1] + 1;
}

while (--t[e] 0) {
integer_partition(n - t[e], e + 1);
}
}
int main(int argc, char *argv[])
{
int n = 4;

if (argc 1) {
n = atoi(argv[1]);
}
assert(n < MAX);
t[0] = n;
integer_partition(n, 1);
return 0;
}

/*
From:
"PROGRAMMING CHALLENGES
The Programming Contest Training Manual"
by Steven S. Skiena Miguel A. Revilla

Definition for how to compute them:
Integer Partitions - An integer partition of n is an unordered set of
positive integers which add up to n. For example, there are seven
partitions
of 5, namely, (5), (4, 1), (3, 2), (3, 1, 1), (2, 2, 1), (2, 1, 1, 1),
and
(1, 1, 1, 1, 1). The easiest way to count them is to define a function
f(n,
k) giving the number of integer partitions of n with largest part at
most k.
In any acceptable partition the largest part either does or does not
reach
with limit, so f(n, k) = f(n-k, k)+f(n, k-1). The basis cases are f(1,
1) =
1 and f(n, k) = 0 whenever k n.
*/
Sep 25 '08 #31
On Fri, 19 Sep 2008 20:08:35 +0100, Flash Gordon
<sm**@spam.causeway.comwrote:
James Kuyper wrote, On 19/09/08 19:33:
Harald van D?k wrote:
On Fri, 19 Sep 2008 15:22:21 +0000, James Kuyper wrote:
the standard explicitly allows for one particular
case where the only difference between a trap representation and a
normal one is determined by the value and sign bits (6.2.6.2.p2). For
one's complement and sign/magnitude types, this case is called _negative
zero_.

Nit: it's only called negative zero when it's not a trap representation.
Agreed. Also, it's a different bit pattern for 1's complement types than
it is for sign/magnitude types; but for any one choice of representation
of signed integers, it's a single bit pattern (modulo padding bits). All
of which makes it very hard to talk about.

Also note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation. I don't know of any systems
that do this, but it is allowed.
YM 1sC, where as already said it can be negative zero or a trap rep.
In 2sC, all bits set must be negative one.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Oct 1 '08 #32
David Thompson <da************@verizon.netwrites:
On Fri, 19 Sep 2008 20:08:35 +0100, Flash Gordon
<sm**@spam.causeway.comwrote:
>James Kuyper wrote, On 19/09/08 19:33:
Harald van D?k wrote:
On Fri, 19 Sep 2008 15:22:21 +0000, James Kuyper wrote:
the standard explicitly allows for one particular
case where the only difference between a trap representation and a
normal one is determined by the value and sign bits (6.2.6.2.p2). For
one's complement and sign/magnitude types, this case is called _negative
zero_.

Nit: it's only called negative zero when it's not a trap representation.

Agreed. Also, it's a different bit pattern for 1's complement types than
it is for sign/magnitude types; but for any one choice of representation
of signed integers, it's a single bit pattern (modulo padding bits). All
of which makes it very hard to talk about.

Also note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation. I don't know of any systems
that do this, but it is allowed.

YM 1sC, where as already said it can be negative zero or a trap rep.
In 2sC, all bits set must be negative one.
Yes, but there is still a small bit of information missing:

2sC systems /are/ permitted a trap representation without padding
bits: when the sign bit is one and the values bits are all zero, just
like sign+magnitude systems. This is probably what Flash Gordon is
(mis-)remembering.

--
Ben.
Oct 1 '08 #33
Ben Bacarisse wrote, On 01/10/08 12:28:
David Thompson <da************@verizon.netwrites:
>On Fri, 19 Sep 2008 20:08:35 +0100, Flash Gordon
<sm**@spam.causeway.comwrote:
>>James Kuyper wrote, On 19/09/08 19:33:
Harald van D?k wrote:
On Fri, 19 Sep 2008 15:22:21 +0000, James Kuyper wrote:
>the standard explicitly allows for one particular
>case where the only difference between a trap representation and a
>normal one is determined by the value and sign bits (6.2.6.2.p2). For
>one's complement and sign/magnitude types, this case is called _negative
>zero_.
Nit: it's only called negative zero when it's not a trap representation.
Agreed. Also, it's a different bit pattern for 1's complement types than
it is for sign/magnitude types; but for any one choice of representation
of signed integers, it's a single bit pattern (modulo padding bits). All
of which makes it very hard to talk about.
Also note that on 2's complement systems the bit pattern with all bits
set is allowed to be a trap representation. I don't know of any systems
that do this, but it is allowed.
YM 1sC, where as already said it can be negative zero or a trap rep.
In 2sC, all bits set must be negative one.
My error was corrected ages ago and I acknowledged the correction and
clarified what I meant.
Yes, but there is still a small bit of information missing:

2sC systems /are/ permitted a trap representation without padding
bits: when the sign bit is one and the values bits are all zero, just
like sign+magnitude systems. This is probably what Flash Gordon is
(mis-)remembering.
Oh, I remembered it al right, I just failed to say what I intended to say.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 1 '08 #34

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Steele | last post by:
Does anyone have a copy or know where I can get an installation executable for chiliReports 2.0 from chili! soft? The company I'm working for bought a copy several years ago, then moved their...
13
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
1
by: Mike | last post by:
I got the code below from an earlier post but I can't get it to work (I get an error on the "for (i=0; i<a.length; i++)" line) Anyone have code that works for cookies with keys? > Anyone...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
13
by: penguin732901 | last post by:
Checking back for discussions, there was a lot of talk about 2000 being slower than 97, but not so much lately. What is the latest opinion? Anyone care to set up a poll for how many NG members...
4
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
2
by: Kevin Walzer | last post by:
Does anyone use the Tkdnd module that comes with Tkinter to allow drag-and-drop of Tkinter widgets in your application? (Not the binary extension that hooks into Xdnd and OLE-dnd on Windows.) I've...
6
by: placid | last post by:
Hi all, I'm looking for anyone who is working on a project at the moment that needs help (volunteer). The last project i worked on personally was screen-scraping MySpace profiles (read more at...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
1
Eclipse
by: Eclipse | last post by:
G'day all Can anyone explain the difference in the results to me as I don't understand why specifying the directory name in two different ways could give a different answer. In CODE 1 below i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.