Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2005, 5:54 PM   #1
DJ_Mittens
Newbie
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0 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.

Quote:
#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?
DJ_Mittens is offline   Reply With Quote
Old Apr 19th, 2005, 6:30 PM   #2
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
i don't have time to help you much, but let me suggest the following.

every once in a while, write a status line to a flat text file. then, execute the program. read the output file to figure out how far it got.

this is a good diagnostic tool and i think it'll get you an answer in short order. just output a line number and the values of a few variables followed by a carriage return.
brkstf is offline   Reply With Quote
Old Apr 19th, 2005, 7:02 PM   #3
DJ_Mittens
Newbie
 
Join Date: Apr 2005
Posts: 3
Rep Power: 0 DJ_Mittens is on a distinguished road
I tried it. I tried dumping my main function right at the beginning, and it wouldn't even do that. I think it has something to do with the function declaration at the beginning, but since they aren't called until later, I can't dump them either.

I admit, I'm not good at creating debugging scripts, but the program seems to die before it even gets going. Like it's something wrong with one of the libraries I included that won't let it execute. I have no clue, really, especially since it compiles perfectly.
DJ_Mittens is offline   Reply With Quote
Old Apr 20th, 2005, 2:53 AM   #4
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
sorry i cant help i cant even get it to compile, i think we need your custom header file to check it easier
Berto is offline   Reply With Quote
Old Apr 20th, 2005, 7:48 AM   #5
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
scanf requires the address of the variable you're reading into

&start_atoms
&enter_atoms

Also, you should you srand() to seed the random number generator, or you'll always get the same output.

a common solution is to include ctime.h and use time(NULL)
srand(time(NULL));

Last edited by spydoor; Apr 20th, 2005 at 7:54 AM.
spydoor is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC