473,406 Members | 2,745 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,406 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 1528
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philip Mette | last post by:
I am in a crunch and need to covert this Oracle statement to MSSQL. Is there any Oracle/MSSQL experts out there that can help me? I do not understand the syntax enough to modify this. Thanks so...
3
by: Cindy Liu | last post by:
Hi Everyone, In .Net we have XmlSerializer to convert c# class to xml, or vice versa. Do we have any APIs to convert c# class to XSD? Thanks in advance, Cindy
13
by: Ray Z | last post by:
So far, I get the idea that if I want to use both the unmanaged and managed memory, I can not avoid memory copy. But I DO need to avoid it. I get a idea that maybe I could use "union" to convert...
3
by: ad | last post by:
I use a fileUpload of ASP.NET to read a csv file from client into a Strem like: Stream myStream = FileUpload1.FileContent; But the file from Client if of CharSet BIG5. How can I covert it to...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
21
by: Wisdo | last post by:
Hi All, char to char ** is compile error. why? if i hava a string array. yes. it's not safe and it's better to use vector<stringinstead. but my point is the language feature. char...
1
by: Biji | last post by:
Help to Covert MS Acess Data Base tO SQL Server Database
1
by: cvijaykrishna | last post by:
hi i already designed a web based application in VS-2005 and i have a problem now that is how to covert it into ajax based wesite i have ajax frame work installed on my system pls sugesst me any...
6
by: =?Utf-8?B?dmluY2VudA==?= | last post by:
Is there any method to covert a pdf file into jpg file in c#? Many thanks for your replying.
1
by: jonny | last post by:
Option Explicit Private mdTarget1 As Date Private mdTarget2 As Date ' called on form load Public Sub SetRecordStartTime() mdTarget1 = DateAdd("n", 60, Time) mdTarget2 = DateAdd("n", 120,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.