Mauwanna0327
Eternal Poster
- Joined
- Jul 20, 2017
- Posts
- 773
- Reaction
- 114
- Points
- 269
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MyClass {
public static void main(String args[]) {
int[][] table;
int inputRows = 0;
int inputColumns = 0;
int rowCounter = 0;
int colCounter = 0;
int evenSum = 0;
int totalSum = 0;
int uniqueSum = 0;
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter no. of rows: ");
inputRows = Integer.parseInt(buff.readLine());
System.out.print("Enter no. of columns: ");
inputColumns = Integer.parseInt(buff.readLine());
table = new int[inputColumns][inputRows];
System.out.println(String.format("Enter %d numbers: ", (inputColumns*inputRows)));
//loop for user input
for(int i=1;i<=inputRows*inputColumns;i++){
System.out.print(String.format("%d.) ", i));
int cell = Integer.parseInt(buff.readLine());
table[colCounter][rowCounter] = cell;
if(colCounter == inputColumns - 1){
rowCounter++;
colCounter=0;
}
else {
colCounter++;
}
}
//loop for displaying output
rowCounter = 0;
colCounter = 0;
for(int i=0;i<inputRows*inputColumns;i++){
int cell = table[colCounter][rowCounter];
boolean isUnique = true;
int dupe = 0;
String property = "";
for(int rowLooper=0; rowLooper<inputRows; rowLooper++){
for(int colLooper=0; colLooper<inputColumns; colLooper++){
if (cell == table[colLooper][rowLooper]){
dupe++;
isUnique = !(dupe >= 2);
}
}
}
System.out.print(String.format("%d",cell));
totalSum += cell;
if (cell % 2 == 0 ){
evenSum += cell;
property += "^";
}
if (isUnique){
uniqueSum += cell;
property += "*";
}
if (property.length > 0){
property = " ";
}
uniqueSum += isUnique ? cell : 0;
System.out.print(isUnique ? property+" " : " ");
if(colCounter == inputColumns - 1){
rowCounter++;
colCounter=0;
System.out.print("\n");
}
else {
colCounter++;
}
}
System.out.println("Sum of all Integers: " + totalSum);
System.out.println("Sum of all Even: " + evenSum);
System.out.println("Sum of all Unique: " + uniqueSum);
}catch(IOException e){
System.out.println("Invalid input");
}
}
}
public class Problem01
{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
int sizeRow;
int sizeCol;
int sumAll = 0;
int sumEven = 0;
int sumUnique = 0;
boolean isUnique;
int uniqueCounter = 0;
System.out.print("Row Size: ");
sizeRow = userInput.nextInt();
System.out.print("Column Size: ");
sizeCol = userInput.nextInt();
int arrayInt[][] = new int[sizeRow][sizeCol];
int uniqueInt[] = new int[sizeRow * sizeCol];
//Get the inputs from user and get sum needed
System.out.println("Put the " + sizeRow*sizeCol + " integers: ");
for(int i = 0; i < sizeRow; i++)
{
for(int j = 0; j < sizeCol; j++)
{
arrayInt[i][j] = userInput.nextInt();
sumAll += arrayInt[i][j];
sumEven += ((arrayInt[i][j] % 2 == 0) ? arrayInt[i][j] : 0);
//Check if unique, if yes add
isUnique = true;
for(int k = 0; k < uniqueCounter; k++)
{
if(arrayInt[i][j] == uniqueInt[k])
{
isUnique = false;
break;
}
}
if(isUnique)
{
uniqueInt[uniqueCounter] = arrayInt[i][j];
sumUnique += arrayInt[i][j];
uniqueCounter++;
}
}
}
System.out.println("Input: ");
for(int i = 0; i < sizeRow; i++)
{
for(int j = 0; j < sizeCol; j++)
{
System.out.print(arrayInt[i][j] + "\t");
}
System.out.println("");
}
System.out.println("Sum of all integers: " + sumAll);
System.out.println("Sum of all even: " + sumEven);
System.out.println("Sum of all unique: " + sumUnique);
}
}
public class Problem02
{
public static void main(String[] args)
{
int sizeArray;
Scanner userInput = new Scanner(System.in);
System.out.print("Size: ");
sizeArray = userInput.nextInt();
int arrayInt[][] = new int[sizeArray][sizeArray];
//Get the inputs from user
System.out.println("Put the " + sizeArray*sizeArray + " integers: ");
for(int i = 0; i < sizeArray; i++)
{
for(int j = 0; j < sizeArray; j++)
{
arrayInt[i][j] = userInput.nextInt();
}
}
//Display the table
System.out.println("Input: ");
for(int i = 0; i < sizeArray; i++)
{
for(int j = 0; j < sizeArray; j++)
{
System.out.print(arrayInt[i][j] + "\t");
}
System.out.println("");
}
//Get the sum of 1st diagonal
int firstDiagonalSum = 0;
for(int i = 0; i < sizeArray; i++)
{
firstDiagonalSum += arrayInt[i][i];
}
System.out.println("\nSum of first diagonal: " + firstDiagonalSum);
//Get the sum of 2nd diagonal
int secondDiagonalSum = 0;
for(int i = 0; i < sizeArray; i++)
{
secondDiagonalSum += arrayInt[i][(sizeArray-1) - i];
}
System.out.println("Sum of second diagonal: " + secondDiagonalSum);
}
}
Pano po yan sa oop sir?hmm multi dimensional arrays. get position of the diagonal. tapos kunin mo yung numbers then total mo lahat.