473,400 Members | 2,163 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,400 software developers and data experts.

dynamicly defined number of loops?

I want to program general class for function nodes of factor graph….

Functions (I define them separately) can have different number of variables, for example f_A(x1,x2), f_B(x1, x2, x3)… Each variable may take different number of values, let say N1 for x1, N2 for x2….

In the program I need to build a table of all possible values of a function.
If I knew the number of variables taken by function I would write something like this:

//for the function A, x1[], x2[] arrays of values of the variables

values * int;

values = new * int[N1*N2];

int n=0;

for(int i=0; i<N1; i++)

for(int j=0; j<N2; j++)
{

values[n]=f_A(x1[i], x2[j]);

n++;
}
In case of f_B there would be 2 inner loops(3 in total). But what can I do if I do not know the number of variables? I think I cannot program variable number of inner loops, can I? Actually for small number of variables I can write switch…case….but is it only solution?

Thank you very much if you answer.
Jun 20 '07 #1
2 1305
DeMan
1,806 1GB
Don't quite follow what you are trying to do, but.....
I think you could achieve (sort of) what you want with a recursive function.....

Expand|Select|Wrap|Line Numbers
  1.  
  2. my_function(int start, int fin, list RemainingItems)
  3. {
  4.     for(int i=start; i<fin)
  5.     {
  6.       if(!RemainingItems.IsEmpty())
  7.       {
  8.         RemainingItems.NextElement();
  9.         my_function(StartVal; FinVal; RemainingItems);
  10.       }
  11.       doProcess();
  12.     }
  13. }
  14.  
Obviously your example may be a little more complex, as you need to be able to find some of the values specified in the excerpt above. I am also assuming that each nested loop has basically the same behaviour?......


}
Jun 20 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
This is an excellent place for the Visitor design pattern.

Visitor allows you to access a derived object using a base class pointer and from there you are able to access derived methods using a derived pointer.

Check out: Design Patterns by Erich Fromm, et al. Addison-Wesley 1994.

Here I would see a base class of FunctionNode with an Accept() method for the Visitor.

TwoFuncttionNode derives from FunctionNode.
ThreeFunctionNode derives from FunctionNode.
etc...

So, you create a ThreeFunctionNode object and assign it to a FunctionNode*.

Next, you create the FunctionNodeVisitor hierarchy:

TwoFuncttionNodeVisitor derives from FunctionNodeVisitor.
ThreeFunctionNodeVisitor derives from FunctionNodeVisitor.
etc...

Now, you create a ThreeFunctionNodeVisitor object and assign it to a FunctionNodeVisitor*.

Next, you call the FunctionNode::Accept() using the FunctionNodeVisitor*.

The Accept() method calls FunctionNode::DoAccept(). This is a private virtual function that is overridden by ThreeFunctionNode. You are now in ThreeFunctionNode::DoAccept with a FunctionNodeVisitor pointer that is really a ThreeFunctonNodeVisitor pointer. That makes it safe to call FunctionNodeVisitor::VisitThreeFunctionNode using the this pointer of the FunctionNode object (really the address of a ThreeFunctionNode object).

You end up in ThreeFunctionNodeVisitor::VisitThreeFunctionNode with a ThreeFunctionNode pointer. You can now access any methods of this class. Or even add methods not in the original ThreeFunctionNode class.

Visitor removes the restriction that all derived classes must support only the base class methods.

Seriously, check out that book. Page 331. It's your answer.
Jun 21 '07 #3

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

Similar topics

2
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
5
by: Stef | last post by:
Hello, I have a question that maybe some of you could answer because I don't know how to do this: I want the user to be able to define how many itterations or better loops should spawn... The...
1
by: Henke | last post by:
Hi I have a aspx-page with a panel-control. On this panel control I add user controls dynamicly with LoadControl and panel.Controls.Add(myControl). On some of the dynamicly added user controls I...
2
by: DaWoE | last post by:
Hi all, I'm fairly new to ASP.NET. What i want to do is creat a online registration form. On the first step is getting the users details and the number of people he wants to register. Based on...
9
by: Stan Cook | last post by:
Can anyone tell me how to get the length of a number. I know len(string) will get the length of a string, but it doesn't like len(int). I seem to remember something like %s string. I tried to...
6
by: nullgraph | last post by:
Hi everyone, I'm new to Python and the notion of lambda, and I'm trying to write a function that would have a varying number of nested for loops depending on parameter n. This just smells like a...
36
by: AB3004 | last post by:
Hi, I'm currently working on this piece of code & unfortunately, whenever I run it, it states that selectNumbers isn't defined and it errors on Line 1 (?) can anyone tell me why? I'm tearing my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.