Finding the sum of multiple inputted Arrays

Koru

I am finding the sum of multiple arrays and have searched various codes and programs but it seems none has worked for me or should I say none has actually made my brain work to create a code based on what I saw. "What I am trying to do is to add the 3 inputed Grade Arrays and then divide it by 3 to get the Array for Average."

package Activity;

import java.util.*;

public class TestCode {

  static Scanner sc = new Scanner(System.in);
  static int indexMax = 3;



  public static void main(String[] args) {
    String InfoName[][] = new String[indexMax][2];
    Double InfoGrade[][] = new Double[indexMax][3];

    InputInfoName(InfoName);
    InputGrade(InfoGrade);

    PrintInfo(InfoName, ComputeAvg);


  }
  public static String[][] InputInfoName(String NameArr[][]) {
    for (int i = 0; i < indexMax; i++) {
      System.out.println();
      System.out.println("Enter Student[" + (i + 1) + "]'s Full Name");
      System.out.println();

      System.out.print("First Name: ");
      NameArr[i][0] = sc.nextLine();

      System.out.print("Last Name: ");
      NameArr[i][1] = sc.nextLine();
    }
    return NameArr;
  }

  public static Double[][] InputGrade(Double GradeArr[][]) {
    for (int i = 0; i < indexMax; i++) {
      System.out.println();
      System.out.println("Enter Student[" + (i + 1) + "]'s Grades");
      System.out.println();

      System.out.print("1st Grade: ");
      GradeArr[i][2] = sc.nextDouble();

      System.out.print("2nd Grade: ");
      GradeArr[i][3] = sc.nextDouble();

      System.out.print("3rd Grade: ");
      GradeArr[i][4] = sc.nextDouble();

    }
    return GradeArr;
  }
  // Main Problem
  public static Double[][] ComputeAvg(Double ComputeArr[][]) {
    Double AddAvg[][] = new Double[indexMax][];
    Double getAvg[][] = new Double[indexMax][];

    for (int i = 0; i < indexMax; i++) {
      AddAvg[i][] = // I don't know what is next
    }
    return ComputeArr;
  }
  // End of Main Problem
  public static void PrintInfo(String arr[][], Double ComputeAvg[]) {
    System.out.println("");
    System.out.println("Student Info");
    for (int i = 0; i < arr.length; i++) {

      System.out.println("Student[" + i + "]: " + arr[i][0] + "," + arr[i][1] + "     Average: " + ComputeAvg[i]);
    }
  }
}

dreamcrash

Side-note Personally, I would replace the use of the Double object by the primitive-type double, mainly because of (apart from performance) readability and because in your code is not really needed.

Create the array that will keep the average by array:

Double AddAvg[] = new Double[indexMax];

iterate through the arrays and get the average of those arrays using Java streams:

  Arrays.stream(ComputeArr[i])
        .mapToDouble(Double::doubleValue)
        .average()
        .orElse(0);

The full method:

   public static Double[] ComputeAvg(Double[][] ComputeArr) {
        Double[] AddAvg = new Double[indexMax];

        for (int i = 0; i < indexMax; i++) {
            AddAvg[i] =  Arrays.stream(ComputeArr[i])
                                .mapToDouble(Double::doubleValue)
                                .average()
                                .orElse(0);
        }
        return AddAvg;
    }

If you cannot use streams then do the sum manually:

public static Double[] ComputeAvg(Double[][] ComputeArr) {
    Double[] AddAvg = new Double[3];
    for (int i = 0; i < 3; i++) {
        double sum = 0.0;
        for(int j = 0; j < ComputeArr[i].length; j++) {
            sum += ComputeArr[i][j];
        }
        AddAvg[i] = sum /  ComputeArr[i].length;
    }
    return AddAvg;
}

In Java arrays indexed from 0 to the size of the array -1. So this method:

  public static Double[][] InputGrade(Double GradeArr[][]) {
    for (int i = 0; i < indexMax; i++) {
      System.out.println();
      System.out.println("Enter Student[" + (i + 1) + "]'s Grades");
      System.out.println();

      System.out.print("1st Grade: ");
      GradeArr[i][2] = sc.nextDouble();

      System.out.print("2nd Grade: ");
      GradeArr[i][3] = sc.nextDouble();

      System.out.print("3rd Grade: ");
      GradeArr[i][4] = sc.nextDouble();

    }
    return GradeArr;
  }

is accessing positions outside the boundaries of the array GradeArr (i.e., GradeArr[i][4]). Change to :

  public static Double[][] InputGrade(Double GradeArr[][]) {
    for (int i = 0; i < indexMax; i++) {
      System.out.println();
      System.out.println("Enter Student[" + (i + 1) + "]'s Grades");
      System.out.println();

      System.out.print("1st Grade: ");
      GradeArr[i][0] = sc.nextDouble();

      System.out.print("2nd Grade: ");
      GradeArr[i][1] = sc.nextDouble();

      System.out.print("3rd Grade: ");
      GradeArr[i][2] = sc.nextDouble();

    }
    return GradeArr;
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding the average of multiple arrays

From Dev

Finding dependency from multiple arrays

From Dev

SUM and COUNTIFS with multiple arrays?

From Dev

finding all possible sum of two arrays element

From Dev

finding the sum of two arrays elements that is equal to the input

From Dev

Process of finding sum of cells from multiple sheets

From Dev

Finding an algorithm without divisions for multiple sum

From Dev

Querying multiple columns in a database and finding their sum

From Dev

Finding and locating multiple values in two multidimensional arrays

From Dev

Finding common elements in multiple arrays php

From Dev

Finding highest and lowest array count in multiple arrays

From Dev

finding the difference in multiple arrays. using reduce

From Dev

Finding the sum of common elements between n number of arrays in java

From Dev

Problems finding out total sum using data structures and arrays

From Dev

Merge and sum multiple multidimensional associative arrays

From Dev

Merge and sum multiple multidimensional arrays in PHP

From Dev

Finding nearest next number for n, with sum of multiple of 2 numbers

From Dev

Problems with sum of user-inputted array elements

From Dev

finding symmetric difference/unique elements in multiple arrays in javascript

From Dev

Finding a single, or multiple values from a string of arrays in Javascript

From Dev

Java: Finding the weekday of a calendar date which is inputted by the user

From Dev

GET action with multiple ids inputted .NET

From Dev

Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

From Dev

Adding to arrays and finding values in arrays

From Dev

MongoDB sum arrays from multiple documents on a per-element basis

From Dev

Finding the sum of a nested array

From Dev

Finding the sum of an array in javascript

From Dev

Finding the sum of a nested array

From Dev

Finding Count Sum in Array

Related Related

  1. 1

    Finding the average of multiple arrays

  2. 2

    Finding dependency from multiple arrays

  3. 3

    SUM and COUNTIFS with multiple arrays?

  4. 4

    finding all possible sum of two arrays element

  5. 5

    finding the sum of two arrays elements that is equal to the input

  6. 6

    Process of finding sum of cells from multiple sheets

  7. 7

    Finding an algorithm without divisions for multiple sum

  8. 8

    Querying multiple columns in a database and finding their sum

  9. 9

    Finding and locating multiple values in two multidimensional arrays

  10. 10

    Finding common elements in multiple arrays php

  11. 11

    Finding highest and lowest array count in multiple arrays

  12. 12

    finding the difference in multiple arrays. using reduce

  13. 13

    Finding the sum of common elements between n number of arrays in java

  14. 14

    Problems finding out total sum using data structures and arrays

  15. 15

    Merge and sum multiple multidimensional associative arrays

  16. 16

    Merge and sum multiple multidimensional arrays in PHP

  17. 17

    Finding nearest next number for n, with sum of multiple of 2 numbers

  18. 18

    Problems with sum of user-inputted array elements

  19. 19

    finding symmetric difference/unique elements in multiple arrays in javascript

  20. 20

    Finding a single, or multiple values from a string of arrays in Javascript

  21. 21

    Java: Finding the weekday of a calendar date which is inputted by the user

  22. 22

    GET action with multiple ids inputted .NET

  23. 23

    Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

  24. 24

    Adding to arrays and finding values in arrays

  25. 25

    MongoDB sum arrays from multiple documents on a per-element basis

  26. 26

    Finding the sum of a nested array

  27. 27

    Finding the sum of an array in javascript

  28. 28

    Finding the sum of a nested array

  29. 29

    Finding Count Sum in Array

HotTag

Archive