Question

Hi! I am struggling trying to figure out how to do this. For this lab, you...

Hi! I am struggling trying to figure out how to do this.

For this lab, you must have two implementations of calculating the Fibonacci sequence. You are required to calculate it utilizing an array and then with a pointer. You must use the array provided and you must malloc space for your pointer. Your algorithms must be in-place, meaning they do not use a temporary variable to calculate the next sequence number. Lastly, you must insert a comment above each function describing if it is passed by reference or by value and why you believe it to be so.

For this lab, I also want to know what was difficult and what was easy. Functional Requirements:

-Your program must print from 0 to 89.

-You must have two functions, one function must take in an array as its parameter, the other must take in a pointer.

Non-Functional Requirements:

-Your algorithms to calculate the Fibonacci sequence must remain in-place, which means you must calculate the series without using a temp value.

-Your functions should be well documented, explaining what you are doing.

-You must have a header comment with your name, lab number and section number, email address, and a brief description of what the program does. starter code:

#include

void fib1(int a[]);

void fib2(int* a);

int main()

{

int arr[2];

int *pointer;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
void fib1(int a[])
{
a[0]=0;
a[1]=1;
int i=2;
while(a[i-1]<=89)
{
a[i]=a[i-1]+a[i-2];
i+=1;
}
i=0;
//This function print the elements of an array
printf("With array:-");
while(a[i]<=89)
{
printf("%d ",a[i]);
i+=1;
}
}
void fib2(int *a)
{
*a=0;
*(a+1)=1;
int i=2;
while(*(a+i)<=89)
{
*(a+i)=*(a+i-1)+*(a+i-2);
i+=1;
}
i=0;
//This function print the pointer array
printf("With pointer:-");
while(*(a+i)<=89)
{
printf("%d ",*(a+i));
i+=1;
}
}
int main()
{
int arr[20];
int *pointer=(int *)malloc(20*sizeof(int));
//Pass by reference because array is always pass its base address
fib1(arr);
printf("\n");
//Pass by Reference because pointer is pass its address
fib2(pointer);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Hi! I am struggling trying to figure out how to do this. For this lab, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here's an assignment that'll give you some practice with pointers. All you have to do is...

    Here's an assignment that'll give you some practice with pointers. All you have to do is write a program that allows the user to populate an array of doubles (5 values) by calling the function InitArray. After the user has entered all the values, then call the function DispRevArray to display the array in reverse. Main will create and allocate space for an array of type double for 5 elements. Then you will create and allocate a pointer that will...

  • I am struggling with a program in C++. it involves using a struct and a class...

    I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors. Instructions A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per...

  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

    please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay ...

    Hi i will give you a thumbs up if you do this problem correctly. Sorting Analysis Code and Essay Due: 4/22/2019(Monday) Introduction And now for something completely different.   Different sorting algorithms are better for different size data sets.   Other sorting algorithms are better for data sets of a specific type – for instance, data that is already ordered. In this assignment you will implement four different sorting algorithms and collect statistics for each of those algorithms while sorting multiple different...

  • Please help with this function i'm having trouble with: must be written in c++ All of...

    Please help with this function i'm having trouble with: must be written in c++ All of the functions you must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning. Your implementations must not use any global variables whose values may be changed during execution. Your program must build successfully under both Visual C++ and either clang++ or g++. Your program must not use...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless...

    Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless it asking for. /* Write a program designed to get ages and heights from the user, then find the average age, average height, and average age/height ratio. */ #include <stdlib.h> #include <stdio.h> #define MAXNUM 50 typedef struct person { int age; double height; } Person; int getData(Person people[], int max) /* Get the data from the user and put it in an array of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT