Thread: a diamond

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    a diamond

    We have to create a diamond that is centered on the screen using this code that creates a triangle. My thought process has been to use 4 right triangles to create the diamond. What im stuck on is trying to make the triangle increase in the opposite direction. Is this the way to go or is there a better way? thx for any advice.


    #include<ctype.h>
    #include<stdio.h>
    #define N 33
    void repeat(char, int);

    int main(void)
    {
    char c = 'X';
    int i;
    for (i = 1;i <= N; i+=2) {
    repeat(c, i);
    putchar('\n');

    }

    }

    void repeat(char c, int how_many)
    {
    int i;

    for (i = 1; i <= how_many; ++i)
    putchar(c);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i = 1;i <= N; i+=2) {
    Perhaps
    for (i = N;i >= 1; i-=2) {
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    > for (i = 1;i <= N; i+=2) {
    Perhaps
    for (i = N;i >= 1; i-=2) {

    right, but im stuck on trying to get the triangles to go in the opposite direction so as to complete the diamond. so far i have the right half but how could i mirror this?
    Last edited by wagman; 10-04-2001 at 03:24 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start by printing N-i spaces on a line, followed by i 'X's
    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.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    I don't understand what you mean by
    N-i spaces on a line" ?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    Talking

    nm..got it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  2. Bitwise Unwanted Output
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2008, 04:07 AM
  3. Diamond loopy
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2007, 09:10 AM
  4. making a diamond using functions
    By Nikisha in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2003, 09:05 PM
  5. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM