Don't like this style? Click here to change it! blue.css
Lab 3: Bracketless walks
Oh no the [
and ]
keys are broken on your keyboard!
You have to complete the first part of this lab with no brackets ([
and ]
). You can
use exactly 4 brackets in the second part.
Random Walking in Space
- Allocate enough memory for 10
int
s. (use malloc
and free
)
- Initialize the 10
int
s to the value -10.
- Make a counter set to 1.
- Make an index variable.
- Let the index randomly walk up or down. If it makes it to -1 reset it to 9, if it makes it to 10 reset it
to 0.
- Increment the counter at each step and save that value into memory at spot
index
.
- When all spaces have a positive number then display the ten values, free the memory and exit the program.
Version 2: Copy paste that code, and change malloc
and
free
to new
and delete
(you can use 4 brackets for this).
A Sample:
- FIRST ROUND:
- ints : -10, -10, -10
- counter : 1
- index : 0
- ints: 1, -10, -10
- NEXT ROUND:
- random move (up)
- index: 1
- counter: 2
- ints: 1, 2, -10
- NEXT ROUND:
- random move (down)
- index: 0
- counter: 3
- ints: 3, 2, -10
- NEXT ROUND:
- random move (down)
- index: 2
- counter: 4
- ints: 3, 2, 4
- END PROGRAM
- Display
3, 2, 4