arrays - How do I fix my addition for summing a specific row and column in java? -
i have of other methods required class program working properly, can not think of how fix row input , sum method , column input , sum method. apologize ridiculously long method names, methods pre-named assignment.
import java.util.scanner; public class two_d_array_cmps280_2016 {// begin class two_d_array_cmps280_2016 public static void main(string args[]) {// begin main method int[][] brianarray = {// initialize array , hard code values {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };// end array initialization // print segment print values methods brianarray system.out.print("the sum of elements " + total_2darray_elements(brianarray)); system.out.print("\nthe average of elements " + average_2darray_elements(brianarray)); system.out.print("the sum is: " + total_2darray_elements_by_certain_row_number(brianarray)); system.out.print("the sum is: " + total_2darray_elements_by_certain_column_number(brianarray)); system.out.print("the largest value is: " + the_largest_value_in_certain_row_number(brianarray)); system.out.print("the largest value is: " + the_largest_value_in_certain_column_number(brianarray)); }// end main method public static void printarray(int[][] myarray) {// begin print arrrays (int = 0; < myarray.length; i++) { (int j = 0; j < myarray[i].length; j++) { system.out.print(myarray[i][j] + " "); } system.out.println(); } }// end printarray method public static int total_2darray_elements(int[][] myarray) {// begin method sum array values int total = 0; (int = 0; < myarray.length; i++) { (int j = 0; j < myarray[i].length; j++) { total += myarray[i][j]; } } return total; } public static int average_2darray_elements(int[][] myarray) {// begin method sum array values int total = 0; int average = 0; (int = 0; < myarray.length; i++) { (int j = 0; j < myarray[i].length; j++) { total += myarray[i][j]; average = total / 16; } } return average; }// end method sum array values public static int total_2darray_elements_by_certain_row_number(int[][] myarray) {// begin method sum array user input row java.util.scanner input = new scanner(system.in); int total = 0; system.out.println("\nplease enter row sum: "); int row = input.nextint() - 1; (int = row; < myarray.length; i++) { (int j = 0; j < myarray[0].length; j++) { total += myarray[row][j]; } } return total; }// end method sum array user input row public static int total_2darray_elements_by_certain_column_number(int[][] myarray) {// begin method sum array user input column java.util.scanner input = new scanner(system.in); int total = 0; system.out.println("\nplease enter column sum: "); int column = input.nextint() - 1; (int = 0; < myarray.length; i++) { (int j = column; j < myarray[0].length; j++) { total += myarray[i][column]; } } return total; }// end method sum array user input column public static int the_largest_value_in_certain_row_number(int[][] myarray) {// begin method return largest number user input row java.util.scanner input = new scanner(system.in); int highestvalue = 0; system.out.println("\nplease enter row highest value from: "); int row = input.nextint() - 1; (int = row; < myarray.length; i++) { (int j = 0; j < myarray[i].length; j++) { if (myarray[row][j] > highestvalue) highestvalue = myarray[row][j]; } } return highestvalue; }// end method return largest number user input row public static int the_largest_value_in_certain_column_number(int[][] myarray) {// begin method return largest number user input column java.util.scanner input = new scanner(system.in); int highestvalue = 0; system.out.println("\nplease enter column highest value from: "); int column = input.nextint() - 1; (int = 0; < myarray.length; i++) { (int j = column; j < myarray[i].length; j++) { if (myarray[i][column] > highestvalue) highestvalue = myarray[i][column]; } } return highestvalue; }// end method return largest number user input column }// end class
change methods following, should :
public static int total_2darray_elements_by_certain_row_number(int[][] myarray) {// begin method sum array user input row java.util.scanner input = new scanner(system.in); int total = 0; system.out.println("\nplease enter row sum: "); int row = input.nextint() - 1; (int j = 0; j < myarray[0].length; j++) { total += myarray[row][j]; } return total; }// end..
and
public static int total_2darray_elements_by_certain_column_number(int[][] myarray) {// begin method sum array user input column java.util.scanner input = new scanner(system.in); int total = 0; system.out.println("\nplease enter column sum: "); int column = input.nextint() - 1; (int = 0; < myarray.length; i++) { total += myarray[i][column]; } return total; }// end m..
Comments
Post a Comment