Covert C# to C

  • Thread starter Thread starter insirawali
  • Start date Start date
I

insirawali

Can someone please convert this C# code to C

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

}
else
{
Console.WriteLine(s); //This can be put as a Printf i
think...
}
}
 
Scott said:
Why? Since "C" is not a .NET language, why do you want to convert to
it?

Why not? I guess he has found some C# code and wants to use the
partitioning algorithm in his C program.
 
Cowboy said:
And he wants someone else to do his work for him. :-)

Well perhaps, but manipulating strings, recursively, is not really that
easy, in C, wile it is no problem at all in a language like C#.

--
Rudy Velthuis http://rvelthuis.de

"Now, now my good man, this is no time for making enemies."
-- Voltaire (1694-1778) on his deathbed in response to a priest
asking that he renounce Satan.
 
I disagree that string manipulation is all that hard in C.  There's good  
support in the CRT, including the function sprintf() that can handle all  
of the string formatting functionality used in the original code (though, 
itoa() and strcat() would work just as well).  And the recursive natureof  
the algorithm is a complete red-herring; C can handle the recursion every 
bit as well as C# can.

But regardless, the guy didn't ask "what function do I use for this?" or  
"how should I approach this problem?"  He asked for someone else to do the  
work.  And if it really is as hard to convert to C as you imply, then the  
request for someone else to do his work for him is that much more  
unreasonable.

Pete


1st of all thnkx for ur responses...

Please dont take this the wrong way.....

