[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1430

# Number Range within an array

Should be an easy question, I'll provide an example that's not working and maybe you guys can help me solve the issue or provide me with a better solution.

``````/*
Base1X - Base5X = Some number all ready chosen.
Base1Y - Base5Y = Some number all ready chosen.

so maybe Base1X = 45, and Base2X = 455, something like that.
*/
int StructX[5]={Base1X,Base2X,Base3X,Base4X,Base5X};

int StructY[5]={Base1Y,Base2Y,Base3Y,Base4Y,Base5Y};

for (int NumCount = 0; NumCount <= 6; NumCount ++)
{&#9;&#9;
&#9;for (int BaseX = StructX[NumCount] - 250;
&#9;       BaseX <= StructX[NumCount] + 250;
&#9;       BaseX++)
&#9;{
&#9;for (int BaseY = StructY[NumCount] - 65;
&#9;       BaseY <= StructY[NumCount] + 35;
&#9;       BaseY++)
&#9;{
if(Base1X== BaseX )
&#9;&#9;{
&#9;&#9;&#9;if(Base1Y== BaseY)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;//Count +1 if successful
&#9;&#9;&#9;}
&#9;&#9;}
if(Base2X== BaseX )
&#9;&#9;{
&#9;&#9;&#9;if(Base2Y== BaseY)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;//Count +1 if successful
&#9;&#9;&#9;}
&#9;&#9;}
if(Base3X== BaseX )
&#9;&#9;{
&#9;&#9;&#9;if(Base3Y== BaseY)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;//Count +1 if successful
&#9;&#9;&#9;}
&#9;&#9;}
if(Base4X== BaseX )
&#9;&#9;{
&#9;&#9;&#9;if(Base4Y== BaseY)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;//Count +1 if successful
&#9;&#9;&#9;}
&#9;&#9;}
if(Base5X== BaseX )
&#9;&#9;{
&#9;&#9;&#9;if(Base5Y== BaseY)
&#9;&#9;&#9;{
&#9;&#9;&#9;&#9;//Count +1 if successful
&#9;&#9;&#9;}
&#9;&#9;}
&#9;}
&#9;}
}
``````

What my goal is, is to go through the whole array from 1-5 and do the code within.

So i want NumCount to be 1-5. Which will make "StructX[NumCount]" go through 1-5 within its array and do the code within successful.

The code inside, its goal is to count how many times a number is within BaseX's range and BaseY's range and +1 each time.
0
RevJoker
2 Solutions

Senior Software Engineer (Avast)Commented:
C style arrays have a zero index base. Your for loop should be like this...

for (size_t NumCount = 0; NumCount < 5; ++NumCount )

NB. Notice I use size_t and not int, this is the correct type to use when indexing arrays. Also, note I use pre and not post increment - it's more efficient.

If that doesn't answer your question you'll need to elaborate I'm afraid as your actual problem isn't clear.
0

Author Commented:
It may answer my question I haven't checked yet but I'll explain a bit more.

Base1X = 245;
Base2X = 685;
Base3X = 115;
Base4X = 995;
Base5X = 656;

Base1Y = 123;
Base2Y = 265;
Base3Y = 958;
Base4Y = 45;
Base5Y = 392;

int StructX[5]={Base1X,Base2X,Base3X,Base4X,Base5X};

int StructY[5]={Base1Y,Base2Y,Base3Y,Base4Y,Base5Y};
Okay the code above is supposed to store these values into an array I'll use later. If you have a better solution please tell me. btw the values right there are just an example I have another code that retrieves it and stores them.

for (int NumCount = 0; NumCount <= 6; NumCount ++)
{
Now this code is suppose to make NumCount 1 through 6. So it'll make NumCount into 1 and do the code within and when its finish make it into 2 and repeat itself until it hits 6 and then it'll go back to 1. Again if you have a better solution please tell me.

for (int BaseX = StructX[NumCount] - 250;
BaseX <= StructX[NumCount] + 250;
BaseX++)
{
for (int BaseY = StructY[NumCount] - 65;
BaseY <= StructY[NumCount] + 35;
BaseY++)
{
Okay this code is used as a "range". So "BaseX" will become "StructX[NumCount]" which will be every number inside the array since "NumCount" will let it cycle through it all. There's not much going on here, but if you have another solution again please tell me.

Now the rest of the code is not an issue since it works fine. But the problem is, is that I don't think this works...

for (int NumCount = 0; NumCount <= 6; NumCount ++)
{
for (int BaseX = StructX[NumCount] - 250;
BaseX <= StructX[NumCount] + 250;
BaseX++)
{
for (int BaseY = StructY[NumCount] - 65;
BaseY <= StructY[NumCount] + 35;
BaseY++)
{
I've tested it out and it seems not to work. It doesn't cycle through the whole array. Maybe you guys can help me make a better code function that does this exact thing.
0

Commented:
you are right it doesn't work cause you didn't made the necessary change evilrix told you:

``````for (int NumCount = 0; NumCount <= 6; NumCount ++)
``````

what would set NumCount to 0,1,2,3,4,5,6 before break. that are 7 values, but the arrays StructX and StructY only were defined for five elements. so, the last 2 loop cycles of the outer loop would access the arrays beyond their array boundaries what not necessarily would crash but gives unpredicted results.

note, both the inner loops could be replaced by simple if statements cause the counting would happen only once for each constant.

``````if (BASE1X >= StructX[NumStruct]-250 && BASE1X <= StructX[NumStruct]+250)
{
if (BASE1Y >= StructY[NumStruct]-65 && BASE1Y <= StructY[NumStruct]+35)
count++;
}
if (BASE2X >= StructX[NumStruct]-250 && BASE2X <= StructX[NumStruct]+250)
{
if (BASE2Y >= StructY[NumStruct]-65 && BASE2Y <= StructY[NumStruct]+35)
count++;
}
....
``````

but i have no idea what the count result should tell. you can see at one glance to your arrays whether two constants BASEnX were within mutual -250 and +250 range or not and same applies for the corresponding BASEnY constants. why do you need 250000 loop cycles for a calculation that easily could be made mentally?

Sara

Sara
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.