Program in C Write a function that takes a single integer pa

Program in C++

Write a function that takes a single, integer parameter n. This function should dynamically allocate an integer array of size n. It should then fill the arrays with the first n numbers in the Fibonacci sequence and return the array pointer. Write a second function that takes such an array pointer, deallocates the memory associated with it, and sets the pointer to nullptr (in such a way that it becomes nullptr in the calling method). For problems 5-6, assume that the linked lists are formed with nodes of the following type: struct Node {int data; Node*next;}; The nodes are not part of a class and you should not assume that any functions already exist that you can use.

Solution

Here are the two functions for you:

#include <iostream>
using namespace std;
int fib(int n)
{
if(n == 1)
return 1;
else if(n == 2)
return 1;
else
return fib(n-1) + fib(n-2);
}
//This function takes a single integer parameter n.
//Dynamically allocate an integer array of size n.
//Fill with first n Fibonacci numbers. Returns the array pointer.
int* allocateAndInitializeArray(int n)
{
int *array = new int[n];
for(int i = 1; i <= n; i++)
array[i] = fib(i);
return array;
}
//This function takes an array pointer, and deallocates memory associated with it.
//Set the pointer to nullptr.
void deallocateArray(int* array)
{
delete array;
array = nullptr;
}

Program in C++ Write a function that takes a single, integer parameter n. This function should dynamically allocate an integer array of size n. It should then f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site