Question

In: Computer Science

/* Test#1: Please type 0 for the Fibonacci sequence Please type 1 for the Fibonacci square...

/*
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;
}

Solutions

Expert Solution

/* 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.


Related Solutions

Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 +...
Fibonacci numbers are defined by F0 = 0, F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈ N ∪ {0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
Identify the type of test (chi-square, t-test, or ANOVA) that we have to use in each...
Identify the type of test (chi-square, t-test, or ANOVA) that we have to use in each of the following scenarios. Explain why you chose that test. We want to compare males and females (gender) based on the difference in time spent on social media (measured as hours per day) We want to compare our customers and the customers of our closest competitors based on their brand loyalty (measured as a Likert scale from 1 to 5) We want to compare...
1) What’s the difference among the chi-square test for goodness of fit, the chi-square test for...
1) What’s the difference among the chi-square test for goodness of fit, the chi-square test for independence, and the chi-square test for homogeneity 2) State the requirements to perform a chi-square test
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a non-recursive function Fibonacci (n) that calculates the nth Fibonacci number. Write a program to display a table of terms and the Fibonacci number in two columns for the first 15 terms, using the function you created.
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally,...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded C++ program that generates the Fibonacci series using the pthread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: Fib(N) = {N, if N = 0 or 1 Fib(N - 2) + Fib(N - 1), if N > 1 a) Write a recursive method fibonacci that returns...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part of a code public class FS { public static void main(String[] args){ IntSequence seq = new FibonacciSequence(); for(int i=0; i<20; i++) { if(seq.hasNext() == false) break; System.out.print(seq.next()+" "); } System.out.println(" "); } } ///Fill in the rest of the code! Output 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 You should define...
Choose a point at random from the unit square [0, 1] × [0, 1]. We also...
Choose a point at random from the unit square [0, 1] × [0, 1]. We also choose the second random point, independent of the first, uniformly on the line segment between (0, 0) and (1, 0). The random variable A is the area of a triangle with its corners at (0, 0) and the two selected points. Find the probability density function (pdf) of A.
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT