What is asked of me is:
You will write a C file with the main function with additional functions described below. Your Program will start calling part1 and part 2 in that order. For each part, you will receive the inputs from the user and print the output to the console. Details of the parts are further discussed below. Please pay attention to the output format. Any deviation from the shared format may be penalized regardless of the correct execution. Write a function that will read one integer from the command prompt as term number of Fibonacci Sequence. If the input number is not positive integer value then your program will print the message explaining the reason for ineligibility. Your function will continue until it gets the correct input. Print the Fibonacci Sequence elements as many as the number of input. Let the function continue working until it gets the ‘*’ input.
Here, when you enter a positive integer into the program, it should show the elements of the Fibonacci number sequence. If it enters a string , float , or negative number I should give a warning but the program should continue until * a is pressed.
I did something for this program but I am struggling keeping the program running.
here is my code:
void calculate_fibonacci_sequence()
{
int eleman;
printf("Enter how many elements of the fibonacci sequence you want to see : ");
scanf("%d", & eleman);
if (eleman <= 0) {
while (eleman <= 0) {
printf("Please enter positive term(s) number: a \n");
scanf("%d", & eleman);
if (eleman > 0) {
break;
}
}
}
if (eleman > 0) {
printf("\n\n");
int i, n, e1 = 1, e2 = 1;
for (i = 1; i <= eleman; i ) {
printf(" %d\n", e1);
n = e1 e2;
e1 = e2;
e2 = n;
}
}
}
CodePudding user response:
It's better to break your code into separate functioning pieces. Start off by writing a function that reads a line of input and then parses it:
int read_int(long *number)
{
char tmp[16];
if (!fgets(tmp, sizeof tmp, stdin))
return RETCODE_FAIL;
if (!strcmp(tmp, "*\n"))
return RETCODE_END;
char *end;
*number = strtol(tmp, &end, 10);
if (errno == ERANGE)
return RETCODE_RANGE;
if (end == tmp)
return RETCODE_EMPTY;
if (*end && *end != '\n')
return RETCODE_WARN;
return RETCODE_SUCCESS;
}
fgets()
reads an entire line from a given file (in this case, stdin
, i.e. the keyboard by default) with that little extra '\n'
. it returns NULL
if it fails, so it's a good idea to test its return value.
strtol()
parses a string to an long int
. The second argument gives more information about the parsing process, so it's used to check for errors.
Return codes are grouped in an enum
like this:
enum RETCODE {
RETCODE_FAIL = 0,
RETCODE_END,
RETCODE_RANGE,
RETCODE_EMPTY,
RETCODE_WARN,
RETCODE_SUCCESS
};
Next, write your Fibonacci algorithm:
void print_fibonacci(long n)
{
long e0, e1 = 0, e2 = 1;
for (long i = 0; i < n; i ) {
e0 = e1;
e1 = e2;
e2 = e0 e1;
printf("%ld ", e2);
}
printf("\n");
}
Now, your main()
will be very straightforward:
int main(void)
{
for (;;) {
printf("Enter a number: ");
long number = -1;
switch (read_int(&number)) {
case RETCODE_FAIL:
puts("Internal error");
break;
case RETCODE_END:
puts("Goodbye!");
return 0;
case RETCODE_RANGE:
puts("Number is either too small or too big");
break;
case RETCODE_EMPTY:
puts("<empty>");
break;
case RETCODE_WARN:
puts("Invalid integer format");
break;
case RETCODE_SUCCESS:
number < 0 ? puts("Number must be positive") : print_fibonacci(number);
break;
default:
// This should never run
break;
}
}
}
Credits (& nice to look at as well): A Beginners' Guide Away From scanf()
CodePudding user response:
Updating your code!!!
You are using many if else conditions again and again that is making the code terrible. I just optimized it by do while loop.
Point is that you are performing iterations in your for loop after prinitng massage so, it retains the value 1 for second iteration. Just print the massage after iteration...
Here is revised version of your code.........
#include <stdio.h>
void calculate_fibonacci_sequence()
{
int eleman;
do
{
printf("Enter how many elements of the fibonacci sequence you want to see : ");
scanf("%d", &eleman);
} while (eleman < 0);
printf("\n");
int i, n, e1 = 1, e2 = 1;
for (i = 1; i <= eleman; i )
{
n = e1 e2;
e1 = e2;
e2 = n;
printf("%d\n", e1);
}
}
int main()
{
calculate_fibonacci_sequence();
return 0;
}