Question
Write a class Lab13 that has the method add To10 (below) that accepts a 2 dimensional array of integers. Each row will have exactly one
Write a class Lab13 that has the method add To10 (below) that accepts a 2 dimensional array of integers. Each row will have exactly one element with a value of 0. For each row, modify this element so that the sum of all numbers in the row is 10. No other elements should be modified. CANNOT HAVE FIXED SIZE
public static void addTo10(int[][] array){
//Your code here
someone had posted this code already, but the code must be flexible in dimensions, as it needs to allow the parameters provided when its called to set the size of the array, i
public class Lab13
{
public static void addTop10(int[][] array)
{
int i, j, index = 0;
// iterate through the rows
for( i = 0 ; i < array.length ; i++ )
{
int sum = 0;
// iterate through the columns
for( j = 0 ; j < array[i].length ; j++ )
{
// compute the sum of the i th row
sum += array[i][j];
// if the current element is 0
// save it's column in index
if(array[i][j] == 0)
index = j;
}
// set the element with value 0
// to an appropriate value that
// the sum of row is 10
array[i][index] = 10 - sum;
}
}
public static void main(String[] args)
{
int[][] array = {{1, 2, 3, 1, 0},
{3, 1, 2, 0, 1},
{1, 0, 1, 1, 3},
{2, 2, 3, 0, 1},
{0, 1, 1, 1, 1}};
// call addTop10() function
addTop10(array);
int i, j;
// iterate through the rows
for( i = 0 ; i < array.length ; i++ )
{
int sum = 0;
// iterate through the columns
for( j = 0 ; j < array[i].length ; j++ )
{
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Step 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