[SOLVED] How to make a diamond with progressing & rectracting numbers
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to make a diamond with progressing & rectracting numbers
I would like to make a diamond with centrally aligned progressing & rectracting numbers, more particularly:
Output: "Please enter height of diamond> "
Input: "4"
Output:
<please see attached picture: DiamondCountingUpThenDown.png>
Currently on the web programiz offers the closest version of this, but still after completing their C Programming Code To Create Pyramid and Structure webpage (<http://www.programiz.com/article/c-programming-pattern>) I still write the below failing program. Note well: "//" and" /* ...*/" are comments.
#include <stdio.h>
int
main()
{
int floor, basement, space, height, apartment=0, countup=0, countdown=0;
/* floor = the floor number, counting from top;
height = height of pyramid(rows);
apartment = field #, assumes pyramid shaped apartment building has same apt # on every floor;
countup = field # before apartment reaches one less than twice the floor number;
countdown = field # starting when apartment number equals one less than twice the floor number
*/
printf("Enter height of pyramid(rows): ");
scanf("%d",&height);
for(floor=1;floor<=height;++floor)
{
for(space=1;space<=height-floor;++space)
{
printf(" ");
++countup;
}
while(apartment!=2*floor-1)
{
if (countup<=height-1) //問題:This line is totally illogical, what function does it serve?
{
printf("%d ",apartment+1);
++countup;
}
else
{
printf("%d ",(apartment-(2*countdown+1))); //Counting down: apartment # is one less than [apt # - (2*countdown#) ]
++countdown;
}
++apartment; // while's loop control variable(prevents infinite loop).
}
countdown=countup=apartment=0;
printf("\n");
}
I don't think the "apartment complex" analogy helps with understanding this problem.
I would rather use simple variables like "row", "column", "height" and "spaces" (and maybe a loop index counter like "i").
For( ) loops are all that are needed (mixing for's and while's confuses things) and you don't need "countup" and "countdown" variables.
Printing a single row should be made a function.
Clearly, your error is in the "basements" section.
I actually have a program written in basic that does what you want if you want to compare.
grail,
Re: Please place the code in tags so it is more readable and no interpretation into smiley faces and the like.
Attempt 01:
Code:
#include <stdio.h>
int
main()
{
int floor, basement, space, height, apartment=0, countup=0, countdown=0;
/* floor = the floor number, counting from top;
height = height of pyramid(rows);
apartment = field #, assumes pyramid shaped apartment building has same apt # on every floor;
countup = field # before apartment reaches one less than twice the floor number;
countdown = field # starting when apartment number equals one less than twice the floor number
*/
printf("Enter height of pyramid(rows): ");
scanf("%d",&height);
for(floor=1;floor<=height;++floor)
{
for(space=1;space<=height-floor;++space)
{
printf(" ");
++countup;
}
while(apartment!=2*floor-1)
{
if (countup<=height-1) //問題:This line is totally illogical, what function does it serve?
{
printf("%d ",apartment+1);
++countup;
}
else
{
printf("%d ",(apartment-(2*countdown+1))); //Counting down: apartment # is one less than [apt # - (2*countdown#) ]
++countdown;
}
++apartment; // while's loop control variable(prevents infinite loop).
}
countdown=countup=apartment=0;
printf("\n");
}
//Basement levels
//Initialization
//max_space = 1;
countup=countdown=0;
for (basement = height-1; basement >= 1; --basement) //Basement doesn't start at ground level(basement <=height), rather at -1 level, thus "<= height - 1".
{
for (space = 1; space <= basement; ++space)
printf(" ");
++countup;
//max_space++;
while (apartment!=2*basement-1)
{
if(countup<=(height-basement)-1)
{
printf("%d",apartment+1);
++countup;
}
else
{
printf("%d",(apartment-(2*countdown+1))); //Counting down: apartment # is one less than [apt # - (2*countdown#) ]
++countdown;
}
++apartment; // while's loop control variable(prevents infinite loop).
}
countdown=countup=apartment=0;
printf("\n");
}
return 0;
}
I don't think the "apartment complex" analogy helps with understanding this problem.
I would rather use simple variables like "row", "column", "height" and "spaces" (and maybe a loop index counter like "i").
For( ) loops are all that are needed (mixing for's and while's confuses things) and you don't need "countup" and "countdown" variables.
I actually have a program written in basic that does what you want if you want to compare.
psion,
Yes, actually column is the word I was looking for, this is much clearer than apartment number.
I would like to see your basic program for comparison.
Here it is. Note that lines 45, 505 and 550 were only necessary because in Blassic, FOR loops always execute at least once. You would not need them in C code.
Code:
10 INPUT "Height of diamond: ",height
20 FOR row = 1 TO height
30 GOSUB 500
40 NEXT row
45 IF height = 1 THEN 80
50 FOR row = height-1 TO 1 STEP -1
60 GOSUB 500
70 NEXT row
80 END
500 spaces = height - row
505 IF spaces = 0 THEN 520
510 FOR i = 1 TO spaces : PRINT " "; : NEXT i
520 FOR column = 1 TO row
530 PRINT column; " ";
540 NEXT column
550 IF row = 1 THEN 590
560 FOR column = row-1 TO 1 STEP -1
570 PRINT column; " ";
580 NEXT column
590 PRINT
600 RETURN
It took me 10 minutes to convert this code into C.
Everyone,
I would like to thank all of you, each person gave me a better idea and I now hereby share my solution to the problem below:
Code:
#include <stdio.h>
int
main()
{
int row, space, height, column, countup, countdown;
countup=countdown=column=1;
/* row = the row number, counting from top;
height = height of pyramid(rows);
column = field #, assumes pyramid shaped column building has same apt # on every row;
countup = field # before column reaches one less than twice the row number;
countdown = field # starting when column number equals one less than twice the row number
*/
while(1){ //Under the condition that the program runs without error, this loop will enable the user to experiment repeatedly for unlimited multiple times rather than having to exit terminal and then restart the program n times for n runs.
printf("Enter height of pyramid(rows): ");
scanf("%d",&height);
for(row=1;row<=height;++row) //Row Control
{
for(space=1;space<=height-row;++space)
{
printf(" ");
++countup;
}
for(countup=1;column!=2*row;++countup) /* We always want [2(row)-1] numbers printed given any specific row, and since column starts at 1 (line 7), we want it to stop at [2(row)-1] OR before (2*row).*/
{
if (countup<=row) /*Notice the example given how the max in each row is the row number. {given that the row with the highest number(height) is the top row and the lowest number row is on the bottom. }*/
{
printf("%2d ",(countup));
} //ends print countup if loop
else
{
printf("%2d ",(countup-(2*countdown))); //Counting down: Displayed number = countup number - twice the countdown number.
++countdown;
} //ends countdown else loop
++column; //"print number" for(line 26)'s loop control variable (prevents infinite loop.)
}
countup=countdown=column=1;
printf("\n");
}
//Bottom rows (bottom half)______________________________________________________
for(row=height-1;row>=1;--row) //Row Control: Since the top has already included the bottom and broadest row, thus we want to skip the broadest row on the top, thus instead of starting from row=height which is the broadest row, we start from the second broadest row, which is height-1.
{
for(space=1; space<=height-row; ++space) /* Since: 1)row starts from the height's number counting down(line11), 2) upside-down means 0 spaces on the first line and (height - 1) spaces on the last line therefore we write the condition: a) start at 1 b) height - row(from 5 down to 1)*/
{
printf(" ");
++countup;
} //ends space loop
for(countup=1;column!=2*row;++countup) /* We always want [2(row)-1] numbers printed given any specific row, and since column starts at 1 (line 7), we want it to stop at [2(row)-1] OR before (2*row).*/
{
if (countup<=row) /*Notice the example given how the max in each row is the row number. {given that the row with the highest number(height) is the top row and the lowest number row is on the bottom. }*/
{
printf("%2d ", countup);
} //ends print countup if loop
else
{
printf("%2d ",(countup-(2*countdown))); //Counting down: Displayed number = countup number - twice the countdown number.
++countdown;
} //ends countdown else loop
++column; //"print number" for(line 50)'s loop control variable (prevents infinite loop.)
} //ends "print number" for loop
countdown=countup=column=1;
printf("\n");
} //ends "basement" for loop
printf("\n");
} //ends while loop
return 0;
}
Thanks!!!
Andrew
Last edited by andrew.comly; 11-28-2014 at 07:42 PM.
Reason: terms, comments
Yes ... was this problem shown in Basic BASIC, or in Advanced BASIC? I guess I could go into the other room and look it up, but ...
Hmmm... In the BASIC interpreters that I've used, FOR-loops didn't execute "at least once" if the range was empty. They properly considered the values of the limit-expressions and the sign of the STEP value, and behaved properly in all cases. Did, say, Microsoft put out an interpreter that was broken in that way?
(The very-strangest BASIC interpreter that I ever saw, which honest-to-god was written entirely in COBOL, ... stack of bibles hope-to-die true statement ... also did not have this problem.)
Last edited by sundialsvcs; 12-02-2014 at 07:20 AM.
Hmmm... In the BASIC interpreters that I've used, FOR-loops didn't execute "at least once" if the range was empty.
Code:
$ blassic
Blassic 0.10.2
(C) 2001-2009 Julian Albo
Ok
10 n = 0
20 for i = 1 to n : print i : next
run
1
Ok
There is no universal standard with something as antique as BASIC and you can't make any assumptions about what an individual programmer will do. BASIC (like FORTH) is simple enough for a programmer of moderate ability to "roll his own" so anything goes.
Quote:
Originally Posted by rtmistler
Forgot to mention that I LOVE the fact that psionl0 posted an actual BASIC program.
Posted by request. I just happened to have that code lying around although it doesn't seem to have inspired the OP to clean up his code much (although he did away with "apartment number").
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.