How to input Diamond like this
input : 5
output :
![]()
How to input Diamond like this
input : 5
output :
![]()
Are you sure you mean input, not output?
To input, get atext editor, set the font to monspaced, and type in the pattern.
The pass the name of the file on the commandline, open it with fopen(argv[1], "r"),
read each line with fgets(), and close with fclose();
If you want to generate a a pattern line that, which is what I think that
you really want to do, most likely, the rules are that if a point is
on the edge of the diamond, or on a vertical or horizontal centreline, is
is a "O", else it is a ".". So loop in y and in x over the square. Then you'll
need some logic to decide whether to write an "O" or not. You can experiment
with different approaches until it comes right.
I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
Visit my website for lots of associated C programming resources.
https://github.com/MalcolmMcLean
You start by breaking the problem down into simpler problems.
Like for example, by trying to draw a cross and then an empty diamond
Code:....0..... ....0..... ....0..... ....0..... 000000000. ....0..... ....0..... ....0..... ....0..... ....0..... ...0.0.... ..0...0... .0.....0.. 0.......0. .0.....0.. ..0...0... ...0.0.... ....0.....
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
start with fibonnaci numbers/positioning and improve diamond from there
you tell me you can C,why dont you C your own bugs?
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> // maxDiamondSize = (maxLength/2) #define maxLength 256 #define marker 'O' #define spacer '.' void drawDiamond(int size); int main(int varc, char *argv[]) { //int diamondSize = atoi(argv[1]); //drawDiamond( diamondSize ); drawDiamond(5); return 0; } void drawDiamond(int size) { int count = 0, msize = (size), csize = (size-1); char lineData[maxLength]; char lines[maxLength][maxLength]; while (size > 0) { lineData[0] = '\0'; if (count != (msize-1)) { for (int i=0;i<(msize*2);i++) lineData[i] = spacer; lineData[csize-count] = marker; lineData[csize] = marker; lineData[csize+count] = marker; lineData[msize*2] = '\0'; } else { for (int i=0;i<((msize*2)-1);i++) lineData[i] = marker; } strcpy(lines[count],lineData); size--; count++; } for (int i=0; i<(msize-1); i++) printf("%s\n",lines[i]); for (int i=(msize-1); i>(-1); i--) printf("%s\n",lines[i]); }
![]()
"without goto we would be wtf'd"
So instead of learning how to solve a problem, the OP has learnt that eventually, some schmuck will do their work for them.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.