Question
How can I get this loop that will add all the values in all odd columns, and print their total aligned below the column. I
How can I get this loop that will add all the values in all odd columns, and print their total aligned below the column. I need that the 45 gets aligned with the 9, 135 with 27, 225 with 45, 315 with 63, and 105 with 81. The first picture is what I have so far and the second one is how I need it to be exactly.
This is the code:
import java.util.*;
public class Test
{
//arrays method.
public static void arrays(int _2array[][])
{
//variable to store the sum of all the elements of the matrix.
int sum =0;
//Run the loop to insert the
//values into the 2-D array.
//Display the value present at the
//array index after being updated.
for(int i=0;i
{
for (int j=0;j
{
_2array[i][j] = i*j;
//Use printf() method
//to align the values.
System.out.printf("%5d",_2array[i][j]);
//Update the value of the variable
//sum by adding the value of the
//present at the current index
//of the 2-d array to it.
sum = sum + _2array[i][j];
}
//Move to the next line
//after displaying each row.
System.out.printf(" ");
}
//variable to store the sum of each odd column.
int col_sum=0;
System.out.print("Total");
//Run the loop to find the
//sum of each odd column.
//Display the sum of each column
for(int i=0;i
{
if(i%2!=0)
{
for(int j=0;j
{
col_sum = col_sum + _2array[j][i];
}
//Display the sum of each odd column.
System.out.printf("%10d",col_sum);
//Reset the value of the variable col_sum.
col_sum=0;
}
}
System.out.printf(" Array Index Total:" + sum + " " );
}
//main method.
public static void main(String args[])
{
//Define a 2-D array of int type.
int _2array[][] = new int[10][10];
//Call the arrays() method.
arrays(_2array);
}
}
6 8 10 12 1416 18 6 9 12 15 18 21 24 27 8 12 1620 242832 36 5 10 1520 25 30 35 40 45 6 12 18 24 30 36 42 48 54 0714 21 28 35 42 49 56 63 8 16 243240 48 56 64 72 63 72 81 9 18 27 36 45 54 225 Total 45 135 315 405 Array Index Total:202!5Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started