#assignment in java

1 messages · Page 1 of 1 (latest)

slender wind
#

i am struggling to correct the output of my code, as you can see in the picture, the v output is a c e. but it should be a e c.

gusty cairnBOT
#

<@&987246399047479336> please have a look, thanks.

slender wind
#

here is my code

slender wind
# slender wind here is my code

import java.util.Scanner;

public class Midterms1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Define the number of rows and columns
int numRows;
int numCols;

    System.out.print("Enter Number of Rows: ");
    numRows = scanner.nextInt();

    System.out.print("Enter Number of Columns: ");
    numCols = scanner.nextInt();
    int numCols2 = numCols - 1;

    if (numCols % 2 == 0) {
        numCols = numCols2;
    }

    // Create the 2D character array
    char[][] matrix = new char[numRows][numCols];

    // Populate the matrix with the given values
    char ch = 'a';
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            matrix[i][j] = ch;
            ch++;
            if (ch > 'z') {
                ch = 'a';
            }
        }
    }

    // Print the matrix
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            System.out.print(matrix[i][j] + " ");
        }
        System.out.println();
    }

    // Print the V shape
    System.out.print("V Output: ");
    int midRow = numRows / 2;
    int midCol = numCols / 2;

    // Print the letters forming the V shape
    // Print the V shape
    System.out.print("V Output: ");
    for (int i = 0; i < numRows; i++) {
        for (int j = 0; j < numCols; j++) {
            if ((i == j && i <= midRow && j <= midCol) || (j == numCols - i - 1 && i <= midRow && j >= midCol)) {
                System.out.print(matrix[i][j] + " ");
            }
        }
    }
    System.out.println();

}

}

regal lynx
#

Isn't there a constraint also on the minimum number of rows?

#

Also, you know you're going to take a letter from each column, so that index always increments by 1. It's only which row that goes up and down (after it hits the middle column position.)