473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning the fibonacci string separated by comma

mac
Hi,

I'm trying to write a fibonacci recursive function that will return
the fibonacci string separated by comma. The problem sounds like this:
-------------
Write a recursive function that creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and should return the string. You will not use any extra function.

Do not try to optimize for space or speed. Do not assume any maximum
length for the result string. Also, please don't use global / static
variables.
-----------

The code must be in C. I managed to create a function that returns the
fibonacci value for the specified 'N' as a char*, but I didn't manage
to get the entire string separated by comma.
This is my function:

char* Recursive(int n){
char* a = malloc(n*sizeof (char));
if(n == 0 || n == 1)
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive( n-1)) +
atoi(Recursive( n-2)));
return a;
}
How could I get the entire string?
Thanks in advance for help!

Feb 13 '07 #1
13 3164
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,

I'm trying to write a fibonacci recursive function that will return
the fibonacci string separated by comma. The problem sounds like this:
-------------
Write a recursive function that creates a character string containing
the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) =
F(1) = 1 -, separated by comma. n should be given as an argument to
the program. The recursive function should only take one parameter, n,
and should return the string. You will not use any extra function.

Do not try to optimize for space or speed. Do not assume any maximum
length for the result string. Also, please don't use global / static
variables.
-----------

The code must be in C.
Really? Because this is a C++ newsgroup. comp.lang.c is just down the
hall and will be able to help you better if C++ is not allowed.
I managed to create a function that returns the
fibonacci value for the specified 'N' as a char*, but I didn't manage
to get the entire string separated by comma.
This is my function:

char* Recursive(int n){
char* a = malloc(n*sizeof (char));
if(n == 0 || n == 1)
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive( n-1)) +
atoi(Recursive( n-2)));
return a;
}

How could I get the entire string?
Thanks in advance for help!
There are problems with your program so far. You are seeing the nth
fibonacci number returned, but think about what memory the other
fibonacci numbers are stored in. You have explicit allocation with no
explicit deallocation, so you leak memory. The buffer you allocate
(size n*sizeof(char)) is not big enough if any of the fibonacci
numbers have more than one digit. This depends entirely on the value
of n. In C++, sizeof char is 1 by definition so multiplying n by
sizeof char is redundant. I would be surprised (but I am prepared to
be surprised) if the same is not true in C.

All of that should be fixable, but before looking for help here, can
you confirm which language you are working in? For help in C, ask in
comp.lang.c. They will be better able to help you. If you really are
happy with a C++ solution (for which the first change will be to ditch
char*, malloc and sprintf in favour of std::string) then come back
here and you will get plenty of help with that.

Gavin Deane

Feb 13 '07 #2
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,
Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));
(malloc returns void* not char*).

You should allocate enough space to print
all Fibonacci numbers from n to 0 with
all the commas and formatting required
so if Recursive(4) should return string like
"0,1,1,2" the allocated length should
be 8 chars not 4. It gets more complicated
for numbers with more than one digit.

!don't forget the null terminator!
if(n == 0 || n == 1)
This will stop recursion when n==1 so 0 will
never be printed.
sprintf(a, "%d", n);
else
sprintf(a, "%d", atoi(Recursive( n-1)) +
atoi(Recursive( n-2)));
If n is not 0 you have to sprintf the
result of Recursive(n-1) which is char *
(lookup %s sprintf format specification)
and the n-th Fibonacci number and the
desired format (commas etc...).

if your initial input is 4, Recursive(4) calls
Recursive(4-1) (which returns "0,1,1")
which calls Recursive(4-2) (which returns
"0,1") etc..

Make sure you deallocate memory returned
from recursive call to Recursive (better names
please!)
return a;

}

Feb 13 '07 #3
On 13 Feb, 12:40, "dasjotre" <dasjo...@googl email.comwrote:
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,

Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));

(malloc returns void* not char*).
Whether that line is correct depends on whether the OP is writing C or
C++. In C++ you need to cast the result of malloc. In C, as I
understand it, you must not cast the result of malloc. So if the code
is C, as the OP suggests, I believe it is correct.

<snip>
if(n == 0 || n == 1)

This will stop recursion when n==1 so 0 will
never be printed.
If n>1, the OP's Recursive function calls Recursive(n-1) AND
Recursive(n-2), so unless the recursion logic has dissolved my brain
(a possibility), n == 0 will happen when Recursive(n) for n==2 calls
Recursive(n-2).

<snip>

Gavin Deane

Feb 13 '07 #4
On 13 Feb, 12:54, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 13 Feb, 12:40, "dasjotre" <dasjo...@googl email.comwrote:
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,
Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));
(malloc returns void* not char*).

Whether that line is correct depends on whether the OP is writing C or
C++. In C++ you need to cast the result of malloc. In C, as I
understand it, you must not cast the result of malloc. So if the code
is C, as the OP suggests, I believe it is correct.

<snip>
if(n == 0 || n == 1)
This will stop recursion when n==1 so 0 will
never be printed.

If n>1, the OP's Recursive function calls Recursive(n-1) AND
Recursive(n-2), so unless the recursion logic has dissolved my brain
(a possibility), n == 0 will happen when Recursive(n) for n==2 calls
Recursive(n-2).

<snip>
Yes, but the OP's sollution is completely wrong. My hinted
sollution calls Recursive recursively only once per Recursive
(my head hurts) so n will be 0 only if Recursive is initially
called with 0.

Regards.

Feb 13 '07 #5
On 13 Feb, 12:54, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 13 Feb, 12:40, "dasjotre" <dasjo...@googl email.comwrote:
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,
Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));
(malloc returns void* not char*).

