Question

In: Computer Science

use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...

use c++

1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers.

b) For the second part of this assignment, you must modify the Fib(n) you wrote for part a) so that it uses the array you fill in part a, to compute Fib (n+1) using Fib(n) and Fib(n-1) already saved in the array. This way we do not compute the same Fib numbers many times.

(Don't provide the same answer which is already posted. It should be correct and post the image of your output)

Solutions

Expert Solution

Code is Given Below:

========================

#include <iostream>

using namespace std;
//defining function
long int fib(int n)
{
//checking if n is less than equal to 1
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main()
{
//creating array of size 100
long int fibSeries[100];
//calling fib 100 times and storing result in array
for(int i=0;i<100;i++){
fibSeries[i]=fib(i+3);
}
//printing result
for(int i=0;i<100;i++){
cout<<fibSeries[i]<<endl;
}
return 0;
}

Code Part b)

================

#include <iostream>

using namespace std;
//defining function
long int fib(long int *f,int n)
{
//checking if n==3
if(n==3){
return 2;
}
//checking if n==4
if(n==4){
return 3;
}
//checking if n==5
if(n==5){
return 5;
}
return f[n-4]+f[n-5];
}
int main()
{
//creating array of size 100
long int fibSeries[100];
//calling fib 100 times and storing result in array
for(int i=0;i<100;i++){
long int temp = fib(fibSeries,(i+3));
fibSeries[i]=temp;

}
//printing result
for(int i=0;i<100;i++){
cout<<fibSeries[i]<<endl;
}
return 0;
}

Output:

===========

Code Snapshot:

==================


Related Solutions

use cpp 1 a)Write a console program which creates an array of size 100 integers. Then...
use cpp 1 a)Write a console program which creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }.  For this part of assignment you should first write a recursive Fib(n) funcion, as was done in class.For testing, print out the 100 integers. 1 b) For second part of this assignment,...
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT