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

You are required to read double values from a file named \"values.txt\". You don

ID: 3680474 • Letter: Y

Question

You are required to read double values from a file named "values.txt". You don't know how many values will be there. Therefore, you will use a dynamically allocated array of doubles to store the values you are reading from the file. The initial size of the dynamically allocated array should be 10. Whenever you find that you need more space, increases the size of the array by 10. After reading all the values in the file print the count of the numbers, the sum of the numbers, and the average of the numbers on the prompt and in an output file called "output.txt".

Explanation / Answer

Array size cannot be increased dynamically in java. Instead we canuse ArrayList inplace of array and after reading all the elements the ArrayList can be converted to an array.

But as per your requirement, in the following code, i didnot use ArrayList, but used an array and written some logic ti increase the array size. The Logic is that whenever the array has reached the limit, then a method is called which creates a new array with size 10 more than the previous array size and copies the elemnets of the old array to new array.


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;
import java.util.Scanner;

public class Test_ReadDouble {
public static void main(String args[]) throws IOException {

double d;
   double d_arr[] = new double[10];
   double total=0;
   double average=0;
  

FileReader fin = new FileReader("input.txt");

Scanner s = new Scanner(fin);
   int i=0;
   int max_size = i;
while (s.hasNext())
       {
       if (s.hasNextDouble())
               {
           d = s.nextDouble();
           System.out.println("double: " + d);
                   d_arr[i] = d;
                   if(i==d_arr.length-1)
                       {
                           d_arr = formNewArray(d_arr);
                       }
                   total = total + d;
                   i++;  
        }
   }
  
   System.out.println("THE NUMBER OF VALUES READ ARE : " + i);
   System.out.println("THE TOTAL OF ALL DOUBLES READ IS : " + total);
   System.out.println("THE AVERAGE OF VALUES READ IS : " + (total/i));  


FileWriter fout = new FileWriter("test.txt");
fout.write("THE NUMBER OF VALUES READ ARE : " + i);
fout.write(" THE TOTAL OF ALL DOUBLES READ IS : " + total);
fout.write(" THE AVERAGE OF VALUES READ IS : " + (total/i));      
  
   fout.close();
   fin.close();
}
  
// creating a new array with increased value of size and copying elements of old array to new array
public static double[] formNewArray(double[] oldArray){
double[] d_arr = new double[oldArray.length + 10];
for(int i = 0; i < oldArray.length; i++) {
d_arr[i] = oldArray[i];
}

return d_arr;
}
}

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