473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to Program Variable Number of Loops in C++?

So, the pseudocode for a piece of software I'm writing looks like:

================================================== =======

for (removalRate = minVal; removalRate <= maxVal; removalRate + removalRateStep) {

// DOSTUFF (run a model which determines a value based on removal rate, and output results to a file)

}

================================================== =======
The problem is, there are an indeterminate number of groups that this needs to be done for, each with their own minVal, maxVal, and removalRateStep variables.

For example, the user could have groups like this:

A: minVal = 0, maxVal = 1.0, removalRateStep = 0.1
B: minVal = 0.3, maxVal = 0.4, removalRateStep = 0.025
C: minVal = 0.1, maxVal = 0.8, removalRateStep = 0.05

and so on, with the number of groups unknown to the program except at runtime.

Thoughts on how to do this would be greatly appreciated!
Oct 18 '11 #1
3 2025
Rabbit
12,516 Expert Mod 8TB
There's no problem with the user inputting those values at runtime. You would pretty much do it exactly as you have it in your pseudo-code.

Just a word of warning though. Since you are comparing floats, there will be times when you get 1 more step than intended. If that is not acceptable, you need to shift everything into integer ranges.
Oct 18 '11 #2
Thanks for the input, but I guess I didn't explain precisely enough... The three groups above all vary independently... So, simple example with two groups:

A: minVal = 0, maxVal = 0.2, stepVal = 0.1
B: minVal = 0, maxVal = 1.0, stepVal = 0.2

Model: Sum of all currentValues

Output: Sum for each combination of currentValues

So the specific example above would create two loops, "FOR LOOP A" has three steps (0, 0.1, 0.2), and within that is "FOR LOOP B" with 5 steps (0, 0.2, 0.4, 0.6, 0.8, 1.0).

================================================== =======
Expand|Select|Wrap|Line Numbers
  1. for (currentValueA = minValA; currentValueA <= maxValA; currentValueA + stepValA) {
  2.  
  3.       for (currentValueB = minValB; currentValueB <= maxValB; currentValueB + stepValB) {
  4.  
  5.            modelOutput = currentValueA + currentValueB;
  6.            saveOutputToFile(modelOutput, currentValueA, currentValueB);
  7.        }
  8. }
  9.  
The specific problem is that the number of groups, each of which will vary independently of the others, is unknown precompile...

I'm guessing recursion likely is one answer...

Thanks for the advice on the changing everything into integer ranges...
Oct 18 '11 #3
Rabbit
12,516 Expert Mod 8TB
You could do something like this (pseudocode)
Expand|Select|Wrap|Line Numbers
  1. numLoops = //get number of loops from user
  2. doLoop(numLoops)
  3.  
  4. doLoops(numLoops) {
  5.   min = //get min
  6.   max = //get max
  7.   step = //get step
  8.  
  9.   for (i=min; i<=max; i += step) {
  10.     if (numSteps != 1) {
  11.       doLoop(numSteps - 1)
  12.     } else {
  13.       //do whatever
  14.     }
  15.   }
  16. }
Oct 18 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Snyke | last post by:
Well my problem is simple: I have a class which has a variable number of members. Lets say I have a class Family and a variable number of FamilyMembers. Until now I used a member of Family called...
10
by: The Directive | last post by:
I read the C FAQ question on passing a variable number of arguments, but it didn't help. The example assumes all arguments are of the same type. I want to create a function "trace" that can be...
2
by: EricLin | last post by:
Dear all: I am just concerned about how to use a function with variable number of arguments. for example: int TestFunction(arguments) { .... Testing contents... }
1
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For...
4
by: BLACKDOG157 | last post by:
I have to make a form with a variable number of textboxes. If, in the previous screen, the user chose a 2 day hotel stay, I need to input the number of rooms for 2 days. But if in the previous...
3
by: bennett.matthew | last post by:
Hello, Is there a good way to make PreparedStatements with a variable number of parameters? My queries look something like this: SET @rowid := null; SELECT * FROM ( SELECT
6
by: rashmi | last post by:
Hello All, Can we map a MACRO with variable number of arguments to a function with variable number of arguments? Please help me in finding out how this could be done ? for eg: #define...
2
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this...
4
by: ashish.sadanandan | last post by:
Hi, I'm new to XML and just started reading some documentation on W3C compliant schemas recently. The XML file I'm generating is for an embedded application and so size is of extreme importance....
2
by: Alan | last post by:
I have a couple of questions about using a variable number of arguments in a function call (...). The context is that I have some mathematical functions I created. I currently pass them a pair of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.