Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /*
- =====================================================
- chapter 5: Arrays
- Ex10: The column where the character appears most frequently
- =====================================================
- */
- public class MyProgram {
- public static void main(String[] args) {
- //variables
- final int SIZE=5;
- char arr[][]=new char[SIZE][SIZE];
- char c; //the character we are searching in the matrix
- int maxCol=0; //represent the col with most repetitions
- int count=0; //counter for each col
- int repeat=0; //max repetition
- Scanner s=new Scanner(System.in);
- //user input → init matrix
- System.out.println("Matrix: Enter char: ");
- for(int i=0;i<arr.length;i++){
- System.out.println("row "+i+" :");
- for (int j=0;j<arr[i].length;j++){
- arr[i][j]=s.next().charAt(0);
- }
- }
- //user input → init char
- System.out.println("Enter single char: ");
- c=s.next().charAt(0);
- //loop count the max repetitions
- for(int col=0;col<arr[0].length;col++){
- for(int row=0;row<arr.length;row++){
- //if c found in matrix inc counter
- if(arr[row][col]==c){
- count++;
- }
- }
- //if we found col with more repetitions
- if(repeat<count){
- repeat=count; // update repeats
- maxCol=col; // update the max col
- }
- count=0; //reset count
- }
- //display msg
- if(repeat>0)
- System.out.println(c+" most repetition in colume number "+maxCol);
- else
- System.out.println("no repetition");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment