Home > Software engineering >  Is there any way to simplify printing x amount of columns for different rows to add up to 50 total i
Is there any way to simplify printing x amount of columns for different rows to add up to 50 total i

Time:04-19

I have this assignment I solved, where the user enters X amount of rows. The program then determines how many columns there needs to be to add up to 50 total items. If it adds another column, this column will end up having too many items, so I needed to figure out how to not print these extra characters. I did figure a solution, but I was wondering if there was a more simpler way.

public class J1_Lab08_3 {
    public static void main(String[] args) {
        userInput();
    }

    public static void userInput(){
        Scanner input = new Scanner(System.in); // scanner object
        int rowAmount;
        char[][] array;
        while(true) {
            System.out.println("Please enter the amount of rows 1-50. Enter 0 to quit program"); // asking the user to enter row amount
            rowAmount = input.nextInt();

            if(rowAmount == 0){
                System.out.println("Thank you.");
                System.exit(0);
            } else if(rowAmount >= 51){
                System.out.println("Please enter a value less than 50");
                System.exit(0);
            }

            array = createArray(rowAmount); // created array
            print2dCharArray(array); // prints array
        }
    }

    public static char[][] createArray(int sizeX){
        char[][] array = new char[sizeX][(50 sizeX-1)/sizeX];
        int remainder = 50%sizeX;
        int counter = 0;
        for(int i = 0; i < sizeX; i  ){ // goes through each row in the array
            for(int j = 0; j < 50/sizeX; j  ){ // goes through each column in the array

                array[i][j] = (char)('a'   (Math.random() * ('z' - 'a'))); // creates a random letter from a-z
            }
        }

        while(remainder > 0){
            array[counter][(50/sizeX)] = (char)('a'   (Math.random() * ('z' - 'a')));
            counter  ; // goes to next row in array
            remainder--; // subtracts by one to know when to stop going down rows
        }

        return array; // returns the array that was created with random characters
    }

    public static void print2dCharArray(char[][] array){
        for(int i = 0; i < array.length; i  ){ // goes through row of array
            for(int j = 0; j < array[0].length; j  ){ // goes through column of array
                if(array[i][j] != 0) { // gets rid of remaining zeros
                    System.out.print("("   array[i][j]   ") "); // prints row and column
                }
            }
            System.out.println(); // prints a new line to start printing new row
        }
        System.out.println();
    }
}

using the example number 7 rows, what I did for this is, I created the non-remainder amount of columns or arrays. So for 7 rows, it would then create 7 columns at first, with 8 being needed in total to reach 50. Then it would check for a remainder and then add on another column if there was a remainder, and every time it adds one it lowers the remainder until the remainder is 0.

Edit: I added a variable to control how many items that need to be printed

public static char[][] createArray(int sizeX){
        int pairAmount = 50; // total amount of items
        char[][] array = new char[sizeX][(pairAmount sizeX-1)/sizeX];
        int remainder = pairAmount%sizeX;
        int counter = 0;

        for(int i = 0; i < sizeX; i  ){ // goes through each row in the array
            for(int j = 0; j < pairAmount/sizeX; j  ){ // goes through each column in the array

                array[i][j] = (char)('a'   (Math.random() * ('z' - 'a'))); // creates a random letter from a-z
            }
        }

        while(remainder > 0){
            array[counter][(pairAmount/sizeX)] = (char)('a'   (Math.random() * ('z' - 'a')));
            counter  ; // goes to next row in array
            remainder--; // subtracts by one to know when to stop going down rows
        }

        return array; // returns the array that was created with random characters
    }

keeping it at 50, and entering a number for example 5. would print to the console

using 5 as row amount, and 50 as total amount of items

using 6 as row amount, and 50 as total amount of items

if totalItems gets changed to 24 and we used the same numbers(5 and 6) it would also produce

using 5 as row amount, and 24 as total amount of items

using 6 as row amount, and 24 as total amount of items

CodePudding user response:

You can make it slightly simpler by also adding the remainder characters in the loop. You can do that by simply counting how much you already added. For example like

public static char[][] createArray(int sizeX){
    int pairAmount = 50; // total amount of items
    int columnAmount = (pairAmount sizeX-1)/sizeX;
    char[][] array = new char[sizeX][columnAmount];

    for(int i = 0; i < sizeX; i  ){ // goes through each row in the array
        for(int j = 0; j < columnAmount && pairAmount > 0; j  ){ // goes through each column in the array
            array[i][j] = (char)('a'   (Math.random() * ('z' - 'a'))); // creates a random letter from a-z
            pairAmount--;
        }
    }
    return array; // returns the array that was created with random characters
}

CodePudding user response:

You can get the first n rows which will have additional columns/elements by doing pairAmount % sizeX, for example for pairAmount = 50 and sizeX = 6: pairAmount % sizeX = 2 which means the first two rows will have 9 elements. You can add this condition in the inner loop and get rid of the while loop completly:

public static char[][] createArray(int sizeX){
    int pairAmount = 50; 
    char[][] array = new char[sizeX][(int) Math.ceil((double) pairAmount / sizeX)];  // I think this way it is clearer, but you can keep your approach if you don't like it
    for(int i = 0; i < sizeX; i  ){ 
        for(int j = 0; j < ((i < pairAmount % sizeX) ? array[i].length : pairAmount/sizeX); j  ){ 
            array[i][j] = (char)('a'   (Math.random() * ('z' - 'a'))); 
        }
    }
    return array;
}

or introduce a variable to make if you don't find the above not readable:

public static char[][] createArray(int sizeX){
    int pairAmount = 50;
    char[][] array = new char[sizeX][(int)Math.ceil((double) pairAmount / sizeX)];
    for(int i = 0; i < sizeX; i  ){
        int maxColumn = (i < pairAmount % sizeX) ? array[i].length : pairAmount / sizeX;
        for(int j = 0; j < maxColumn; j  ){
            array[i][j] = (char)('a'   (Math.random() * ('z' - 'a')));
        }
    }
    return array;
}
  • Related