Question

In: Computer Science

Write a function declaration for a function that sums each row of a 2D array, where...

Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. Need it in 10 minutes, please.

Solutions

Expert Solution

Program:

import java.util.*;

class RowsSumDemo
{
public static void sumOfEachRow(int M[][])
{

//calculating the sum of each row
int sum=0;
int sumRow[]=new int[10];

for(int r=0;r<=9;r++)
{
sum=0;
for(int c=0;c<=9;c++)
{
sum=sum+M[r][c];
}
sumRow[r]=sum;
}

//Displaying each row with its sum
System.out.println("\n\nElements of the Array & sum of each row: \n");
for(int r=0;r<=9;r++)
{
for(int c=0;c<=9;c++)
{
System.out.print(M[r][c]+" ");
}
System.out.println(" = "+sumRow[r]);
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int N;

//Assume there are 10 rows and 10 columns
int M[][]=new int[10][10];

//Accepting values from user
System.out.println("\nEnter elements: \n");
for(int r=0;r<=9;r++)
{
for(int c=0;c<=9;c++)
{
M[r][c]=sc.nextInt();
}
}

//calling the function
sumOfEachRow(M);
System.out.println("\n");
}
}

Output:


Related Solutions

Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
Which row has the largest sum? Write a method that takes a 2D int array and...
Which row has the largest sum? Write a method that takes a 2D int array and prints: of The index of the row that has the largest sum The sum of elements in that row java
How to print the element in the fifth row and seventh column given this array declaration...
How to print the element in the fifth row and seventh column given this array declaration in dev c: int hello[5][10];
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
Write a function script DirCos.m that takes a vector (any row or column array) as the...
Write a function script DirCos.m that takes a vector (any row or column array) as the argument and returns the direction cosines for that vector. This is for a MatLab script
C++ How do you make a 2d array with a user input. For example, the row...
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
Suppose that every row of M sums to k. Prove that M^n has constant row sums,...
Suppose that every row of M sums to k. Prove that M^n has constant row sums, and find that row sum.
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use of your prior two functions to create a 2D array filled with a default parameter. var twoD = Init2D(<width>, <height>, <fill val>); Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(<min>, <max>); Challenge 5 – Random Int 2D Array Use your prior functions to provide a function...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT