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:
* * * **** **** * *
* * * * * * * * * *
***** * * * * * * *
* * * * **** **** *
* * ***** * * *
* * * * * * *
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();
}
Can someone help me solve the bellow?
I have attached two codes. Both should be identical from my comparison. One of them prints almost normal Xmas Tree (Code 2/Result Code 2) and the other one (Code 1/Result Code 1) prints only the right side of the tree. I do not understand why and how is this possible....
Also, can someone explain to me the process of how the code works? I understand it only a little bit. Still learning and I copied Code 2 and tried to replicated manually with Code 1.
Code 1:
public class XmasTree {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How tall do you want it?");
int height = input.nextInt();
for (int i = 0; i < height; i++) {
for (int j = 0; j < height -i; j++);{
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
for (int i = 0; i <= height; i++){
for (int j = 0; j <= height; j++){
System.out.print(" ");
}
for (int k = 1; k < 2; k++){
System.out.print("*");
}
System.out.println();
}
input.close();
}
}
Code 2:
public class XmasTree2 {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("How tall do you want your tree to be?");
int height = input.nextInt();
for(int i = 0; i < height; i++){
for(int j = 0; j < height - i; j++){
System.out.print(" ");
}
for(int k = 0; k <= i; k++){
System.out.print("* ");
}
System.out.println();
}
for(int i = 0; i <= height; i++){
for(int j = 0; j <= height; j++){
System.out.print(" ");
}
for(int k = 1; k < 2; k++){
System.out.print("*");
}
System.out.println();
}
input.close();
}
}
Result Code 1:
How tall do you want it?
4
*
* *
* * *
* * * *
*
*
*
*
*
Result Code 2:
How tall do you want your tree to be?
4
*
* *
* * *
* * * *
*
*
*
*
*
You have an extra ; after your second for-loop.
Do you want to know how I discovered that? I put your code in an IDE and let it auto-format it for me. That way the mistake becomes clear.
This is why correct formatting is so important.
I want to get this result, where _ space characters :
*___*
_*_*_
__*__
public static void main(String args[]) {
int level = 2; // quantity line
int stars = 5; //quantity drawing stars
for(int i = 1;i <= level ; i++){
for(int j =1 ;j <= i; j++){
System.out.print(" ");
}
System.out.println("*");
}
}
So far, I have drawn,
*__
_*_
__*
And I don't know how to draw up ?
Steps to solve these type of questions:
consider * as 1 and spaces as 0. Now i need this output :
10001
01010
00100
first 1 is appearing according to row no. Row 0 - 1 at Col 0, Row 1 - 1 at Col 1
Second 1 is appearing at (total columns-current Row index-1)
print 1 for above two condition otherwise zero.
int rows=3; // quantity line
int cols=5; //quantity drawing stars
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
int k=cols-i-1;
if(i==j || j==k)
System.out.print("*");
else System.out.print(" ");
}
System.out.println();
}
int size=10; // Only one parameter is required which is quantity drawing stars
int length= size%2==0?size/2:size/2+1; // in case of odd one more line need to be print at last on which one Asteric appears.
for (int i = 0; i < length; i++) {
for (int j = 0; j < size; j++) {
if (i == j || i + j == size - 1) { //condition for diagonals
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
Output :
when size = 10;
* *
* *
* *
* *
**
when size = 11
* *
* *
* *
* *
* *
*
You can try below code and output is what you want..
for(int i=3;i>=1;i--)
{
for(int j=i;j<3;j++)
{
System.out.print(" ");
}
for(int j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
My code works I just want to know what to do to get it to actually look like a diamond. Everything is just left justified like this:
*
* *
* * *
* * *
* *
*
But I want it to look like this:
*
* *
* * *
* * *
* *
*
public static void s3(int num, int len)
{
for(i = 1; i<=num; i++)
System.out.print(" " + "*");
System.out.print(" ");
System.out.println();
if(num < len)
s3(num + 1, len);
for (j = 1; j<= num; j++)
System.out.print(" " + "*");
System.out.print(" ");
System.out.println();
}
}
should I use print f to format or what? I've had a long day of frustrating programming and just need some help. Thanks!
The solution is not really a beauty, but it's 4am here and that's all I can provide for now ;)
About recursion you are very welcome to think how you can do it on your own.
public static void s3(int len) {
for (int i = 0; i <= len; i++) {
for (int j = len; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < len - (len - i); j++) {
System.out.print(" *");
}
System.out.println("");
}
for (int i = 0; i <= len; i++) {
for (int j = 0; j < (i); j++) {
System.out.print(" ");
}
for (int j = len; j > i; j--) {
System.out.print(" *");
}
System.out.println("");
}
}