Free2Code
 
Time: 2008-11-21, 09:42pm
C++ flow help
Subject: C++ flow help  ·  Posted: 2007-01-31, 07:17am
Rank: ? (6)
Member #: 29538
I want the following logic to be implemented in C++. It is similar to the concept of building a truth table.

The user will provide set of inputs and their states, say

In1 v11 v12
In2 v21 v22

where (v11 v12 and v21 v22) are the two inputs states. I need to create a flow which iterates through all possible permutations of these states.

Example

In1 In2
v11 v21
v11 v22
v12 v21
v12 v22 just like a truth table.

the user provides the input during the run.

Can someone please help me solve this.

Thanks.



 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-01-31, 08:36am
Rank: ? (767)
Member #: 11085
You need to use a nested loop. For example:
Code:
  1. for(int i=0; i<2; i++){
  2.     for(int l=0; l<2; l++){
  3.         std::cout << In1[i] << " " << In2[l] << std::endl;
  4.     }
  5. }


- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-01-31, 08:57am
Rank: ? (6)
Member #: 29538
that is true if i know the number of inputs(namely A and B) but what if i dont know the number of inputs.

Thanks.

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-01-31, 02:33pm
Rank: ? (767)
Member #: 11085
You use an iterator and a vector instead of an integer and a static array.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-01, 02:52am
Rank: ? (6)
Member #: 29538
Could you please demostrate that with an example. I am really not getting this concept. Thanks

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-01, 03:51am
Rank: ? (767)
Member #: 11085
Why didn't you originally just ask what an iterator was? That would have saved you some time. Anyway, I'll show you an example of how an iterator works.

Code:
  1. for(vector<int>::iterator it=In1.begin(); it != In1.end(); i++){
  2.     std::cout << *it << std::endl;
  3. }


vector<int>::iterator would be replaced with whatever type of vector you are using and the iterator is always a pointer to the current value, which is why it is dereferenced.


- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-01, 06:08am
Rank: ? (6)
Member #: 29538
Thanks. I think i was not clear regarding the problem.

I need to generate all possible combinations between the inputs provided by the user. These inputs futher have 2 states each( 0 and 1). So if the user provides 2 inputs (A & B) then i need to generate the following combinations

A B
0 0
0 1
1 0
1 1

if the user provides 3 inputs ( A B C) then i need to generate

A B C
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1


I know i need to use the iterator but i cannot get the flow algorithm properly. Hope this makes the problem a little clear.

Thanks.

 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-01, 10:12am
Rank: ? (767)
Member #: 11085
If you have a variable amount of inputs with a variable amount date for each, use a vector of vectors for the input variables.

Why don't you show the code you have so far so I can tell you what you are doing wrong?

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-05, 06:15am
Rank: ? (6)
Member #: 29538
Thank You.

One more clarification. I want to get all possible combinations for given number of inputs and their respective values.

Example

Input Values
A1 V1 V2
B1 V3 V4

I want all the possible combinations (in this case they will be 4), namely

V1 V3
V1 V4
V2 V3
V2 V4.

I am using map<string, vector<string>> to store the pins and their values but I am not getting the logic to find all possible combinations.
Please suggest.

Thanks,
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-05, 08:52am
Rank: ? (767)
Member #: 11085
I already have suggested it, multiple times. Run an iterator through your map and then, inside that iterator, run an iterator through the vector portion of the map.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-05, 09:41am
Rank: ? (6)
Member #: 29538
map<string, vector<string>> state;
map<string, vector<string>> ::iterator mi;
vector<string>:: iterator vi;

for(mi = state.begin(); mi != state.end(); mi++){
for(vi = mi.begin(); vi != mi.end(); vi++){
(*mi).first = (*mi).second[*vi];

// This should assign the first string member a value from vector<string>

Is this correct?
}
}

thanks.
 
  Reply to this ·  Post link ·  Top
Subject: Re: C++ flow help  ·  Posted: 2007-02-05, 03:40pm
Rank: ? (767)
Member #: 11085
If you are still trying to do what you stated in your original post, read in a variable amount of inputs with a variable amount of data and print out all possible combinations, then no. You don't even have a command in that code to print, however, so I am not certain you are attempting that any longer.

Nonetheless, at present, if that code could compile, it would do no more than than set the string of each member in state equal to the string of the final value in the vector portion of state.

In any case, would you please clarify if you are still trying to do what you originally posted? If so, and that is your code at present, not to be rude, but you should consider telling the truth in the future and not say you are having trouble figuring out the logic behind a problem but rather, that you have no idea how to program the solution to a problem in order that I or anyone else answering your question better knows how to respond.

» Post edited 2007-02-05, 03:44pm by relpats_eht.

- relpats_eht
 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons