Summing 2 arrays ( Defining and manipulating your own one dimensional arrays. )
ID: 3634741 • Letter: S
Question
Summing 2 arrays ( Defining and manipulating your own one dimensional arrays. )Get the input file “inputNumbers.txt”. Write a program to read in the first 50 numbers into one array, then read the next 50 numbers into a second array. Create a new third array that will contain the sum of the corresponding values of the first 2 arrays. Print: First array number, Second array number and then the sum that is stored in the third array, print the second number stored in the first array, the second number in the second array and the sum in the third array, etc.
For Example, if the arrays only contained 4 numbers and the input file looked like this:
1 2 3 4 5 6 7 8
The first array would have: 1 2 3 4
The second array would have: 5 6 7 8
The third array would have: 6 8 10 12
What is printed:
1 5 6
2 6 8
3 7 10
4 8 12
Explanation / Answer
Here is the code import java.util.Scanner; import java.io.*; public class SumArray { public static void main(String[] args)throws IOException { String filename; // File name int arr1[]=new int[50]; int arr2[]=new int[50]; int arr3[]=new int[50]; // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Get the filename. System.out.print("Enter the filename: "); filename = keyboard.nextLine(); // Open the file. FileReader freader = new FileReader(filename); BufferedReader inputFile = new BufferedReader(freader); // Read the first line from the file. int num = Integer.parseInt(inputFile.readLine()); int i=0; System.out.print("Array1 "); while (iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.