Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need assistance with program

Status
Not open for further replies.

DJMittens

Programmer
Dec 20, 2004
10
0
0
CA
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.

#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?
 
Well quote tags were better than nothing, but ideally...
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

> #define SIZE 800 /* Cannot be changed because of lack of implementation of C99
Or you use dynamic memory allocation - but that's an easy change for later, stick with what you have, it will be a bit easier to manage.

> scanf("%d", start_atoms);
You forgot the & in scanf calls (both of them)
Try
Code:
scanf("%d", &start_atoms);

> for (i = 0; i <= enter_atoms; i++)
This generates enter_atoms+1 atoms, which isn't quite what you said you wanted.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top