Printing pattern using Jagged Array - java

Given the jagged Array, we are asked to use a looping statement to display the character based on the position. Display a "*" if the position matched or a " " if it doesn't.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
I have created a program, but the output is not what I expected and I am now stuck.
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
The output was suppose to be Happy but I get:
enter image description here

It's because of your program does not compare the first j value (which is 0) with itself. Since every value equals itself you can add manually * for each line like this.
int arr [][] = {{0,4,8,12,13,14,15,18,19,20,21,24,28},
{0,4,7,9,12,16,18,22,25,27},
{0,1,2,3,4,6,10,12,16,18,22,26},
{0,4,6,10,12,13,14,15,18,19,20,21,26},
{0,4,6,7,8,9,10,12,18,26},
{0,4,6,10,12,18,26}};
for (int i = 0; i < arr.length; i++)
{
System.out.print("*");
for (int j = 0; j < arr[i].length - 1; j++)
{
for (int spaces = 1; spaces < arr[i][j + 1]-arr[i][j]; spaces++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}

Try this.
for (int i = 0; i < arr.length; i++) {
for (int j = 0, p = -1; j < arr[i].length; p = arr[i][j++])
System.out.print(" ".repeat(arr[i][j] - p - 1) + "*");
System.out.println();
}
output:
* * * **** **** * *
* * * * * * * * * *
***** * * * * * * *
* * * * **** **** *
* * ***** * * *
* * * * * * *

Related

Having problem in printing butterfly pattern with stars in java

public static void butterFly(int n){
//Outer loop
for(int i = 1; i <= n; i++){
// star - i
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
//Spaces - 2 * (n - i)
for(int j = 1; j <= 2*(n-i); j++){
System.out.print(" ");
}
//Stars - i
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
System.out.println();
}
// 2nd half
//Outer loop
for(int i = n; i >= 1; i--){
// star - i
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
//Spaces - 2 * (n - i)
for(int j = 1; j <= 2*(n-i); j++){
System.out.print(" ");
}
//Stars - i
for(int j = 1; j <= i; j++){
System.out.print("* ");
}
System.out.println();
}
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
I was expecting this
* *
** **
*** ***
********
*** ***
** **
* *
I have breaked my code into 2 parts 1st half of butterfly and second half of butterfly .
For print stars i have taken the value j =i where i is less then equals to n which is user input.
for printing spaces i have taken the j <= 2*(n-i).
and for the 2nd part i just reversed the outer loop.
You should remove spaces after *, i.e., System.out.print("* "); should be System.out.print("*");
Also, if you do not want the middle row to be duplicated, start the second half from n - 1, not n.
//Outer loop
for(int i = 1; i <= n; i++){
// star - i
for(int j = 1; j <= i; j++){
System.out.print("*");
}
//Spaces - 2 * (n - i)
for(int j = 1; j <= 2*(n-i); j++){
System.out.print(" ");
}
//Stars - i
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}
// 2nd half
//Outer loop
for(int i = n - 1; i >= 1; i--){
// star - i
for(int j = 1; j <= i; j++){
System.out.print("*");
}
//Spaces - 2 * (n - i)
for(int j = 1; j <= 2*(n-i); j++){
System.out.print(" ");
}
//Stars - i
for(int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println();
}

java array to print square box of asterisk

I'm trying to write a square shape of asterisk around border using the https://stackoverflow.com/a/34209565 answer.
But I cannot get it working at all.
Here is the code I'm trying.
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
System.out.println();
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i - 1|| j == _j - 1){
array[i][j] = "*";
System.out.print(array[i][j]);
}
}
}
The output I'm getting is:
**********
**
**
**
**
**
**
**
**
**********
I tried running tthe code from the answer but it produces a one line of asterisk. Something from the code in the answer has been omitted.
The code provdied in the answer is:
int _i = 10;
int _j = 10;
String[][] array = new String[_i][_j];
for (int i = 0; i < _i; i++) {
for (int j = 0; j < _j; j++) {
if(i==0 || j == 0 || i == _i-1|| j == _j-1){
array[i][j] = "*";
}
}
}
And the output in the answer is:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You need an else to print a space when the entry is not a star. Also, I would move the println() to the end of the loop (instead of the beginning). Assuming you actually want to fill the array too, populate it with a space as well. Like,
for (int j = 0; j < _j; j++) {
if (i == 0 || j == 0 || i == _i - 1 || j == _j - 1) {
array[i][j] = "*";
} else {
array[i][j] = " ";
}
System.out.print(array[i][j]);
}
System.out.println();
Outputs
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
You can use the repeat method, so the code is less verbose, also enclosing the generation in a method you can make it dynamic bu setting two arguments: number of lines and number of columns:
class Test
{
public static void main(String[] args)
{
System.out.println(createBox(10, 10));
System.out.println(createBox(10, 5));
}
public static String createBox(int qtaRig, int qtaCol)
{
String result = "";
for(int r = 1; r <= qtaRig; r++)
{
if(r == 1 || r == qtaRig)
{
result += "*".repeat(qtaCol);
}
else
{
result += "*" + " ".repeat(qtaCol-2) + "*";
}
result += "\n";
}
return result;
}
}
Result:
**********
* *
* *
* *
* *
* *
* *
* *
* *
**********
*****
* *
* *
* *
* *
* *
* *
* *
* *
*****

Printing a * pattern

So I have to print the following pattern by accepting a value of n
Input : 7
Output has to be this :
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
code:
public static void printPattern(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.println("*");
}
System.out.println("\n");
}
for (int a = n-1; a >= 1; a--)
{
for (int b = 1; b <= a; b++)
{
System.out.print("*");
}
System.out.println("\n");
}
}
But for some reason it prints this pattern(say n=8):
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*******
******
*****
****
***
**
*
What is the mistake here?
Use System.out.print instead of System.out.println in the first for loop, the latter one always appends a newline character at the end which is what you are trying to do manually.
And either do System.out.print("\n"); OR System.out.println(""); to add the newline after the inner loop iteration.
System.out.println already adds a line break at the end, therefore System.out.println("\n") adds two line breaks.
The code can be condensed to a single double-for loop. The following routine accepts a single parameter which defines the maximum number of '*' on a single line:
/**
* #param width
* the maximum width of the pattern.
*/
public static void print(int width) {
for (int i = 1; i < 2 * width; ++i) {
for (int j = 0; j < width - Math.abs(width - i); ++j)
System.out.print("*");
System.out.println();
}
}
public static void main(String[] args) {
print(4);
}
Output:
*
**
***
****
***
**
*
first for loop should be like this,
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
System.out.println();
always move the cursor to a new line
in your code when we use System.out.println("\n"); this will move cursor to 2 lines
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int a = n - 1; a >= 1; a--) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
By changing println to print you won't go to another line after each * and remove the \n to not skip 2 lines because println it's already there

Printing Diamonds Java Output not exactly as required

I have developed a code to print a diamond in java. The code prints a diamond using * and "o" and the code is:
System.out.println("Diamond Height: " + DIAMOND_SIZE);
System.out.println("Output for: For Loop");
int noOfRows = DIAMOND_SIZE;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
//Initializing row with 1
int row = 1;
//Printing upper half of the diamond
for (int i = midRow; i > 0; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j = 1; j <= row; j++) {
System.out.print("* ");
}
System.out.println();
//Incrementing the row
row++;
}
//Printing lower half of the diamond
for (int i = 0; i <= midRow; i++) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
int mid = (row+1) / 2;
for (int j = row; j > 0; j--) {
if(i==0 && j==mid) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
}
System.out.println();
//Decrementing the row
row--;
}
}
The result I get from this is:
Diamond Height: 5
*
* *
* o *
* *
*
Diamond Height: 3
*
* o
*
But Im trying to get the following results:
Diamond Height: 5
*
* * *
* * o * *
* * *
*
Diamond Height: 3
*
* o *
*
What am I doing wrong? I have tried several things but nothing seems to help, Please help.
You can modify your inner loop logic when printing lower half of the diamond.
//Printing j *'s at the end of each row
int mid = (row+1) / 2;
for (int j = row; j > 0; j--)
{
if(i==0 && j==mid) System.out.print("o ");
else System.out.print("* ");
}

Pattern using looping

I have to make a loop that will read the user's input (let's say his input is 5) so the output should be as the following:-
*
**
***
****
*****
****
***
**
*
int x;
//input size of triangle from 1-20
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle from 1 to 20: ");
x = scanner.nextInt ();
for( int i = 0; i <= x; i++){
for(int j=0; j < i; j++){
System.out.print( " *");
}
System.out.println(" ");
I just don't know how to finish it by decreasing the pattern so I can form a full triangle.
example using ternary operator .increase j if x grater than i decrease otherwise
int x = 5, j=0;
for (int i = 0; i <= x*2; i++) {
j= (x>i)? ++j:--j; // u can use if else also
for (int y = 0; y < j; y++) {
System.out.print(" *");
}
System.out.println("");
}
output>>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
The following code prints the triangle, assuming the user enters 10
for (int i = 0; i < 10; i++){
for (int j = 0;j < i;j++ )
System.out.print('x');
System.out.println();
}
for (int i = 10; i > 0; i--){
for (int j = 0; j < i;j++)
System.out.print('x');
System.out.println();
}
In your code, you exclude the second for loop in which i is decremented.
import java.util.Scanner;
public class Triangle {
public static void printLine(int n) {
for(int i = 0; i < n; i++) System.out.print("*");
System.out.print("\n");
}
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter size of triangle");
int x = scanner.nextInt ();
for(int i = 0; i < x; i++) {
printLine(i);
}
for(int i = x; i > 0; i--) {
printLine(i);
}
}
}
The output :
Enter size of triangle
5
*
**
***
****
*****
****
***
**
*

Categories

Resources