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

How to keep track of a value while a for loop iterates.

Page 1 of 1

6 Replies - 661 Views - Last Post: 24 January 2010 - 03:33 PM Rate Topic: -----

#1 AspiringGameProgrammer  Icon User is offline

  • New D.I.C Head

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

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

Post icon  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  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

View Postkiasta, 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-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

View PostPaul-, 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-  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • 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

View PostAspiringGameProgrammer, 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

View PostPaul-, on 24 Jan, 2010 - 02:23 PM, said:

View PostAspiringGameProgrammer, 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