Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5)SumArray and 6) fibarray Write a method called sumArray that takes in as a par

ID: 3552744 • Letter: 5

Question

5)SumArray and 6) fibarray

Write a method called sumArray that takes in as a parameter an array of integers x and returns an integer. Your method should return the sum of all the values stored in x. public static int [] x) Write a main method that takes an integer n from the user using a Scanner object and prints the sum of the first n Fibonacci numbers. Repeat until the user enters a negative number. You must call the fibArray and sumArray methods you defined in questions 4 and 5. You can assume these methods are defined in the same class and can be used directly from the main method. Hint: call the fibArray method to produce an array, then pass that array as a parameter into sumArray to get the result.

Explanation / Answer

1)

public static int sumArray(int[] x){

for(int i=0;i<x.length;i++){

sum+=x[i]

}

return sum;

}


2)


import java.util.Scanner;


public static int[] fibArray(int n){


int[] arr=new int[n];


int k=0;


int prev, next, sum, n;


prev=next=1


for(n=1;n<=10;n++)


{


arr[k++]=prev;


sum=prev+next;


prev=next;


next=sum;


}


return arr;


}


public static void main(String args[]){

Scanner inp = new Scanner(System.in);

int n;

int flag=1;

int sum;

do{

flag=1;

n=inp.nextInt();

if(n<0)

flag=0;

else{

int[] arr=new int[n];

arr[]=fibArr(n);

System.out.println("Sum of "+n+" fibonnaci nos is "+sumArray(arr));

}while(flag==1);

}







Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote