2d array help

closed account (9Nh5Djzh)
Hi there! I am trying to write a program that generates 6 random lines in an 80X20 2d array.
I keep getting the error "macro "arc4random_uniform" passed 1 arguments, but takes just 0", can someone tell me how to fix this please?
Thanks


#include <cstring>
#include <iostream>
#include <cstdlib> //arc4random_uniform

#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define arc4random_uniform()
#define nRows 20
#define nCols 80

int main(int argc, char *argv[])
{
// fill the sky with spaces
char sky[nRows][nCols];
memset(sky, '.', nRows * nCols);


for (int i = 0; i < 6; ++i)
{
int x1, y1, x2, y2;

// choose points on the perimeter.
// if x == 0 or nCols - 1, the y can be anything.
// if x is between 0 and yCols, y MUST BE BOTTOM (0) or TOP nRows - 1
x1 = arc4random_uniform(nCols);
if (x1 == 0 || x1 == nCols - 1)
y1 = arc4random_uniform(nRows);
else
y1 = (arc4random_uniform(2) ? 0 : nRows - 1);

x2 = arc4random_uniform(nCols);
if (x2 == 0 || x2 == nCols - 1)
y2 = arc4random_uniform(nRows);
else
y2 = (arc4random_uniform(2) ? 0 : nRows - 1);

std::cout << "(" << x1 << "," << y1 << ") --> (" << x2 << "," << y2 << ")" << std::endl;

// convert to y = mx + b
float m = (1.0 * y2 - y1) / (x2 - x1);
float b = y1 - m * x1;

int start = MIN(x1, x2) + 1;
int end = MAX(x1, x2);

// put a star on the two end points obviously!
sky[y1][x1] = '*';
sky[y2][x2] = '*';

// fill in, in the x direction
for (int j = start; j < end; ++j)
{
int y = m * j + b;
sky[y][j] = '*';
}

start = MIN(y1, y2) + 1;
end = MAX(y1, y2);
// fill in in the y direction
for (int j = start; j < end; ++j)
{
int x = (j - b) / m;
sky[j][x] = '*';
}
}

// finally, just print the contents of sky!
std::cout << std::endl;
for (int row = 0; row < nRows; ++row)
{
for (int col = 0; col < nCols; ++col)
std::cout << sky[nRows - row - 1][col];

std::cout << std::endl;
}

return 0;
}
Last edited on
#define arc4random_uniform() Macro which takes 0 parameters.
arc4random_uniform(2) And you passed 1 into it.
Solution: do not pass parameters into it or rewrite your macro to make sense.
Even better: do not use macros. The are evil. Inline functions are better in every way.
#define MIN(a,b) ((a)<(b)?(a):(b)) Use std::min()

#define nRows 20 const int nRows =20; (or constexpr int nRows =20;)
Last edited on
Im also working on a very similar assignment and Im fairly new to C++ i dont understand the use of macros? is there a way of getting this program to work with out the use of them?
Topic archived. No new replies allowed.