In: Computer Science
/* Test#1: Please type 0 for the Fibonacci sequence Please type 1 for the Fibonacci square matrix Please type 2 for the flipped Fibonacci square matrix based on Y-axis Please type 3 for the flipped Fibonacci square matrix based on X-axis 0 Please enter size for the Fibonacci sequence (size <100) 10 0 1 1 2 3 5 8 13 21 34 Median of the Fibonacci sequence is 4.000000 Test#2: Please type 0 for the Fibonacci sequence Please type 1 for the Fibonacci square matrix Please type 2 for the flipped Fibonacci square matrix based on Y-axis Please type 3 for the flipped Fibonacci square matrix based on X-axis 1 Please enter length of edge for the Fibonacci matrix (size <10) 4 The matrix is 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Test#3: Please type 0 for the Fibonacci sequence Please type 1 for the Fibonacci square matrix Please type 2 for the flipped Fibonacci square matrix based on Y-axis Please type 3 for the flipped Fibonacci square matrix based on X-axis 2 Please enter length of edge for the Fibonacci matrix (size <10) 4 The matrix is 2 1 1 0 13 8 5 3 89 55 34 21 610 377 233 144 Test#4: Please type 0 for the Fibonacci sequence Please type 1 for the Fibonacci square matrix Please type 2 for the flipped Fibonacci square matrix based on Y-axis Please type 3 for the flipped Fibonacci square matrix based on X-axis 3 Please enter length of edge for the Fibonacci matrix (size <10) 4 The matrix is 144 233 377 610 21 34 55 89 3 5 8 13 0 1 1 2 Of course, your program cannot just satisfy above four test cases. It should be generalized to satisfy any Fibonacci sequence (size <100) and Fibonacci Matrix (< 10x10). */ #include <stdio.h> //@Hint: put the function you need here int main() { int a[100]; //For Fibonacci sequence int fibonacciMatrix[10][10]; //For Fibonacci Matrix int num = 0; float median = 0.0; //Median number of the Fibonacci sequence int choice = 0; int lengthFbSequence = 0; //the length of Fibonacci sequence int lengthFbMatrix = 0; //the length of the edge of Fibonacci Matrix printf("Please type 0 for the Fibonacci sequence \n"); printf("Please type 1 for the Fibonacci square matrix \n"); printf("Please type 2 for the flipped Fibonacci square matrix based on Y-axis \n"); printf("Please type 3 for the flipped Fibonacci square matrix based on X-axis \n"); scanf("%d", &choice); while (choice < 0) { printf("Please type 0 for the Fibonacci sequence \n"); printf("Please type 1 for the Fibonacci matrix \n"); printf("Please type 2 for the flipped Fibonacci square matrix based on Y-axis \n"); printf("Please type 3 for the flipped Fibonacci square matrix based on X-axis \n"); scanf("%d", &choice); } if (choice == 0) { printf("Please enter size for the Fibonacci sequence (size <100) \n"); scanf("%d", &lengthFbSequence); while (lengthFbSequence < 0) { printf("Please enter size for the Fibonacci sequence (size <100) \n"); scanf("%d", &lengthFbSequence); } //@Hint: Please start to code for option 0 } if (choice == 1 || choice == 2 || choice == 3) { printf("Please enter length of edge for the Fibonacci matrix (size <10) \n"); scanf("%d", &lengthFbMatrix); while (lengthFbMatrix < 0) { printf("Please enter length of edge for the Fibonacci matrix (size <10) \n"); scanf("%d", &lengthFbMatrix); } //@Hint: Please start to code for option 1, 2, 3 } return 0; }
/* C program for fibonacci matrix and sequence */
/* include necessary header files */
#include <stdio.h>
/* Driver method */
int main() {
/* declare variable(s) */
int a[100]; //For Fibonacci sequence
int fibonacciMatrix[10][10]; //For Fibonacci Matrix
int num = 0;
float median = 0.0; //Median number of the Fibonacci sequence
int choice = 0;
int lengthFbSequence = 0; //the length of Fibonacci sequence
int lengthFbMatrix = 0; //the length of the edge of Fibonacci Matrix
int k, i, j;
/* Take input(s) */
do {
printf("Please type 0 for the Fibonacci sequence \n");
printf("Please type 1 for the Fibonacci square matrix \n");
printf("Please type 2 for the flipped Fibonacci square matrix based on Y-axis \n");
printf("Please type 3 for the flipped Fibonacci square matrix based on X-axis \n");
scanf("%d", & choice);
} while (choice < 0 || choice > 3);
/* do operations as per choice */
if (choice == 0) {
do {
printf("Please enter size for the Fibonacci sequence (size <100) \n");
scanf("%d", & lengthFbSequence);
} while (lengthFbSequence <= 0 || lengthFbSequence >= 100);
//@Hint: Please start to code for option 0
/* initialize */
a[0] = 0;
a[1] = 1;
/* generate fibonacci */
for (i = 2; i < lengthFbSequence; i++)
a[i] = a[i - 1] + a[i - 2];
/* display array */
for (i = 0; i < lengthFbSequence; i++)
printf("%d ", a[i]);
/* find median */
if (lengthFbSequence % 2 == 1)
median = a[lengthFbSequence / 2];
else
median = (float)(a[lengthFbSequence / 2] + a[(lengthFbSequence / 2) - 1]) / 2;
printf("\nMedian of the Fibonacci sequence is: %f", median);
}
if (choice == 1 || choice == 2 || choice == 3) {
do {
printf("Please enter length of edge for the Fibonacci matrix (size <10) \n");
scanf("%d", & lengthFbMatrix);
} while (lengthFbMatrix >= 10 || lengthFbMatrix <= 0);
//@Hint: Please start to code for option 1, 2, 3
/* initialize */
a[0] = 0;
a[1] = 1;
/* generate fibonacci */
for (i = 2; i < lengthFbMatrix * lengthFbMatrix; i++)
a[i] = a[i - 1] + a[i - 2];
/* store inside fibonacciMatrix */
k = 0;
for (i = 0; i < lengthFbMatrix; i++)
for (j = 0; j < lengthFbMatrix; j++)
fibonacciMatrix[i][j] = a[k++];
/* display fibonacciMatrix */
if (choice == 1) {
printf("The matrix is:\n");
for (i = 0; i < lengthFbMatrix; i++) {
for (j = 0; j < lengthFbMatrix; j++)
printf("%d ", fibonacciMatrix[i][j]);
printf("\n");
}
} else if (choice == 2) {
printf("The matrix is:\n");
for (i = 0; i < lengthFbMatrix; i++) {
for (j = lengthFbMatrix - 1; j >= 0; j--)
printf("%d ", fibonacciMatrix[i][j]);
printf("\n");
}
} else if (choice == 3) {
printf("The matrix is:\n");
for (i = lengthFbMatrix - 1; i >= 0; i--) {
for (j = 0; j < lengthFbMatrix; j++)
printf("%d ", fibonacciMatrix[i][j]);
printf("\n");
}
}
}
return 0;
}
___________________________________________________________________
___________________________________________________________________
Please type 0 for the Fibonacci sequence
Please type 1 for the Fibonacci square matrix
Please type 2 for the flipped Fibonacci square matrix based on Y-axis
Please type 3 for the flipped Fibonacci square matrix based on X-axis
4
Please type 0 for the Fibonacci sequence
Please type 1 for the Fibonacci square matrix
Please type 2 for the flipped Fibonacci square matrix based on Y-axis
Please type 3 for the flipped Fibonacci square matrix based on X-axis
3
Please enter length of edge for the Fibonacci matrix (size <10)
5
The matrix is:
6765 10946 17711 28657 46368
610 987 1597 2584 4181
55 89 144 233 377
5 8 13 21 34
0 1 1 2 3
___________________________________________________________________
Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.