What's new

Closed Hayssss hanggang table lang mapiga ko kaphc

Status
Not open for further replies.

Mauwanna0327

Eternal Poster
Joined
Jul 20, 2017
Posts
773
Reaction
114
Points
269
Pano ba codes nito mga peeps received_2043557822332235.jpeg received_343133809780745.jpeg received_343133809780745.jpeg received_2043557822332235.jpeg
 

Attachments

2 dimensional array po yan,

box[0][0] ung first po is row ung second po is column meaning po mag gumawa ka ng table na 3x3 ang mga array mo is

-------------------------------------------------
| box[0][0] | box[0][1] | box[0][2] |
-------------------------------------------------
| box[1][0] | box[1][1] | box[1][2] |
-------------------------------------------------
| box[2][0] | box[2][1] | box[2][2] |
-------------------------------------------------

try ko lng maya eh code, medyo mahaba kasi,
 
try nyo po ito sir pag sumuko na kayo; konting loops lang po yan, kayo na po bahala sa diagonal
Code:
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");
        }
    }
}
 
Simple solution para sa simple problem mo. Since array ang topic niyo use array.
Pag-aralan mo mabuti. Madali na yan intindihin.

Custom size table:
Code:
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);
    }
    
}

Perfect size table:
Code:
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);
    }
}
 
hmm multi dimensional arrays. get position of the diagonal. tapos kunin mo yung numbers then total mo lahat.
 
Status
Not open for further replies.

Similar threads

Back
Top