# How to keep track of a value inside of a 'for loop'

Page 1 of 1

## 6 Replies - 1839 Views - Last Post: 24 January 2010 - 03:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=151761&amp;s=71e5e418df832cc99efd5cb771239b87&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AspiringGameProgrammer

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 10-September 09

# How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 01:40 PM

I understand a for loop and its structure,...

```{
float rMax, radius, dist;

for (i = 0; i < numPoints; i++)
{
rMax = 0;
float x = static_cast<float>(xPts[i] - xAvg);
float y = static_cast<float>(yPts[i] - yAvg);
radius = (x * x) + (y * y);
dist = sqrt(radius);
rMax = dist;
}
}

```

After each iteration, how can i keep track of a value (such as rMax in this code example)?

This post has been edited by AspiringGameProgrammer: 24 January 2010 - 03:24 PM

Is This A Good Question/Topic? 0

## Replies To: How to keep track of a value inside of a 'for loop'

### #2 kiasta

• D.I.C Regular

Reputation: 30
• Posts: 287
• Joined: 18-November 07

## Re: How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 02:33 PM

You can try storing them in an array, for example:

```{
float rMax, radius, dist;
float myArray[3] = {0,0,0}

for (i = 0; i < numPoints; i++)
{
rMax = 0;
float x = static_cast<float>(xPts[i] - xAvg);
float y = static_cast<float>(yPts[i] - yAvg);
radius = (x * x) + (y * y);
dist = sqrt(radius);
rMax = dist;
myArray[0] = rMax;
myArray[1] = radius;
myArray[2] = dist;
}
}
```

You can change the size of the array to what you need, I am unsure how many times your for loop is looping so I just used [3] as an example. You can than in turn increment the location of the array by using a variable and store each value into a new location of the array.

something like:

```{
float rMax, radius, dist;
float myArray[3] = {0,0,0};
int inc = 0;

//I'm assuming you initialized 'i' elsewhere, so I initialized it in this for loop to make more sense

for (int i = 0, x = 0; i < numPoints; i++)
{
rMax = 0;
float x = static_cast<float>(xPts[i] - xAvg);
float y = static_cast<float>(yPts[i] - yAvg);
radius = (x * x) + (y * y);
dist = sqrt(radius);
rMax = dist;
myArray[inc] = rMax;
++inc;
myArray[inc] = radius;
++inc;
myArray[inc] = dist;
++inc;
}
}
```

Each value passed to those 3 variables will be stored into the array. You can even use a vector if you wish and dynamically store values.

I hope that helps, if that is not what you were asking well I guess I misinterpreted your question.

This post has been edited by kiasta: 24 January 2010 - 02:42 PM

Was This Post Helpful? 0

### #3 AspiringGameProgrammer

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 10-September 09

## Re: How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 02:48 PM

kiasta, on 24 Jan, 2010 - 01:33 PM, said:

I hope that helps, if that is not what you were asking well I guess I misinterpreted your question.

Unfortunately this isn't the solution.

The program asks for the user to provide a number of points. Then the program randomly generates (x,y) points in a 2D grid. So it could be 3 points, 5 points, 10 points, etc.

Then, the average of all points (x,y) are determined, which determines the center of all points. Finally, the program iterates through all of the points and determines which is the farthest from the (center point) and computes the radius of the circle. A circle is drawn around all points (in which one of the farthest points is on the outer perimeter of the circle.)
Was This Post Helpful? 0

### #4 Paul-

• D.I.C Regular

Reputation: 61
• Posts: 260
• Joined: 11-December 09

## Re: How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 02:58 PM

I think you are asking about how to keep track of the maximum value for rMax. This should work:

```{
float rMax, radius, dist;

for (i = 0; i < numPoints; i++)
{
float x = static_cast<float>(xPts[i] - xAvg);
float y = static_cast<float>(yPts[i] - yAvg);
radius = (x * x) + (y * y);
dist = sqrt(radius);

if (i == 0) {
rMax = dist;
} else if (dist > rMax) {
rMax = dist;
}
}
}

```

Was This Post Helpful? 1

### #5 AspiringGameProgrammer

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 10-September 09

## Re: How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 03:18 PM

Paul-, on 24 Jan, 2010 - 01:58 PM, said:

I think you are asking about how to keep track of the maximum value for rMax. This should work:

```{
float rMax, radius, dist;

for (i = 0; i < numPoints; i++)
{
float x = static_cast<float>(xPts[i] - xAvg);
float y = static_cast<float>(yPts[i] - yAvg);
radius = (x * x) + (y * y);
dist = sqrt(radius);

if (i == 0) {
rMax = dist;
} else if (dist > rMax) {
rMax = dist;
}
}
}

```

That worked perfect! Thank you very very much!

I cant figure out why this worked:
```if (i == 0)
{
rMax = dist;
}
```

The rest of my code resembled your solution except for this portion.

Could you explain why you used if (i == 0)...

and once again Thank You
Was This Post Helpful? 0

### #6 Paul-

• D.I.C Regular

Reputation: 61
• Posts: 260
• Joined: 11-December 09

## Re: How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 03:23 PM

AspiringGameProgrammer, on 24 Jan, 2010 - 02:18 PM, said:

I cant figure out why this worked:
```if (i == 0)
{
rMax = dist;
}
```

Could you explain why you used if (i == 0)...

It sets the initial value of rMax to the first distance in the list.
Was This Post Helpful? 0

### #7 AspiringGameProgrammer

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 10-September 09

## Re: How to keep track of a value inside of a 'for loop'

Posted 24 January 2010 - 03:33 PM

Paul-, on 24 Jan, 2010 - 02:23 PM, said:

AspiringGameProgrammer, on 24 Jan, 2010 - 02:18 PM, said:

I cant figure out why this worked:
```if (i == 0)
{
rMax = dist;
}
```

Could you explain why you used if (i == 0)...

It sets the initial value of rMax to the first distance in the list.

Thank you.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }