Thread: Christmas tree with lights and balls

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    1

    Christmas tree with lights and balls

    Hi everyone,

    In Programming practicum we were given an assignment to make a program which displays a christmas tree with randomly drawn "+" and "o" which represent lights and balls. The result should look like this:

    Christmas tree with lights and balls-2cetf8x-png

    This is my code for the christmas tree itself, but I am struggling with the lights and balls. Maybe someone can help me out?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
        int ROWS, starNumber, spaceNumber;
        int rowCount, spaceCount, starCount, treeTrunkRows, treeTrunkSpaceCount;
    
        printf ("Enter number of rows: ");
        scanf ("%d", &ROWS );
        printf("\n");
    
        for(rowCount = 1; rowCount <= ROWS; rowCount++) {
            starNumber = rowCount * 2 - 1;
            spaceNumber = rowCount + ROWS - starNumber;
    
            for(spaceCount = 0; spaceCount < spaceNumber; spaceCount++)
                printf(" ");
    
            for(starCount = 0; starCount < starNumber; starCount++)
                printf("*");
    
            printf("\n");
        }
    
        for(treeTrunkRows = 0; treeTrunkRows < 2; treeTrunkRows++) {
            for(treeTrunkSpaceCount = 0; treeTrunkSpaceCount < (ROWS * 2 + 1)/2; treeTrunkSpaceCount++)
                printf(" ");
                printf("*\n");
        }
    }
    Last edited by Martin Pelov; 09-14-2015 at 03:59 AM.

  2. #2
    Registered User
    Join Date
    May 2013
    Posts
    228
    Suppose you want to generate 'light' with probability p, 'bulb' with probability q, and 'star' with probability 1-p-q.
    At line 22 in your code, instead of printing '*', you could use rand() (or other auxiliary function) to generate number x in the range [0,M], now if 0 <= x < p*M then print 'light', else if p*M <= x < (p+q)*M then print 'bulb', else print 'star'.
    Notice that this assumes you are uniformly sampling from [0,M], which is not always true for rand(), if I am not mistaken.
    Last edited by Absurd; 09-14-2015 at 03:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Draw a Christmas tree
    By Manicman in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2012, 04:26 AM
  2. christmas tree in c programming using loops.
    By poojaraghoo in forum C Programming
    Replies: 3
    Last Post: 10-09-2011, 02:29 PM
  3. Bouncing balls C++
    By darkowl in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2010, 03:29 PM
  4. looking for advice on selling golf balls
    By lambs4 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-30-2004, 04:03 PM