Can anyone help me with this code

I

insirawali

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
 
R

Ron Ford

(e-mail address removed) said:


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.
 
I

insirawali

(e-mail address removed) said:



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...
 
S

sh.vipin

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.
 
B

Ben Bacarisse

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?
 
J

James Kuyper

Sensei 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.
 
A

Andrew Poelstra

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.
 
A

aburry

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
 
B

Ben Bacarisse

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!
 
A

aburry

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!

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
 
A

Antoninus Twink

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();
 
P

Peter Nilsson

Andrew said:
               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?
<snip>

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

Antoninus Twink said:
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];
 
V

vippstar

Why avoid it?
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.
 
C

CBFalconer

.... 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.
 
J

James Kuyper

CBFalconer said:
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.
 
V

vippstar

(e-mail address removed) wrote:

... snip ...


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.
 
H

Harald van Dijk

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.
 
J

James Kuyper

Harald said:
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.
 
P

Peter Nilsson

pete said:
Peter said:
  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. :-]

... 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.h> for the
sake of a byte or two.
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top