471,330 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Covert C# to C

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...
}
}
Sep 15 '08 #1
25 1355
Why? Since "C" is not a .NET language, why do you want to convert to it?
<in********@gmail.comwrote in message
news:ba**********************************@v13g2000 pro.googlegroups.com...
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...
}
}

Sep 15 '08 #2
Scott M. wrote:
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.
--
Rudy Velthuis http://rvelthuis.de

"So I went to the dentist. He said "Say Aaah." I said "Why?"
He said "My dog's died.'" -- Tommy Cooper
Sep 15 '08 #3
And he wants someone else to do his work for him. :-)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn**************@news.microsoft.com...
Scott M. wrote:
>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.
--
Rudy Velthuis http://rvelthuis.de

"So I went to the dentist. He said "Say Aaah." I said "Why?"
He said "My dog's died.'" -- Tommy Cooper
Sep 15 '08 #4
Cowboy (Gregory A. Beamer) wrote:
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.
Sep 15 '08 #5
On Mon, 15 Sep 2008 16:26:22 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
Cowboy (Gregory A. Beamer) wrote:
>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#.
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
Sep 16 '08 #6
On Sep 16, 4:56*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 15 Sep 2008 16:26:22 -0700, Rudy Velthuis *

<newsgro...@rvelthuis.dewrote:
Cowboy (Gregory A. Beamer) wrote:
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#.

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
Sep 16 '08 #7
On Mon, 15 Sep 2008 17:25:12 -0700, <in********@gmail.comwrote:
[...]
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... :-( )
Frankly, given that your question is about how to write C code, you should
post in a newsgroup for C programming. Use pseudo-code instead of C# for
broadest understanding of the question.

I already described in my previous posts functions in the CRT that would
be useful for you. Most of the rest of the code is just like C# anyway.
It seems to me that you should have enough information at this point to
get it working, but if you don't, a newsgroup for C programming would be a
much more appropriate place to find your answers.

When you post there, be sure to ask specific questions about what you're
having trouble with, rather than just asking for people to write the code
for you.

Pete
Sep 16 '08 #8
I wouln't think that this would be the place to ask that question.
"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn**************@news.microsoft.com...
Scott M. wrote:
>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.
--
Rudy Velthuis http://rvelthuis.de

"So I went to the dentist. He said "Say Aaah." I said "Why?"
He said "My dog's died.'" -- Tommy Cooper

Sep 16 '08 #9
Perhaps you should try a C forum, since that is the language you want the
code in?
<in********@gmail.comwrote in message
news:17**********************************@1g2000pr e.googlegroups.com...
On Sep 16, 4:56 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 15 Sep 2008 16:26:22 -0700, Rudy Velthuis

<newsgro...@rvelthuis.dewrote:
Cowboy (Gregory A. Beamer) wrote:
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#.

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
Sep 16 '08 #10
Scott M. wrote:
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.

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?

--
Rudy Velthuis http://rvelthuis.de

"When choosing between two evils, I always like to try the one
I've never tried before." -- Mae West (1892-1980)
Sep 17 '08 #11
Scott M. wrote:
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.

--
Rudy Velthuis http://rvelthuis.de

"Having the source code is the difference between buying a house
and renting an apartment." -- Behlendorf
Sep 17 '08 #12
On Wed, 17 Sep 2008 05:12:39 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
He has a question about a piece of C# code.
No, not really. He apparently already knows C#, and simply used the C#
code as a way of expressing his question. He's not asking _about_ the C#
code.
I am sure that some of the people here also know C. So why not?
For the same reason that, even though some people reading a C/C++
newsgroup also know C#, posting a question asking "how do I write C# code
to do this?" in a C/C++ programming newsgroup wouldn't be appropriate.

Pete
Sep 17 '08 #13
Peter Duniho wrote:
On Wed, 17 Sep 2008 05:12:39 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
He has a question about a piece of C# code.

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
Sep 17 '08 #14
On Wed, 17 Sep 2008 15:21:23 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
[...]
>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.
Why? He's already told us he wrote the code himself:

On Mon, 15 Sep 2008 17:25:12 -0700, <in********@gmail.comwrote:
[...]
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.....
As far as your other rationale...
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.
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.

If a person has so little experience that they need help both with
_reading_ some specific dode and _writing_ that code in some other
language, then it's likely they really ought to be starting somewhere
else. Like learning at least one or the other of the languages. This
doesn't appear to be the issue here anyway (the OP states he's comfortable
with C#), but even if it were, it's not an argument for putting questions
pertaining to writing C code in a C# newsgroup.

Pete
Sep 17 '08 #15
Peter Duniho wrote:
On Wed, 17 Sep 2008 15:56:45 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
[...]
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)
Sep 18 '08 #16
in********@gmail.com wrote:
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)
Sep 18 '08 #17
On Wed, 17 Sep 2008 17:03:10 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
[...]
>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#. He could just as easily
have used pseudo-code or some other descriptive technique.

There's _nothing_ about the original question that inherently relates to
C#. Any connection is artificial and arbitrary. More importantly, it's
one thing for the original post to have been made, but given that it was
it still makes sense to encourage the OP to seek a more relevant forum.

Pete
Sep 18 '08 #18
Peter Duniho wrote:
On Wed, 17 Sep 2008 17:03:10 -0700, Rudy Velthuis
<ne********@rvelthuis.dewrote:
[...]
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)
Sep 18 '08 #19
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.

"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn**************@news.microsoft.com...
Scott M. wrote:
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.

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?

--
Rudy Velthuis http://rvelthuis.de

"When choosing between two evils, I always like to try the one
I've never tried before." -- Mae West (1892-1980)

Sep 18 '08 #20
Console is a class in the .NET Framework. Is there a console class in C?

"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn**************@news.microsoft.com...
in********@gmail.com wrote:
> 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)

Sep 18 '08 #21
On Thu, 18 Sep 2008 14:46:08 -0700, Scott M. <s-***@nospam.nospamwrote:
Console is a class in the .NET Framework. Is there a console class in C?
Not in the CRT, no. That's why Rudy's example uses printf() instead (and
it seems from the original post that the OP had already figured that part
out).
Sep 18 '08 #22
Scott M. wrote:
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>

--
Rudy Velthuis http://rvelthuis.de

"I would have made a good Pope." -- Richard Nixon.
Sep 18 '08 #23
Scott M. wrote:
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>
--
Rudy Velthuis http://rvelthuis.de

"I'm not a member of any organized political party, I'm a
Democrat!" -- Will Rogers (1879-1935)
Sep 18 '08 #24
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?
"Rudy Velthuis" <ne********@rvelthuis.dewrote in message
news:xn**************@news.microsoft.com...
Scott M. wrote:
>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>

--
Rudy Velthuis http://rvelthuis.de

"I would have made a good Pope." -- Richard Nixon.

Sep 20 '08 #25
Scott M. wrote:
So why do you keep making a fuss then?
I had already forgotten about it. You make the fuss.
--
Rudy Velthuis http://rvelthuis.de

"Vote early and vote often." -- Al Capone (1899-1947)
Sep 20 '08 #26

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Philip Mette | last post: by
3 posts views Thread by Cindy Liu | last post: by
13 posts views Thread by Ray Z | last post: by
3 posts views Thread by ad | last post: by
6 posts views Thread by Calros Lo | last post: by
6 posts views Thread by =?Utf-8?B?dmluY2VudA==?= | last post: by
reply views Thread by rosydwin | last post: by

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.