i'm not trying get my work done by someone else. i tried for 2 days to
write this algorithm using C. to no success..... the handling strings
in within 'partation' function is a big problem... (im not very
comfortable with C... :-( )

So thats y i wrote the algorithm with a language that im comfortable
with (thats C#).. and im need sme help to get the string to work
properly.....

sory 4 da above confusion.

Insira
 
Perhaps you should try a C forum, since that is the language you want the
code in?


I disagree that string manipulation is all that hard in C. There's good
support in the CRT, including the function sprintf() that can handle all
of the string formatting functionality used in the original code (though,
itoa() and strcat() would work just as well). And the recursive nature of
the algorithm is a complete red-herring; C can handle the recursion every
bit as well as C# can.

But regardless, the guy didn't ask "what function do I use for this?" or
"how should I approach this problem?" He asked for someone else to do the
work. And if it really is as hard to convert to C as you imply, then the
request for someone else to do his work for him is that much more
unreasonable.

Pete


1st of all thnkx for ur responses...

Please dont take this the wrong way.....

i'm not trying get my work done by someone else. i tried for 2 days to
write this algorithm using C. to no success..... the handling strings
in within 'partation' function is a big problem... (im not very
comfortable with C... :-( )

So thats y i wrote the algorithm with a language that im comfortable
with (thats C#).. and im need sme help to get the string to work
properly.....

sory 4 da above confusion.

Insira
 
Scott said:
I wouln't think that this would be the place to ask that question.

He has a question about a piece of C# code. I am sure that some of the
people here also know C. So why not?
 
Scott said:
Perhaps you should try a C forum, since that is the language you want
the code in?

Because this is a C# forum, and he has a question about a code snippet
in that language. I'm sure that some here know C as well, and will be
able to help him.
 
Peter said:
No, not really. He apparently already knows C#, and simply used the
C# code as a way of expressing his question.

I am not so sure of that. I assume he found a piece of C# code that
seems to do what he wants and likes to have it translated to C.

ISTM there are two possible kinds of forums he can ask: A C# forum,
where people will know the source language and some will know the
target, and a C forum, where people will know the target language and
some will be able to read the source. IMO, both are appropriate groups
to ask a question concerning both languages.

Of course, if there were a "convert C# to C" forum, that one would be
the most appropriate.
--
Rudy Velthuis http://rvelthuis.de

"I think 'Hail to the Chief' has a nice ring to it."
-- John F. Kennedy (1917-1963) when asked what is his favorite
song
 
Ok, I guess I missed that.
As far as your other rationale...


Well, I respectfully disagree. If the question is about writing some
specific code, then the appropriate newsgroup is the one relevant to
the target language. If the question is about reading some specific
code, then the appropriate newsgroup is the one relevant to the
source language.

I still don't quite see the problem. Even if you think it is not the
right forum, then simply ignore it. Fact is that the question is about
converting from C# to C. I still see no real harm in asking in groups
for both languages.
 
Peter said:
[...]
I still don't quite see the problem. Even if you think it is not the
right forum, then simply ignore it. Fact is that the question is
about converting from C# to C. I still see no real harm in asking
in groups for both languages.

Frankly, it's more for the OP's benefit than anyone else's.

I mean, there's value in keeping a newsgroup on-topic.

Sure, I fully agree. But one can overdo it, too, IMO. This one may be
dubious, but it has some relation to C#, after all.
--
Rudy Velthuis http://rvelthuis.de

"Everywhere I go I'm asked if I think the university stifles
writers. My opinion is that they don't stifle enough of them."
-- Flannery O'Connor (1925-1964)
 
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());

}
else
{
Console.WriteLine(s); //This can be put as a Printf i
think...
}
}

OK, try this:

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

void partation(int n, int m, char *s)
{
int i;
char *p; /* local temporary string */
char num[10]; /* for itoa() - 10 chars should do */

if (n > 0)
for (i = min(n, m); i > 0; i--)
{
itoa(i, num, 10); /* num = i.ToString(); */
p = calloc(strlen(s) + 1 + sizeof(num), sizeof(char));
strcpy(p, s); /* p = s; */
strcat(p, num); /* p = p + num; */

partation(n - i, i, p);

free(p);
}
else
{
printf("%s\n", s);
}
}

Output:

5
41
32
311
221
2111
11111
 
Peter said:
[...]
I mean, there's value in keeping a newsgroup on-topic.

Sure, I fully agree. But one can overdo it, too, IMO. This one may
be dubious, but it has some relation to C#, after all.

Only because the OP stated the algorithm in C#.

Yes. At least he chose C#. <g>
--
Rudy Velthuis http://rvelthuis.de

"There are some experiences in life which should not be demanded
twice from any man, and one of them is listening to the Brahms
Requiem."
-- George Bernard Shaw (1856-1950)
 
I'm pretty sure I've already said why not.

He doesn't have a question about C# code, he knows the C# code, he wants C
code and I'm suggesting he'd have better luck in a C forum.

To use your own logic: surely someone there knows C# and should be able to
help him.
 
Console is a class in the .NET Framework. Is there a console class in C?



Rudy Velthuis said:
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());

}
else
{
Console.WriteLine(s); //This can be put as a Printf i
think...
}
}

OK, try this:

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

void partation(int n, int m, char *s)
{
int i;
char *p; /* local temporary string */
char num[10]; /* for itoa() - 10 chars should do */

if (n > 0)
for (i = min(n, m); i > 0; i--)
{
itoa(i, num, 10); /* num = i.ToString(); */
p = calloc(strlen(s) + 1 + sizeof(num), sizeof(char));
strcpy(p, s); /* p = s; */
strcat(p, num); /* p = p + num; */

partation(n - i, i, p);

free(p);
}
else
{
printf("%s\n", s);
}
}

Output:

5
41
32
311
221
2111
11111

--
Rudy Velthuis http://rvelthuis.de

"The right to swing my fist ends where the other man's nose
begins." -- Oliver Wendell Holmes (1841-1935)
 
Scott said:
I'm pretty sure I've already said why not.

And I have already said why I don't agree. I also have said that I
don't think it makes sense to make a lot of fuss about it.

<shrug>
 
Scott said:
Console is a class in the .NET Framework. Is there a console class
in C?

There are NO classes in C. But printf() writes to the console.

IIRC, consoles already existed before .NET. <g>
 
So why do you keep making a fuss then? I'm simply answering the question
that you asked me. Did you want my response or not?
 
Back
Top