Thread: Need assistance with program

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    Need assistance with program

    I've got a program written that seems to be set up fine. I can compile without any warnings or errors, but I get zero output. Nothing back from the system at all. It runs and then kills itself. If I had an error to work with, I might be able to figure it out, or at least some kind of output, but this is crazy.

    It's for a term project that, unfortunately, is due tomorrow. I've been spending the time tweaking the program, getting it to run with reasonable success, but now it won't do anything.

    Here is the assignment question:

    A simple model of diffusion and pattern formation can be made as follows.
    Imagine a grid of, say, 400 × 400 points upon which are scattered, say, 3000
    atoms randomly. Now have an atom enter the grid from some random point on
    the edge, and make a rule that the atom will randomly move around the grid
    until either it leaves the grid, or else it comes to a point which is immediately
    next to a point containing an atom, in which case it will stick to that point. 1
    Now have another atom enter the grid, and follow the same rule, and so on.
    Write a C program which will have, as configureable variables, the size (in
    pixels) of the (square) grid to consider, the initial number of atoms to scatter
    randomly about the grid, and the number of atoms that enter the grid, and
    which will then produce a png (or jpeg) image of the results, using the gd library.
    Include in your output the source file, and also 4–6 screen shots representing an
    interesting range of parameters.
    Below is what I've written.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include "gd.h"
    
    #define SIZE 800 /* Cannot be changed because of lack of implementation of C99 with free Borland compiler.  It can be modified by the user before compilation.*/
    #define TRUE 1
    #define FALSE 0
    
    void initialize (int start[][SIZE], int cells);  /* Creates array.*/
    void atom (int array[][SIZE]);  /* Moves the atom.*/
    int direction (int side);  /* Chooses direction of movement. */
    
    int main (void)
    {
        int start_atoms, enter_atoms;
        int i,x,y;
        int grid[SIZE][SIZE];
        int black, white;
        
        gdImagePtr im;
        FILE *pngout, *jpegout;
        
        /* Gets the number of atoms to scatter in the grid. */
        printf("\nPlease enter the number of starting atoms: ");
        scanf("%d", start_atoms);
        
        /* Gets the number of atoms to fire into the grid. */
        printf("\nPlease enter the number of atoms to be fired into the grid: ");
        scanf("%d", enter_atoms);
        
        initialize (grid, start_atoms);
        
        for (i = 0; i <= enter_atoms; i++)
        {
            atom (grid);
        }
        
        im = gdImageCreate(SIZE,SIZE);
        
        black = gdImageColorAllocate(im, 0, 0, 0);
        white = gdImageColorAllocate(im, 255, 255, 255);
        
        for (x = 0; x < SIZE; x++)
        {
            for (y = 0; y < SIZE; y++)
            {
                if (grid[x][y] == TRUE)
                    gdImageSetPixel (im, x, y, black);
                else gdImageSetPixel (im, x, y, white);
            }
        }
        
        jpegout = fopen("output.jpg", "wb");
        gdImageJpeg(im, jpegout, 0);
        fclose(jpegout);
        gdImageDestroy(im);
        
        printf("Image output.jpg written to the disk.");
        
        return 0;
    }
    
    
    void initialize (int start[][SIZE], int cells)  /* Creates an empty array, then populates with a random assortment of atoms in existence. */
    {
        int x, y, i=0;
        
        for (x = 0; x < SIZE; x++)
        {
            for (y = 0; y < SIZE; y++)
            {
                start[x][y] = 0;
            }
        }
    
        while (i <= cells)
        {
            int x = (rand() % (SIZE-10) + 5);  /* Creates the atoms in the middle of the map, leaving the borders empty, giving the new atoms a chance to get into the map and not block off the sides. */
            int y = (rand() % (SIZE-10) + 5);
            
            if (start[x][y] == FALSE)
            {
                start[x][y] = TRUE;
                i++;
            }
        }
    }
    
    
    void atom (int array[][SIZE])
    {
        /* Four sides: 0=top, 1=right, 2=bottom, 3=left.
         * When a given side is chosen to start from, the opposite side will be selected as the preferred direction of travel.
         * This function only controls the movement for a single atom.
         */
        
        int side = rand() % 4;
        int cont = TRUE;
        
        int x, y;
        
        /* Sets the starting point of the atom, given the starting side.*/
        switch (side)
        {
            case 0:
                x = rand() % SIZE;
                y = 0;
                break;
            
            case 1:
                x = SIZE - 1;
                y = rand() % SIZE;
                break;
            
            case 2:
                x = rand() % SIZE;
                y = SIZE - 1;
                break;
            
            case 3:
                x = 0;
                y = rand() % SIZE;
                break;
        }
        
        /* Moves the atom one cell at a time.
         * First it moves one space.
         * If it has left the grid, then the loop quits.
         * If it has not left the grid, then it checks for any neighbors.  If it has a neighbor, it sets the appropriate array space to TRUE.
         */
        
        while (cont == TRUE)
        {
            int dir = direction (side);
            /* Moves the atom based on the pseudo-random direction. */
            
            if (dir == 0) x -= 1;
            else if (dir == 1) y += 1;
            else if (dir == 2) x += 1;
            else y -= 1;
            
            /* If the atom has left the grid, the loop quits. */
            
            if (x < 0 || y < 0 || x >= SIZE || y >= SIZE) cont = FALSE;
            else if (array[x+1][y] == TRUE || array[x-1][y] == TRUE || array [x][y+1] == TRUE || array[x][y-1] == TRUE)
            {
                array[x][y] = TRUE;
                cont = FALSE;
            }
            
            /* If cont hasn't been changed, then the loop repeats and the atom moves again. */
            
        }
        
        /* Function will end with either the atom leaving the grid or getting attached to another atom. */
        
    }
    
    
    int direction (int side)
    {
        /* To set the propensity to move in a direction, the opposide side of the starting side needs to be selected.
         * Opposite side of entrance is given a 40% chance of being chosen, the other three sides get 20% chance each.
         */
        
        int propensity = (side + 2) % 4;
        
        int direction = rand() % 5;
        
        if (direction == 4)  /* If the direction happens to be 4, which is an invalid variable, it is reset to the direction of propensity, giving it the extra 20%. */
        {
            direction = propensity;
        }
        
        return direction;
    }
    What am I missing here? All I want is a direction to go in, and I'm kind of stumped. I'm going to try dumping the variable contents to a log file, but I only "kind of" know how to manipulate text files.

    Thanks.
    Last edited by DJ_Mittens; 04-19-2005 at 05:49 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I would suggest first just making a program to write a blank jpg/png file using the API you're given here. Make sure that works. Now do the same, but put a dot some place in the file. Make sure that works. Now scatter a random number of dots around and write that to a file. Make sure that works.

    Only after you're sure that all works would I bother trying to move from one side to the other.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    scanf() takes the address of a variable as it's parameter...i.e.:

    scanf("%d", start_atoms);
    becomes
    scanf("%d", &start_atoms);

    I noticed a few scanf() calls that were missing the ampersand sign. This allows the scanf() function to manipulate the contents of your variable.

    Adding a: getch(), getche(), or getchar(), I recommend getchar(), towards the end of your program will allow it to pause before closing the window so you can see what it output to your console.
    EDIT: Nevermind, I recommend getch()
    EDIT: No I don't, that's another lie. Read quzah's post below as to why.

    Give it a go with those scanf() calls and see what comes up...
    EDIT: Also, tossing in fflush(stdin); after a scanf() call is recommended so that any extra keys pressed don't ruin your next scanf() call.
    EDIT: Again, that's a lie.
    Last edited by Epo; 04-19-2005 at 08:14 PM. Reason: quzah reminded me not to be a moron
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Epo
    EDIT: Nevermind, I recommend getch()

    Give it a go with those scanf() calls and see what comes up...
    EDIT: Also, tossing in fflush(stdin); after a scanf() call is recommended so that any extra keys pressed don't ruin your next scanf() call.
    1) getch is a non-standard function that not all compilers have. Therefore, I definately wouldn't suggest using it.
    2) Also reading the FAQ will tell you why you're wrong to even think about using fflush( stdin ).

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Bah, what was I thinking? quzah couldn't be more right. I think a year of University programming's taught me some bad habits. I didn't know that about getch, but jeez, what was I thinking with fflush?

    I'm gonna go drink my problems away now
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM