CS1020 Matrix Lab 0 Ex 2

This is a continuation of the series of code related to CS1020.
Link to the question – Matrix Lab 0 Ex 2

import java.util.*;

public class Matrix {
	
	public static void main(String[] args) {

        // declare the necessary variables


		// create new object from class Result


		// declare a Scanner object to read input


		// read input and process them accordingly
		Scanner scan = new Scanner(System.in);
		int arrRow = scan.nextInt();
		int arrCol = scan.nextInt();
		int[][] array = new int[arrRow][arrCol];
		for(int i = 0; i < arrRow; i++)
		{
			for(int j = 0; j < arrCol; j++)
			{
				array[i][j] = scan.nextInt();
			}
		}
		String toSum = scan.next();
		int row = scan.nextInt();
		int result = 0;

		if(toSum.equals("ROW"))
		{
			for(int i = 0; i < arrCol; i++)
			{
				result = result + array[row-1][i];
			}
		}
		else if(toSum.equals("COLUMN"))
		{
			for(int i = 0; i < arrRow; i++)
			{
				result = result + array[i][row-1];
			}
		}
		System.out.println(result);
	}
}
Advertisements
This entry was posted in NUS, SoC and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s