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>
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?
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