In design phase. Is this layout/syntax correct?

Pages: 12
seeplus:
Nice to hear from you again. You helped me out and gave me some confidence in my first post in this forum.
Well you wouldn't do it like this:


Have you tried it that way? If it works the way you want, it's succinct code and probably easy to follow.....

Also, try this. Does this do what you want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	for (int a = 0, b1 = 1; a < p5.size; ++a, b1 = 1) {  //  used to cycle through each row in 2d vector named p5 .
		for (int b {0}; b1 && (b < (b + 65)); ++b)  // Loop#1 - 65 iterations
			b1 = p5vec1[a] == vec1[b];

		for (int b = 100; b1 && (b < (b + 35)); ++b)  // Loop #2 - 35 iterations
			b1 = p5vec2d_1[a][0] == vec2d_1[b][0];

		for (int b = 100; b1 && (b < (b + 44)); ++b)  // Loop #3 - 44 iterations
			b1 = p5vec2d_1[a][1] == vec2d_1[b][1];

		for (int b = 50; b1 && (b < (b + 24)); ++b)  // Loop#4 - 24 iterations
			b1 = p5vec2[a] == vec2[b];

		if (b1)
			std::cout << p5[a][0] << " " << p5[a][1] << " " << p5[a][2] << " " << p5[a][3] << " " << p5[a][4] << endl;
	}


Last edited on
Have you tried it that way? If it works the way you want, it's succinct code and probably easy to follow.....
Easy to follow indeed. And your example above is also easy to follow.

I'm in the process of trying the function way. Here is the code I have for it so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void f ( )
{
    while ( a < p5.size() ) {
        func ();
        ++a;
    }
}

void func ( )
{
    for ( int b {lay01} ; b < ( lay01 + strat[0][0] ); ++b ) {
            if ( p5sums[a] != sums[b] )  return;
    }

    outfile_01 << p5[a][0] << " " << p5[a][1] << " " << p5[a][2] << " " << p5[a][3] << " " << p5[a][4] << endl;
}

I'm in the process of fixing the errors from the build. UGH! I got 11 of them. All stating 'this' was not declared in this scope. Glad I only started with one FOR loop in func(). Can you imagine having 10 or 20 FOR loops inside func(). Yikes!! That would have been devisating for my confidence. Now, I know I have A LOT to learn, but for right now, that would have been a setback for me mentally. I will give your method a try also. I just want to see if I can fix the errors using the function method. I don't want to give up on something just cause it's not easy for me to do. It's probably a good learning experience for me. I think I'll give your way a try cause it is also easy to follow. I will try the func method too. I just would like something to work, then improve the code from there.
Thanks seeplus!!!!
I have given seeplus's code a try. I got no output. The code below should produce 511 lines of output. Here is what the code looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
    int lay01 {0};
    int lay02 {1000000};
    for ( size_t a = 0, b1 = 1; a < p5.size() ; ++a ) {
        for ( int b {lay01}; b1 && ( b < (b + strat[0][0])); ++b )  //  strat is a 2d vector and 65 is in that location.
            b1 = p5sums[a] == sums[b];
        if ( b1 ) {
            for ( int b {lay02}; b1 && ( b < (b + strat[1][0])); ++b )   //  24 is in that location for strat
                b1 = p5sums[a] == sums[b];
            if ( b1 ) {
                std::cout << p5[a][0] << " " << p5[a][1] << " " << p5[a][2] << " " << p5[a][3] << " " << p5[a][4] << endl;
            }
        }
    }
You don't actually need L6 and L12. The for condition will take care of it. See update to my code above.

If you're getting no output, then one of the conditions tested in the for loops has evaluated to false. You could insert some cout statements if the conditions evaluate to false.

1
2
3
4
5
6
7
8
9
10
11
12
13
	int lay01 {0};
	int lay02 {1000000};

	for (size_t a = 0, b1 = 1; a < p5.size(); ++a) {
		for (int b {lay01}; b1 && (b < (b + strat[0][0])); ++b)  //  strat is a 2d vector and 65 is in that location.
			b1 = p5sums[a] == sums[b];

		for (int b {lay02}; b1 && (b < (b + strat[1][0])); ++b)   //  24 is in that location for strat
			b1 = p5sums[a] == sums[b];

		if (b1)
			std::cout << p5[a][0] << " " << p5[a][1] << " " << p5[a][2] << " " << p5[a][3] << " " << p5[a][4] << endl;
	}


Last edited on
Hey, seeplus:
I have gotten farther than before with your code. I used the debugger, thinking that I was not setting it up correctly. I think I may know the reason I'm getting no output. Through the first iteration it is false, BUT b1 never resets back to 1. Once it becomes " 0 " it never changes when it goes back to
for (size_t a = 0, b1 = 1; a < p5.size(); ++a). I think other than that, I believe it will perform as expected.

I also changed this part
b1 && (b < (b + strat[0][0])); ++b) changed it to
b1 && ( b < (lay01 + strat[0][0])); ++b )Please correct me if I'm wrong, but reading it, it looked like, with ++b after each iteration, b < b + strat[0][0], it would never end?? Please correct me if I'm wrong!! Now, I gotta figure out where to place b1 = 1 to reset b1.

I believe I'm close!!!

Another Edit: I made a mistake on posting an example from before. I forgot to change the " == " to " != " Sorry bout that!
Last edited on
OMG!!!! I got it to work!!! THNAKS SO MUCH SEEPLUS!!!!!!!!!!!

Here is the final improvements to the latest developments:
1
2
3
4
5
6
7
8
9
10
11
12
13
    int lay01 {0};
    int lay02 {1000000};
    for ( size_t a = 0, b1 = 1; a < p5.size() ; ++a ) {
        for ( int b {lay01}; b1 && ( b < (lay01 + strat[0][0])); ++b )  //  strat is a 2d vector and 65 is in that location.
            b1 = p5sums[a] != sums[b];
        for ( int b {lay02}; b1 && ( b < (lay02 + strat[1][0])); ++b )   //  24 is in that location for strat
                b1 = p5sums[a] != sums[b];
        if ( b1 ) {
            outfile_01 << p5[a][0] << " " << p5[a][1] << " " << p5[a][2] << " " << p5[a][3] << " " << p5[a][4] << endl;
        }
    b1 = 1;
    }
    outfile_01.close();


When I was debugging this latest code. If the first FOR loop fails, it still hits the other FOR loops, BUT with the addition of b1 && in the condition part, it doesn't check anything else in that FOR loop. As far as the output goes, the output IS accurate!

This may not be the MOST optimized way of going about it. BUT, I have some working code!!! Right now that is important to me. I definetly will look into the functions that dhayden helped me with, but I have some more learning to do before tackling that task. Also, and a BIG also, I want to really look farther into what mbozzi was directing me towards. It was the STD::NONE/all/any. I did take a look at the cpp reference material on that. Although it was really difficult for me to understand at this moment in learning, the thought of letting the STL do most of the work is probably the MOST optimized way to go.

BUT hey, I got some working code!!! I'm very happy with that. I would like to THANK everyone who participated in this thread. I learned so much by being here. I'm gonna consider this SOLVED! Thanks again everyone!!
Yeah - I realised the solution when in bed last night! Originally there were different bool variables for each for loop - but I changed the code to just using one and forgot to re-initialise the test variable each time round the loop. This is why you test and debug code, never assume and anyone can make small mistakes!

The lines b < b + strat[0][0] et al came straight from your earlier example!

Great that you got it to work! The only change I would have made was where to put the b1 = 1

1
2
3
4
5
6
7
8
9
10
for (size_t a = 0, b1 = 1; a < p5.size(); ++a, b1 = 1) {
	for (int b {lay01}; b1 && (b < (lay01 + strat[0][0])); ++b)  //  strat is a 2d vector and 65 is in that location.
		b1 = p5sums[a] != sums[b];

	for (int b {lay02}; b1 && (b < (lay02 + strat[1][0])); ++b)   //  24 is in that location for strat
		b1 = p5sums[a] != sums[b];

	if (b1)
		outfile_01 << p5[a][0] << " " << p5[a][1] << " " << p5[a][2] << " " << p5[a][3] << " " << p5[a][4] << '\n';
}


Note that using '\n' rather than endl is quicker as endl flushes the output buffer whereas '\n' does not.

Last edited on
I have made both changes.
The only change I would have made was where to put the b1 = 1
Note that using '\n' rather than endl is quicker as endl flushes the output buffer whereas '\n' does not.



Thanks Again!!!
Brian
Topic archived. No new replies allowed.
Pages: 12