# Fibonacci using C and Bool type

Discussion in 'C' started by brainless, Mar 27, 2009.

1. ### brainlessNew Member

Joined:
Mar 27, 2009
Messages:
1
0
Trophy Points:
0
Hi, can any one help me with this?
i use Quincy 2005
i've made a fibonacci program in C :--->
Code:
```#include <malloc.h>
#include <stdlib.h>

int main()

{

int NUM_FIBON;
int *farray;
int i;

printf("Enter a number \t");
scanf("%d",&NUM_FIBON);

/* Display the purpose of the program */
printf("\nThis program displays the first %d fibonacci numbers\n",NUM_FIBON);

farray = (int *) malloc(NUM_FIBON);

farray[0] = 1;
farray[1] = 1;

for (i = 2; i < NUM_FIBON; i++ )
{
farray[i] =farray[i -1] + farray[i - 2];

}

for (i = 0; i < NUM_FIBON; i++ )
{
printf(" %-8i ",farray[i]);
}

}

```
<----------------

now i have been asked to change the code so that the program
asks repeatedly for a new number of Fibonacci numbers until the user decides to quit. The user's decision to continue or not is taken within a simple user-defined function.
Design this, say, "continueOK" function to take no parameter, but return a boolean value
(bool type). The function should return true when the user types 'Y' or 'y', false when the user enters 'N' or 'n'.
You need to #include <stdbool.h>.
The stdbool.h header defines the type bool, and values true and false.
Within the "continueOK" function, you have to do a case-insensitive check of
whether the character entered by the user is 'Y' or 'N'. You can use the
toupper system function to convert the character to uppercase. For this you
need to #include <ctype.h>.
Use Strict ANSI/ISO Compliance and C99 support for C programs.

I tried using a do - while loop but could not achieve anything..
you wil need to #include <stdbool.h> , #include <ctype.h> , #include <stdio.h>

Last edited by a moderator: Mar 31, 2009
2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Please use code tags when posting code.

How far have you got with this new function?
Have you got the function prototype yet?
Do you know how to define a variable?
Do you know how to output a message?
Do you know how to get data in?
Do you know how to compare that data with the appropriate character constants and set the defined variable accordingly?
Do you know how to return a value?

There's not much more to it than that (if anything) so post what you've got when you're stuck and we can let you know what to do next.

Where did you put the do-while loop and why do you think continueOK() needs one?

3. ### alwaysLearningNew Member

Joined:
Mar 27, 2009
Messages:
10
0
Trophy Points:
0
Code:
```#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>

bool doContinue(){
char ch, temp;
printf("Do you want to contiue? Y/n ");

temp = getchar(); //taking care the trailing \n in the input buffer
ch = getchar();

if(ch =='n')
return false;

return true;
}

int main()

{

int NUM_FIBON;
int *farray;
int i;

do{

printf("Enter a number \t");
scanf("%d",&NUM_FIBON);

/* Display the purpose of the program */
printf("\nThis program displays the first %d fibonacci numbers\n",NUM_FIBON);

farray = (int *) malloc(NUM_FIBON);

farray[0] = 1;
farray[1] = 1;

for (i = 2; i < NUM_FIBON; i++ )
{
farray[i] =farray[i -1] + farray[i - 2];

}

for (i = 0; i < NUM_FIBON; i++ )
{
printf(" %-8i ",farray[i]);
}
}while(doContinue());

}```
this is an example, change the way you like..

Shaikat

Last edited by a moderator: Mar 31, 2009

Joined:
Aug 6, 2004
Messages:
3,012