Whether that line is correct depends on whether the OP is writing C or
C++. In C++ you need to cast the result of malloc. In C, as I
understand it, you must not cast the result of malloc. So if the code
is C, as the OP suggests, I believe it is correct.

<snip>
if(n == 0 || n == 1)
This will stop recursion when n==1 so 0 will
never be printed.

If n>1, the OP's Recursive function calls Recursive(n-1) AND
Recursive(n-2), so unless the recursion logic has dissolved my brain
(a possibility), n == 0 will happen when Recursive(n) for n==2 calls
Recursive(n-2).

<snip>
Yes, but the OP's sollution is completely wrong. My hinted
sollution calls Recursive recursively only once per Recursive
(my head hurts) so n will be 0 only if Recursive is initially
called with 0.

Regards.

Feb 13 '07 #6
On 13 Feb, 12:54, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 13 Feb, 12:40, "dasjotre" <dasjo...@googl email.comwrote:
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,
Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));
(malloc returns void* not char*).

Whether that line is correct depends on whether the OP is writing C or
C++. In C++ you need to cast the result of malloc. In C, as I
understand it, you must not cast the result of malloc. So if the code
is C, as the OP suggests, I believe it is correct.

<snip>
if(n == 0 || n == 1)
This will stop recursion when n==1 so 0 will
never be printed.

If n>1, the OP's Recursive function calls Recursive(n-1) AND
Recursive(n-2), so unless the recursion logic has dissolved my brain
(a possibility), n == 0 will happen when Recursive(n) for n==2 calls
Recursive(n-2).

<snip>
Yes, but the OP's sollution is completely wrong. My hinted
sollution calls Recursive recursively only once per Recursive
(my head hurts) so n will be 0 only if Recursive is initially
called with 0.

Regards.

Feb 13 '07 #7
On 13 Feb, 12:54, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 13 Feb, 12:40, "dasjotre" <dasjo...@googl email.comwrote:
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,
Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));
(malloc returns void* not char*).

Whether that line is correct depends on whether the OP is writing C or
C++. In C++ you need to cast the result of malloc. In C, as I
understand it, you must not cast the result of malloc. So if the code
is C, as the OP suggests, I believe it is correct.

<snip>
if(n == 0 || n == 1)
This will stop recursion when n==1 so 0 will
never be printed.

If n>1, the OP's Recursive function calls Recursive(n-1) AND
Recursive(n-2), so unless the recursion logic has dissolved my brain
(a possibility), n == 0 will happen when Recursive(n) for n==2 calls
Recursive(n-2).

<snip>
Yes, but the OP's sollution is completely wrong. My hinted
sollution calls Recursive recursively only once per Recursive
(my head hurts) so n will be 0 only if Recursive is initially
called with 0.

Regards.

Feb 13 '07 #8
On 13 Feb, 12:54, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 13 Feb, 12:40, "dasjotre" <dasjo...@googl email.comwrote:
On 13 Feb, 11:51, "mac" <andrei.croito. ..@gmail.comwro te:
Hi,
Hi, this is off-topic (not C++) and obviously a coursework
so I will give you hints only
char* Recursive(int n){
char* a = malloc(n*sizeof (char));
(malloc returns void* not char*).

Whether that line is correct depends on whether the OP is writing C or
C++. In C++ you need to cast the result of malloc. In C, as I
understand it, you must not cast the result of malloc. So if the code
is C, as the OP suggests, I believe it is correct.

<snip>
if(n == 0 || n == 1)
This will stop recursion when n==1 so 0 will
never be printed.

If n>1, the OP's Recursive function calls Recursive(n-1) AND
Recursive(n-2), so unless the recursion logic has dissolved my brain
(a possibility), n == 0 will happen when Recursive(n) for n==2 calls
Recursive(n-2).

<snip>
Yes, but the OP's sollution is completely wrong. My hinted
sollution calls Recursive recursively only once per Recursive
(my head hurts) so n will be 0 only if Recursive is initially
called with 0.

Regards.

Feb 13 '07 #9
On 13 Feb, 13:26, "dasjotre" <dasjo...@googl email.comwrote:
<>
sorry, my reader has gone beserk.

Feb 13 '07 #10

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

Similar topics

3
2187
by: Piet | last post by:
Hello, I have a very strange problem with regular expressions. The problem consists of analyzing the properties of columns of a MySQL database. When I request the column type, I get back a string with the following composition: vartype(width|list) further variable attributes. vartype is a simple string(varchar, tinyint ...) which might be followed by a string in curved brackets. This bracketed string is either composed of a single...
5
3710
by: dnsstaiger | last post by:
I try to accomplish the following: I have two tables which are connected via a third table (N:N relationship): Table 1 "Locations" LocationID (Primary Key) Table 2 "Specialists" SpecialistID (Primary Key)
1
33909
by: idog | last post by:
If I start off with a string which contains comma separated ints. I want to move this into an int array so i can do sorting. I thought I might get this to work: int test = {textBox1.Text}; What would be the best way to accomplish this? This is what i came up with:
5
3235
by: Ann Marinas | last post by:
Happy New Year to all! :D I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with. My problem is this... I am encountering a string that has the example below: a, b, c. "d,e,f,g", abcdef
2
9286
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable called fileIds that is oftype varchar. I am putting in a comma separated string with ids (such as:'9B176B0C-CA03-49C9-A2E7-063038E7CF20','9B176B0C-CA03-49C9-A2E7-063038E7CF22','9B176B0C-CA03-49C9-A2E7-063038E7CF23') However, when I execute the sp via...
14
4934
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b; std::cout << a/b << std::endl;
17
2818
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
6
4971
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
2
357
by: shapper | last post by:
Hello, I have a class, Item, with 2 properties: "ID" of type Int "Product" of type String I create a variable which is a Generic.List(Of Item):
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.