Question

In: Computer Science

I'm having trouble printing a fibonacci sequence using pointers with malloc's to get this output. now...

I'm having trouble printing a fibonacci sequence using pointers with malloc's to get this output.

now printing with pointers:

0 1 1 2 3 5 8 13 21 34 55 89

Here's my code:

#include <stdio.h>

#include <stdlib.h>

void fib1(int a[]);

void fib2(int* a);



}

int main() {

  int arr[2] = {0,1};

  int *pointer;

  arr[0]=0;

  arr[1]=1;

  fib1(arr);

  

  return 0;

}

void fib1(int a[]){

  printf("printing with arrays:\n");  

  

  for(int i=0; i<6; i++){

    printf("%d %d ", a[0],a[1]);

    a[0] = a[0] + a[1];

    a[1] = a[0] + a[1];

  }

  printf("\n\n");

}

void fib2(int* a){

  printf("printing with pointers:\n");  

  

  for(int i=0; i<6; i++){

    printf("%d %d ", a,a);

    

  }

}

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

void fib1(int a[]);

void fib2(int* a);


int main() {

int arr[2] = {0,1};

int *pointer;

arr[0]=0;

arr[1]=1;

fib1(arr);
//creating array with size 2 using malloc
pointer=(int *)malloc(2*sizeof(int));
// initiliazing the values 0 and 1
pointer[0]=0;

pointer[1]=1;

fib2(pointer);

return 0;

}

void fib1(int a[]){

printf("printing with arrays:\n");

  

for(int i=0; i<6; i++){

printf("%d %d ", a[0],a[1]);

a[0] = a[0] + a[1];

a[1] = a[0] + a[1];

}

printf("\n\n");

}

void fib2(int* a){

printf("printing with pointers:\n");

  

for(int i=0; i<6; i++){

// so here we need to print the 0 th and 1th element
// so we need to use * operator
// to print 1th element we need to add 1 to the pointer
printf("%d %d ", *(a+0),*(a+1));
a[0] = a[0] + a[1];

a[1] = a[0] +a[1];

  

}

}


Related Solutions

I'm having trouble understanding smart pointer functions what is get(), release(), reset(), swap() for unique pointers?...
I'm having trouble understanding smart pointer functions what is get(), release(), reset(), swap() for unique pointers? How do I use it? what is get(), reset(), swap(), unique(), use_count() for shared pointers? How do I use it? what is expired(), lock(), reset(),swap(), use_count for weak pointer? How do I use it?
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I'm having trouble creating a histogram using openCV (color segmentation)
I'm having trouble creating a histogram using openCV (color segmentation)
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
I'm having trouble getting a certain output with this code #include <stdio.h> //function prototypes void initializeArray(int...
I'm having trouble getting a certain output with this code #include <stdio.h> //function prototypes void initializeArray(int size, int ids[]); void printArray(int size, int * idPointer); int main(void) { // 1. declare an array of 5 integers called ids int ids[5]; // 2. declare an integer pointer called arrayPointer and // initialize it to point to the array called ids int *arrayPointer = ids; // 3. call initializeArray() function sending to it // 5 for the size and the array called...
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
Using the Issue, Rule, Analysis, Conclusion outline, I'm having trouble creating the IRAC for this situation....Happy...
Using the Issue, Rule, Analysis, Conclusion outline, I'm having trouble creating the IRAC for this situation....Happy City opened bidding for an airport construction project, by the usual process of advertising a request for bids. Crafty Construction submitted the lowest responsive bid. When the Happy City Council met to review the bid the Council members discovered the bid exceeded the budget for the project and discussed the possibility of negotiating a bid reduction. The city manager told the Council that Chris...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
I'm having trouble programming connect four board game using linked lists, sets and maps in c++....
I'm having trouble programming connect four board game using linked lists, sets and maps in c++. Can you code connect four game using these concepts.
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the...
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the story. Why would Rama be set apart as a true hero? The story of Rama and Sita is a favorite story that parents tell their children. What purpose does the Ramayana serve as an instructional story for Indian culture? What effect do the test and temptations have on the heroic character?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT