Help with array assignment.

Here are the rules for the program...
(1) Create a function that displays a two dimensional array. The example uses a
35 X 35 array. If the stored value is 0, show “.”, otherwise show the stored
number. Hint: Use two dimensional array, nested loops, and if statement.
(2) In the main program, set the randomly selected starting locations for each
number /animal. The example uses number 1 through number 6.
(3) Move each number/animal so that an animal chases the next animal. For
example, animal # 1 chases animal # 2, animal # 2 chases animal # 3, and
animal # 6 (the last animal) chases animal # 1(the first animal). Hint: Use
modulus division, as well as if statements. You may choose the direction of
the movement to either four directions or eight directions.
(4) Repeat Number 3 at least 20 times. Show the trace of each animal’s
numbers. Hint: Use a loop. You may use system("CLS"); to clear the
screen, and system("pause"); to pause after each iteration.

-----------------------------------------------------------------------
This is what i have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
	int array[10][10] = { 0 };
	int i, j;

	array[1][1] = { 5 }; 
	array[8][8] = { 3 };
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			if (array[i][j] < 1)
				cout << '.';
			else
				cout << array[i][j];
			cout << " ";
		}
	cout << endl;
	}
}


I am not sure what i should do after i have created my field. I am trying to get number "5" and "3" to chase after each other for about 20 pause iterations. I made the field and about of animals(numbers) less than the actual assignment requirements for ease of understanding.
Last edited on
without a user name and password, the link you gave is worthless. Why don't you explain what you are trying to do?

What do you mean by "chase after each other?" If they are chasing each other, won't they just run into each other? Is one supposed to lead and the other chase? How far can each animal mover each turn? Is the leader moving randomly or trying to get away from the chaser?

You probably want to calculate the chaser's new position based on the relative location of the leader. Each time you move, you will have to calculate the chaser's new position and the leader's new position.
sorry, didnt realize that the link would do that to you. Ideally, this program is suppose to simulate an array that is a 35X35 grid that each point are represented with a ".". On this grid(field) we are supposed to place 6 random locations represented with a number 1-6. The program is meant to simulate movement and the numbers are supposed to move in each others direction...following one another. They are suppose to leave a trail behind them, the same as the number we assign on that specific point. We have to give them a 4 or 8 direction movement for about 20 frames.

I really want to understand this program and do it intuitively. Problem is my proffessor has never taught such a program to us. Ive done research online but found nothing useful. I was hoping someone with good understanding could possibly guide me.

(BTW i edited by that link. you should be able to see the program rules.)
Start by just getting a single animal to move. Choose a starting location, say the bottom left of the grid, and have it move up 5 times the right 5 times. Watch it move and make sure it does what you want it to.

What has to change if you move up or down? What has to change if you move left or right?

Something to think about (although not strictly needed for the program you have been assigned): How do you handle boundary conditions (try to move down when you're already on the bottom)?

When you get a single animal moving with non-random input, then try 2 animals with non-random input and make sure they work the way you want. Eventually I would try 3 animals with non-random starting points and figure out how they chase one another. (Two is too few because they will just run into each other in the middle. With 3 spread around the screen you might be able to watch them circle around the screen for a few iterations.) When you get 3 working, give them random starting points. Then you can jump up to the full grid and the full number of animals.
Alright that sounds like a good start actually. My problem is how do you get it to move. And every time an animal moves, it is suppose to clear the screen and refresh it. I honestly do not know how to do that. I thought about useing a loop on the entire program but that didnt really work.
Instead of (or at least in addition to) storing where the animal is in the array (lines 10 & 11), Store the coordinates of each animal in a separate variable. So, if animal3 = (2,2) moves to the right, its new locaton is (2, 3).

I don't think you need to store the entire 2-dimensional array in line 1. Just do your nested loops, and check each animal's coordinates. If they match the current loop values, print the animal's number. Otherwise, print ".".

When you get as second animal, I would suggest storing the animal coordinates in an array.

Topic archived. No new replies allowed.