0

Write a program to calculate the first 10 Fibonacci numbers and store the results in a one-dimensional array. In a second array calculate and store the average values of the adjacent numbers in the series. The first array should contain integer values and the second floating point values. Output the contents of both arrays in a neat format

 public static void main(String[] args) {
     //number of elements to generate in a series
     int limit = 10;

     long[] series = new long[limit];

     //create first 2 series elements
     series[0] = 1;
     series[1] = 1;

     //create the Fibonacci series and store it in an array
     for(int i=2; i < limit; i++){
         series[i] = series[i-1] + series[i-2];
     }

     //print the Fibonacci series numbers
     System.out.println("Fibonacci Series upto " + limit);
     for (int i = 0; i < limit; i++) {
         System.out.print(series[i] + " ");
     }
}

Okay so the first part is working fine but now to create an array to calculate the average is a bit tricky for me.So far I tried this.

int[] numbers = new int[]{1,1,2,3,5,8,13,21,34,55};
int sum=0;
for (int i = 0; i < numbers.length ; i++) {        
     sum = (int) (sum + numbers[i]);
     double average = (double)sum/numbers.length;
     System.out.println("Average value of array elements is : " + average);
}

But its not working quite well.Can someone offer me some light on this ?

3
  • A) you want us to spend our time to help you ; so you please spend the few minutes it takes to properly format you input! B) "is not working" is a pretty useless term. What exactly is not working. In other words: please turn to the help center and read "how to ask" Commented Oct 30, 2016 at 19:51
  • 1
    You want to put your average calculation and printing outside the loop. Commented Oct 30, 2016 at 19:52
  • It says "store the average values of the adjacent numbers in the series" i.e. (series[i] + series[i+1])/2.0 Commented Oct 30, 2016 at 20:00

2 Answers 2

2

You should not calculate the average inside the for loop. Move your average outside of the loop so it calculates once sum is accurate.

            int[] numbers = new int[]{1,1,2,3,5,8,13,21,34,55};
            int sum=0;
            for(int i=0; i < numbers.length ; i++){        
                 sum = (int) (sum + numbers[i]);
            }
            double average = (double)sum/numbers.length;
            System.out.println("Average value of array elements is : " + average);
Sign up to request clarification or add additional context in comments.

2 Comments

What if you want to initialize the array with the series[i] instead of int[] numbers = new int[]{1,1,2,3,5,8,13,21,34,55}; How we change that ?
You could do int[] numbers = new int[limit] and then in a for loop you should set each numbers[i] = series[i].
1

Try this:

int[] numbers = new int[]{1,1,2,3,5,8,13,21,34,55};
int sum=0;
for (int i = 0; i < numbers.length - 1; i++) {        
     sum = (int) (sum + numbers[i]);
     double average = sum/2.0;
     System.out.println("Average value of array elements is : " + average);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.