Thread: Diamond input in c

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    6

    Diamond input in c

    How to input Diamond like this

    input : 5

    output :
    Diamond input in c-screen-shot-2020-07-29-17-29-06-jpg

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    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


  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101
    start with fibonnaci numbers/positioning and improve diamond from there
    you tell me you can C,why dont you C your own bugs?

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    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]);
    }
    Diamond input in c-diam-png

    "without goto we would be wtf'd"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    some schmuck

    Diamond input in c-anus-jpg
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Diamond
    By C_Nik in forum C Programming
    Replies: 3
    Last Post: 12-06-2012, 10:52 AM
  2. Diamond diplay
    By Chiefsmokin in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2010, 04:45 AM
  3. drawing diamond
    By bluestar in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2003, 07:57 AM
  4. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM
  5. a diamond
    By wagman in forum C Programming
    Replies: 5
    Last Post: 10-04-2001, 03:48 PM

Tags for this Thread