Advertisement
mmayoub

arrays-exercise 08, slide 29

Jul 1st, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class arrays08 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * Exercise 8, slide 29
  10.          * input : 10 numbers , save every 5 to an array
  11.          * process: save to each key in the a third array the sum of the values from the same keys in the two other arrays
  12.          * output: the same keys that has the same values
  13.          */
  14.        
  15.         // size of each array
  16.         final int SIZE = 5;
  17.  
  18.         // create a scanner
  19.         Scanner s = new Scanner(System.in);
  20.  
  21.         // define an integer arrays
  22.         int[] numbers1 = new int[SIZE];
  23.         int[] numbers2 = new int[SIZE];
  24.         int[] sum = new int[SIZE];
  25.  
  26.         System.out.printf("Enter %d integer numbers to first array:\n",
  27.                 numbers1.length);
  28.         // get data from user and save it in the array
  29.         for (int i = 0; i < numbers1.length; i += 1) {
  30.             numbers1[i] = s.nextInt();
  31.         }
  32.         System.out.printf("Enter %d integer numbers to seconed array:\n",
  33.                 numbers2.length);
  34.         // get data from user and save it in the array
  35.         for (int i = 0; i < numbers2.length; i += 1) {
  36.             numbers2[i] = s.nextInt();
  37.         }
  38.         s.close();
  39.  
  40.         // add and save to results array, then print it
  41.         for (int i = 0; i < sum.length; i += 1) {
  42.             // save sum to results array
  43.             sum[i] = numbers1[i] + numbers2[i];
  44.             // print results
  45.             System.out.printf("%d ", sum[i]);
  46.         }
  47.         System.out.println();
  48.  
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement