GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Apr-2005, 17:00
DJ_Mittens DJ_Mittens is offline
New Member
 
Join Date: Apr 2005
Posts: 4
DJ_Mittens is on a distinguished road

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:
Quote:
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.

CPP / C++ / C 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?
  #2  
Old 19-Apr-2005, 18:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by DJ_Mittens
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.

What am I missing here?


The very first thing you should (always) do is to make sure the program is working on what you think it is. That is, print out the values of all user input variables.

As I look at the beginning of your program, I see errors in scanf. scanf takes addresses. So you could try this, for starters:

CPP / C++ / C Code:
    /* Gets the number of atoms to scatter in the grid. */
    printf("\nPlease enter the number of starting atoms: ");
    scanf("%d", &start_atoms);
    printf("You entered %d\n", 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);
    printf("You entered %d\n", enter_atoms);
    

Now, as program execution progresses, put printf at strategic points to make sure function arguments are being passed correctly, formulas are working on appropriate values, etc.

Regards,

Dave
  #3  
Old 19-Apr-2005, 18:42
DJ_Mittens DJ_Mittens is offline
New Member
 
Join Date: Apr 2005
Posts: 4
DJ_Mittens is on a distinguished road
I appreciate the idea, but the program doesn't even ask for input. I run it from the command line, and it just returns me back to the command line. The program doesn't even execute far enough to get any values from the user, so it's even earlier than that.

As far as the addresses go, I just entered that in for the heck of it, to see if I would get any errors or not, and again, nothing. No compilation errors, no running errors. I get zero output.

Thanks for looking at it, at the least.
  #4  
Old 19-Apr-2005, 20:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by DJ_Mittens
I appreciate the idea, but the program doesn't even ask for input. I run it from the command line, and it just returns me back to the command line. The program doesn't even execute far enough to get any values from the user, so it's even earlier than that.

As far as the addresses go, I just entered that in for the heck of it, to see if I would get any errors or not, and again, nothing. No compilation errors, no running errors. I get zero output.

Thanks for looking at it, at the least.

If you compiled and linked that program with no errors and it didn't run, then there is something seriously wrong with your setup. There's nothing anyone somewhere else can do to help you.

Maybe you can just comment out the rest of the program after the input lines and see if it does anything:

CPP / C++ / C Code:
#include <stdio.h>
int main()
{
/* Gets the number of atoms to scatter in the grid. */
    printf("\nPlease enter the number of starting atoms: ");
    scanf("%d", &start_atoms);
    printf("You entered %d\n", 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);
    printf("You entered %d\n", enter_atoms);
    return 0;
}

If this compiles and links and executes OK, then add other parts and see what happens.

Good luck!

Regards,

Dave
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 13:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 20:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.