Question

In: Computer Science

Please do in java with code available for copy and with comments so I can follow...

Please do in java with code available for copy and with comments so I can follow along :)\

Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns(). Finally, it prints out the array retuned by method sumColumns(). Document your code, and organize and space the outputs properly as shown below. C++ students: instead of asking the user for input, you must read it from a file. See appendix for more information.

Sample run 1:

Enter a value: 9

Enter a value: 1

Enter a value: 2

Enter a value: 4

.

.

.

Enter a value: 3

The entered matrix:

9    1    2    4

2    2    8    0

3    3    3    3

Sum of column 0 is 14

Sum of column 1 is 6

Sum of column 2 is 13

Sum of column 3 is 7

Solutions

Expert Solution

import java.util.*;
public class Main
{
static int [] sumColumn(int arr[][])
{
int i,sum=0,j;
int []res=new int[4];
for(j=0;j<4;j++)//columns loops(loop through column)
{
sum=0;//initialize sum to 0 for every iterration
for(i=0;i<3;i++)//loop through rows
{
sum+=arr[i][j];//add columns elements for each row
}
res[j]=sum;//store the sum value in result array
}
return res;//return array
}
   public static void main(String[] args) {
       int [][]arr=new int[3][4];
       int []result=new int[4];//resultant array to store return array
       int i,j;
       Scanner s=new Scanner(System.in);//Scanner class to read input
       System.out.println("enter values forn3 rows and 4 columns");
       for(i=0;i<3;i++)
       {
       for(j=0;j<4;j++)
       {
       System.out.print("enter a value:");//read the int values
       arr[i][j]=s.nextInt();
       }
       }
       for(i=0;i<3;i++)
       {
       for(j=0;j<4;j++)//print the values
       {
       System.out.print(arr[i][j]+" ");
       }
       System.out.println();
       }
       result=sumColumn(arr);//call function and store in result array
       for(i=0;i<4;i++)
       {
       System.out.println("Sum of column "+i+" is "+result[i]);
       }
   }
}
//For c++ users you asked to input from file but in java you didn't mention if you want to input from file let me know I will update code


Related Solutions

Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Can someone please complete the following code(Java). The stuff in comments are the things you need...
Can someone please complete the following code(Java). The stuff in comments are the things you need to look at and code that package mini2; import static mini2.State.*; /** * Utility class containing the key algorithms for moves in the * a yet-to-be-determined game. */ public class PearlUtil { private PearlUtil() { // disable instantiation } /**    * Replaces all PEARL states with EMPTY state between indices    * start and end, inclusive.    * @param states    * any...
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
Can you add more comments explaining what this code does? i commented what I know so...
Can you add more comments explaining what this code does? i commented what I know so far #include<stdio.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> sem_t mutex,writeblock; int data = 0,rcount = 0; int sleepLength = 2; // used to represent work void *reader(void *arg) { int f; f = ((int)arg); sem_wait(&mutex); // decrement by 1 if rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock); sem_post(&mutex); printf("Data read by the reader%d is %d\n",f,data); //shows current reader and data sleep(sleepLength); // 1 second of "work" is...
please I don't understand this code. Can you put comments to explain the statements. Also, if...
please I don't understand this code. Can you put comments to explain the statements. Also, if there any way to rewrite this code to make it easier, that gonna help me a lot. import java.io.*; import java.util.*; public class State {    private int citi1x,citi1y; private int pop1; private int citi2x,citi2y; private int pop2; private int citi3x,citi3y; private int pop3; private int citi4x,citi4y; private int pop4; private int plantx,planty; public int getCity1X(){ return citi1x; } public int getCity1Y(){ return citi1y;...
Please write the answer in word form so i can copy What is the purpose of...
Please write the answer in word form so i can copy What is the purpose of saying “all-else-equal” or “ceteris paribus” when it comes to multiple linear regression? Why is the F-test important in multiple linear regression more so than in simple linear regression? What is the difference between the AIC and BIC? Why is outlier analysis so important? What is the danger in using stepwise regression? What is the difference between Cook’s Distance and DFFITS? What can you do...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT