Question

In: Computer Science

Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE...

Language: Java

So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE of a 2d matrix using only ONE class. However, the output gives me the incorrect calculations.

This is my class:

public static void MajorAndMinorDiagonalSumAndAvg (Scanner user, int rows, int coluumn, int [][] array) {

double majorarray = 0; double majorarraycount = 0; double minorarray = 0; double minorarraycount = 0;

for (int i = 0; i<array.length; i++)

for(int j =0; j<array[i].length; j++)

{

majorarray += array[i][i];

majorarraycount++;

minorarray += array[i][array.length - i-1];

minorarraycount++;

}

System.out.printf("Major Diagonal Sum: " + "%.2f", majorarray);

System.out.println();

System.out.printf("Major Diagonal Avg: " + "%.2f",majorarray/majorarraycount);

System.out.println();

System.out.printf("Minor Diagonal Sum: " + "%.2f",minorarray);

System.out.println();

System.out.printf("Minor Diagonal Avg: " + "%.2f",minorarray/minorarraycount);

System.out.println();

}

What should I change to get the correct calculations?

Solutions

Expert Solution

public class DiagonalSum {

// method to calculate the MAJOR and MINOR diagonal SUM and AVERAGE of a 2d matrix

// remove user, rows and column parameters from method since they are not used in the function

       public static void MajorAndMinorDiagonalSumAndAvg (int [][] array) {

             double majorarray = 0, majorarraycount = 0, minorarray = 0, minorarraycount = 0;

            

             // loop over the rows of the input matrix

             for(int i=0;i<array.length;i++)

             {

                    majorarray += array[i][i]; // calculate sum of major axis

                    majorarraycount++; // calculate number of elements in major axis

                    minorarray += array[i][array.length-i-1]; // calculate sum of minor axis

                    minorarraycount++; // calculate number of elements in minor axis

             }

            

             // output

             System.out.printf("Major Diagonal Sum: " + "%.2f", majorarray);

             System.out.println();

             System.out.printf("Major Diagonal Avg: " + "%.2f",majorarray/majorarraycount);

             System.out.println();

             System.out.printf("Minor Diagonal Sum: " + "%.2f",minorarray);

             System.out.println();

             System.out.printf("Minor Diagonal Avg: " + "%.2f",minorarray/minorarraycount);

             System.out.println();

                   

       }

      

       public static void main(String[] args){

            

            

             // test the method

             int array[][] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};

             for(int i=0;i<array.length;i++)

             {

                    System.out.println();

                    for(int j=0;j<array[i].length;j++)

                           System.out.print(array[i][j]+" ");

             }

             System.out.println();

             MajorAndMinorDiagonalSumAndAvg(array);

}

}

//end of program

Output:


Related Solutions

So, we did a Beer's Law lab and I am trying to find out how to...
So, we did a Beer's Law lab and I am trying to find out how to find the concentrations to make the absorbace vs concentration graph. Say the path length is 1.0 cm, how do you find the molar absorbtivity (in order to get the concentrations with A = epsilon * path length * concentration)? I think there is a way to get the molar absorbtivity from the wavelength vs absorbance graph but I'm not sure how. Our data is...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 3? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
I am trying to figure out the best way to solving a problem in the language...
I am trying to figure out the best way to solving a problem in the language python. I have some but have no clue if I am even going in the right direction. Here are the instructions: Write a program that calculates the shopping list for a birthday party with the minimum amount of leftovers. The program should ask the user for the number of kids attending the party. Assume each kid will cook (but not necessarily eat) 2 hot...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
Hey I am trying to find a way to find the position of a Red Dwarf...
Hey I am trying to find a way to find the position of a Red Dwarf star that is in an elliptical orbit at a certain time during it's orbit. I know the Red Dwarf's semi major axis which is 3 AU (astronomical unit) and it's period is 1896 days. I've tried to do a simple using its orbit velocity and doing distance=velocity x time, but I wasn't getting answers that made sense. If anyone could set me on the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT