I am making a program that would read the input (the coordinates of horizontal and vertical lines) like this:
struct Data {
// the beginning and ending coordinate of a line
int xStart;
int xEnd;
int yStart;
int yEnd;
};
// the dimension of panel on which you draw lines
int panelWidth, panelHeight;
// number of lines drawn on the panel
int numLines;
scanf("%d %d", &panelWidth, &panelHeight);
struct Data data[5];
int j;
int i;
j = 0;
if (scanf("%s", &numLines) == 1)
{
for (i = 0 ; ((i < numLines) && (j < 5)) ; ++i)
{
if (scanf("%d %d %d %d",
&data[j].xStart,
&data[j].yStart,
&data[j].xEnd,
&data[j].yEnd) == 4)
{
j++;
}
}
}
Now I would like to put these values in an array that would represent the panel and draw all the lines on the panel (e.g. put the value 1 where there is a line and 0 where the cells are empty), so that I can then count all the zones that these lines make (spaces with zeros).
It should work like flood fill.
How do I make such array? Or is there a better option?
This is an example of a more complicated input:
27 27 // width, height
7 // number of Lines
10 0 10 10 // xStart, xEnd, yStart, yEnd
0 10 10 10 // xStart, xEnd ...
5 5 15 5
15 5 15 15
5 15 21 15
5 5 5 15
5 20 26 20
Is your question correctly condensed to 'how do I make an array of a certain size in C'?
– Jongware
I'm guessing that Jongware is on to something, and that you are trying to dynamically allocate an n x m array. In other words, you're hoping to make an array that has its size determined by what the user types in.
To do this, you have to use 'the heap'. If you haven't done dynamic memory allocation in C before, I recommend just making a large array (say 10,000 elements) and only using whatever you need. Then you can just make sure the user can't enter anything that would be bigger than that.
If you really want to do it dynamically, we'll be using your panelWidth
and panelHeight
variables. In fact, there is a nice post on this right over here on StackOverflow:
Hopefully one of those methods is what you're looking for. If not, it wouldn't hurt to clarify what exactly you're asking. You gave lots of information, but not everything is really relevant. I think Jongware has summarized your question well.