Rotate the amount of increase; X times

Pages: 12
Hi, dear all

I have a question
I hopeful that do you help me to get the result I want
The problem is; I want to rotate 20 times. but the "for loop" does not allow it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cmath>
using namespace std;

int main()
{	
	long int taban, us, result,result2,x;
	taban=2;
	
	 for ((x=1),(us=1);(us<=10)&&(x<=100);(us++))
	{
		
		result=pow(taban,us);
		cout<<result<<" ";
		result2=x+result;
		cout<<"x:"<<x<<" uslu sayi:"<<result2<<endl;
		x=result2;
   
	}
	
 return 0;	
}
 /*
	$=================sonuc baslangic==================$
2 x:1 uslu sayi:3
4 x:3 uslu sayi:7
8 x:7 uslu sayi:15
16 x:15 uslu sayi:31
32 x:31 uslu sayi:63
64 x:63 uslu sayi:127

	$=================sonuc bitis==================$
//burada artış miktarını döngü olarak hesaplatamıyorsun! yani burada 10 kere dön diyemiyorsun.
//here, cannot calculate the increase amount as a counter! so you can't say it is loop 10 times.
*/


so
//for ((x=1),(us=1);(us<=20)&&(x<=20);(us++))
We need to introduce(acquaint) 20 turns to the for loop.
//(us<=20)&&(x<=20)
how do I identify this as a rotation counter?



Last edited on
Are you getting an error? Or is it like the for loop just doesn’t have the type of structure you want?
NO, I did not getting
if variable of x is increase, the loop is continuing
If the variable x increases, the loop is in progress.

//(x<=130) so, I need to find the x number on the 20th loop!

1
2
3
4
5
6
7
8
#include <iostream>

int main () {
    for (int x=1,y=1;x<=39,y<=20;x+=2,y++)
    {
        std::cout<<y<<".sayi:"<<x<<std::endl;
    }
}


burada olduğu gibi 20 kere döndürmek için 20th dönüşteki 39 sayısını bulmak gerek!
we need to find the number 39 on the 20th turn to rotate 20 times as it is here!
logically he should be able to do it with the for loop but it didn't! You can try!

Last edited on
If this is the output you want to see

2 x:1 uslu sayi:3
4 x:3 uslu sayi:7
8 x:7 uslu sayi:15
16 x:15 uslu sayi:31
32 x:31 uslu sayi:63
64 x:63 uslu sayi:127
128 x:127 uslu sayi:255
256 x:255 uslu sayi:511
512 x:511 uslu sayi:1023
1024 x:1023 uslu sayi:2047
2048 x:2047 uslu sayi:4095
4096 x:4095 uslu sayi:8191
8192 x:8191 uslu sayi:16383
16384 x:16383 uslu sayi:32767
32768 x:32767 uslu sayi:65535
65536 x:65535 uslu sayi:131071
131072 x:131071 uslu sayi:262143
262144 x:262143 uslu sayi:524287
524288 x:524287 uslu sayi:1048575
1048576 x:1048575 uslu sayi:2097151



Then


1
2
3
4
5
6
7
8
9
10
	 for ((x=1),(us=1);(us<=20);(us++))
	{
		
		result=pow(taban,us);
		cout<<result<<" ";
		result2=x+result;
		cout<<"x:"<<x<<" uslu sayi:"<<result2<<endl;
		x=result2;
   
	}


Unless I misunderstand, "us" is the count through the steps, and since the original stops at 10, there's no chance to get to 20.

Also, stopping on "x" requires that you know how large "x" might be when reaching step "20", which is, in part, what this loop calculates and so that makes no sense.

Just limit on "x".
:(
Excuse me;
Actually it(us) is turkish word(üs-power) so, (exponential numbers)
I forget change it 10 to 20 and I gave the number as an example.
Yes the final number may be large but I may need it
There's something I noticed here! If (x ++) we get the desired result.


1.sayi:1
2.sayi:3
3.sayi:5
4.sayi:7
5.sayi:9
6.sayi:11
7.sayi:13
8.sayi:15
9.sayi:17
10.sayi:19


(then-sonrasında) - vasıtasıyla(by))

1
2
3
4
5
6
7
8
#include <iostream>

int main () {
    for (int x=1,y=1;x<=20;x+=2,y++)
    {
        std::cout<<y<<".sayi:"<<x<<std::endl;
    }
}

In C ++, the for construct does not know different increment definitions, such as x + = 2.
so, it is bug!

but

1
2
3
4
5
6
7
8
#include <iostream>

int main () {
    for (int x=1,y=1;y<=20;x+=2,y++)
    {
        std::cout<<y<<".sayi:"<<x<<std::endl;
    }
}


then(result)


1.sayi:1
2.sayi:3
3.sayi:5
4.sayi:7
5.sayi:9
6.sayi:11
7.sayi:13
8.sayi:15
9.sayi:17
10.sayi:19
11.sayi:21
12.sayi:23
13.sayi:25
14.sayi:27
15.sayi:29
16.sayi:31
17.sayi:33
18.sayi:35
19.sayi:37
20.sayi:39


Thanks
As in this is like example, an increment amount such as (y ++) should always be between the other increment amount(x+=2).









Your terrible English makes it difficult to communicate with you.
Not my mistake!
Translated it(not my fault)! Directly!
they(Google Translate! and translation engine services) are guilty(culpable)!

Is the output of this what you want?
1
2
3
4
5
6
7
8
#include <iostream>

int main () {
    for ( int x=1; x <= 20; ++x )
    {
        std::cout << x << " -> " << 2*x-1 << '\n';
    }
}



Other words:
What output do you need for iteration x?
Last edited on
Is associativity also applied in a for loop because I have never seen a for loop like that before, it makes me want to rip my hair out.
@Shibitto,

The loop is legal, but most coding standards advise against using multiple tests and multiple increment clauses. There can be subtle bug inducing issues and it reads poorly.
> Not my mistake!
> Translated it(not my fault)! Directly!
you may also post the original message

I wonder about the meaning of «Rotate»
https://upload.wikimedia.org/wikipedia/commons/0/02/Rotating_Sphere.gif
When you use google translate, translate it to the English language, then translate it back, then try and change your sentence to make better sense again by changing the English words and translate back to make sure it has your point. Maybe that will help.

Here I will try it.

Raw translate:
Google çeviri kullandığınızda, ingilizce diline çevirin, sonra geri çevirin, sonra ingilizce kelimeleri değiştirerek tekrar daha iyi anlaşılması için cümlenizi değiştirmeye çalışın ve amacınıza sahip olduğundan emin olmak için tekrar çevirin. Belki bu yardımcı olur.

Re - translation, (what’s wrong):
When you use Google translation, translate it to English, then turn it back, then try changing your sentence to better understand it again by changing the English words and redial it again to make sure it has your purpose. Maybe this will help.

Example(örnek) fix(önceden belirlenmiş sonuç):
turn it back = geri çevirin = turn back
geri = back
Translate = tercüme etmek
tercüme etmek geri = translate back

Or just do the () like your already doing that works to
>highwayman (86)
Thanks Hwm;
Acutally, I always or often use qtranslate for pc. it is a good app, really! by my mind, without help.

işte bir problem değil, bu gibi cümleler!
this is like sentences -it can false Bu gibi cümlelerdir!
this is like + sentences->bu gibi + cümleler
"sentences this like" (bunu anlamakta zorlanıyorum yüz yüze konuşurken. )
"bu cümleler gibi" ilk geliyor aklıma

bir örnek veriyorum.
Böyle arada aklıma bu gibi cümleler gelir!
-Yine, Seher(sabah vakti!) düştü aklıma! artık, yâr gele,gele! diye, diye!
-Again, Daybreak ( early of morning !) Fell into my mind! Now! Friend(loved) come to, come to! say to! say to!
We are not completely insufficient,lost, that is!
büsbütün olarak yitik(insufficient) değiliz, yani!
ingilizcenin inceliklerini öğrenmek mecburiyetindeyim ki bana kalıpla öğretilenlerden daha fazlasını öğrenemeliyim ki zaten çoğu insan anadilim dediği lehçeyi bile kalıplarla öğreniyor. ben öyle istemiyorum.

-When you use google translate, translate it to the English language, then translate it back, then try(check)
- google translate kullandığın zaman, onu ingilizce diline tercüme et. sonra geri tercüme et, sonra dene(kontrol et)
and change your sentence to make better sense again
- ve daha manalı halde yapmak için tekrar cümlenizi değiştirin.
by changing the English words
(bu kısımı okurken(birleştirme-ekleme) beynim birden bloke oldu, aynı mide bulantısı gibi zihnimin bulandığını hissettim ve o yüzden translate engine kullanıyorum. Şeytanlar azapta gerek! Bir çeşit büyü,şer ile maruz kaldım :) )
(while reading(combine-add) this part, my brain suddenly became blocked, I felt my mind blurring like a nausea(stomach) and that's why I use the translate engine. Demons need punishment! I was exposed to some kind of magic, evil :))
değiştirilmiş ingilizce kelimeyi kullanarak!(aklımdan böyle çevirmiştim)
Using the modified English word!

-translate back to make sure it has your point. Maybe that will help.
-istediğin manaya sahip olmak için geri tercüme et. Belki(katiyyen) yardımcı olacaktır.
Translate back until you are sure you have reached your intended meaning.
- niyet ettiğin manaya ulaştığından emin olana dek geri tercüme et.




Last edited on
Konu alakalı olanı anlatayım;
-Turkish:şimdi, for loop standart bir "init; condition; increment" construct haline sahip. for dahilinde x+=2 veya x=x*2 gibi increment tanımlandığında "condition" olarak tanımlanan benzer bir x<=20 ifadesi kullanıldığında düzgün olarak sonucu yazdırmıyor. doğru olarak return olmuyor.
-Turkish:ve for içerisinde tanımlı olan koşullar ile, ve ayrıca l++ gibi bir increment ve aynı variable ile bir condition tanımlaman gerekiyor.
(bu noktalama işaretleri ile tercüme servisleri doğru çeviriyi yaptı.)
(Translation services with these punctuation marks made the right translation.)

-English:now, for loop has a standard "init; condition; increment" construct. It does not print the result properly when using a similar x <= 20 expression defined as "condition" when increment is defined, such as x + = 2 or x = x * 2 in for. it does not return correctly.
- English:and with the conditions defined in for, you also need to define an increment such as l ++ and a condition with the same variable.


-Re Translation:Şimdi, döngü için standart bir "init; koşul; artış" yapısına sahiptir. X + = 2 veya x = x * 2 gibi artışlar tanımlandığında "koşul" olarak tanımlanan benzer bir x <= 20 ifadesi kullanıldığında sonucu düzgün şekilde yazdırmaz. doğru dönmüyor.
-Re Translation:ve için tanımlanmış koşullar ile, l ++ gibi bir artış ve aynı değişkene sahip bir koşul tanımlamanız gerekir.

Artık, elimden gelen(tüm gayretim) bu kadar! ve daha fazla isteniyorsa, ingilizceyi daha iyi anlamak için beklemek gerek!
Now, that's all(all my effort) I can do! and if you want more, need to wait for a better understanding of english!

now, for loop has a standard "init; condition; increment" construct.


That much is correct


It does not print the result properly when using a similar x <= 20 expression defined as "condition" when increment is defined, such as x + = 2 or x = x * 2 in for.



I'm not sure what "does not print the result properly" means, but there is no relationship between code in the condition and the increment clauses, only when the code there is called. Nothing about x +=2 or x = x *2 (or x *+ 2) are affected by whatever is in the condition clause. If you see any other behavior, it is some other problem.

If, for example, the code were constructed like this:

1
2
3
4
5
6
int x = 0;

while( x <= 20 )
{
 x += 2; // or x *= 2;
} 


Will perform exactly as this code:

1
2
3
for( int x = 0; x <= 20; x += 2 )
{
}


With one exception. The integer x will only be local to the "for" loop, while it continues to exist in the previous example using while.

Otherwise, these are identical.


it does not return correctly.


This I don't quite understand because "for" loops have no "return".



..and with the conditions defined in for, you also need to define an increment such as l ++ and a condition with the same variable.


Well, you should, but there is no such requirement. For example, these are valid (though strange looking) for loops
1
2
3
4
5
6
7
8
9
10
11
for( ;; ) {} // an infinite loop

for( ; x < 2; ) {} // works like an "if" testing x < 2

for( ; t < 10; ++y ) {} // as long as t eventually grows to >= 10, this loop terminates testing t, 
                        // but will correctly increment y

for( ; t < 10; y *= 2.4 ) {} // as long as t eventually grows to >= 10, this loop terminates testing t, 
                              // but will correctly increment y, in this case assuming y is a double

for( ;; n *= 5 ) {} // an infinite loop that runs through 5 tables (will overflow/wrap the value ) 




Last edited on
>keskiverto (8626)
thanks keskiverto;
>What output do you need for iteration x?
I just wanted to write for practice and pleasure

I have been interested expressions in the for loop
another way(method) in your way and thanks for showing

I hope what I wrote above had helped you!
for (int x=1,y=1;y<=20;x+=2,y++) it is correct,works!
for (int x=1,y=1;x<=20;x+=2,y++) it is false and it is stop on 20th number(19). it(x<=20) should be deleted and it(y<=20) need to add to it.
for (int x=1;x<=20;x+=2) it is false and it is stop on 20th number(19). due to, x+=2.
Last edited on
Is necessary my this way? My intention was to get what I wanted just by this way.
If anyone write anything, I will answer at tonight!
This list of observations:

for (int x=1,y=1;y<=20;x+=2,y++) it is correct,works!
for (int x=1,y=1;x<=20;x+=2,y++) it is false and it is stop on 20th number(19). it(x<=20) should be deleted and it(y<=20) need to add to it.
for (int x=1;x<=20;x+=2) it is false and it is stop on 20th number(19). due to, x+=2.

These are not related to any C++ language issue. This has nothing to do with the "for" loop.

This is a matter of math.

That all work, but merely don't provide what you expected because you didn't quite recognize the math differences in each statement.

In the first one, you are testing y. Y starts at 1 and will continue to execute until y == 21. The loop will not execute when y == 21, which means the code in that loop performs 20 steps, from 1 to 20 in Y.

At that point, notice, that x started at 1, such that when y was, say, 18, x was 35. When y was 20, x was 39.

What you said was the "correct" version of the 3 examples executed the loop until the point when x was 39, not 20, where y was 20

So, in order for the loop to function as you expected, the loop must continue running until x is at least 39. You tested for x <= 20, when the loop would have finished as the first example if you merely tested for <= 39.

That has nothing to do with "for" loops, or with C++.

You were testing for the wrong end point of the loop.

Prove by trying this comparison:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main()
{

 // your working version

 for( int x = 1, y = 1;  y <= 20;  x += 2, y++ )
      {
        cout << "x " << x << ", y " << y << endl;
      } 

 cout << endl << endl;


 // the version you said failed

 for( int x = 1, y = 1;  x <= 20;  x += 2, y++ )
      {
        cout << "x " << x << ", y " << y << endl;
      } 

 cout << endl << endl;

 // what you should have tried 

 for( int x = 1, y = 1;  x <= 39;  x += 2, y++ )
      {
        cout << "x " << x << ", y " << y << endl;
      } 

}


Note, in the first example (as you wrote it), the last line from that output will be x 39, y 20.

That's what you said was correct.

Note that the second ends just as you said, with x at 19 and y at 10.

Then, note that the 3rd example runs until x 39, y 20, just as the first example, executing 20 steps.

The second example stops with x at 19 because you told it to stop when x > 20 ( testing x <= 20 ).

Part of this is because you're starting at 1, not 0 (which is fine, that's not a problem, you need x to start at 1)

However, you merely need to test for the endpoint value of x you require, not the number of steps to perform.


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
int main()
{

 // your working version

 for( int x = 1, y = 1;  y <= 20;  x += 2, y++ )
      {
        cout << "x " << x << ", y " << y << endl;
      } 

 cout << endl << endl;


 // the version you said failed

 for( int x = 1, y = 1;  x <= 20;  x += 2, y++ )
      {
        cout << "x " << x << ", y " << y << endl;
      } 
      cout << endl;
//the version it does not work
for( int x = 1;  x <=20;  x+= 2 )
      {
         
        cout << "x:" << x << endl;
      } 

 cout << endl;

 // what you should have tried 

 for( int x = 1, y = 1;  x <= 39;  x += 2, y++ )
      {
        cout << "x " << x << ", y " << y << endl;
      } 

 cout << endl;
// alternative to working method!
for(int x=1, y = 1;  y <= 20; y++ )
      {
          
        cout << y << "-->" << x << endl;
        x+=2;
      }      

 cout << endl;

// Thanks to keskiverto 
    for ( int x=1; x <= 20; ++x )
    {
        std::cout << x << " -> " << 2*x-1 << '\n';
    }
}

//You can see what the problem is by looking at the codes!


I try to explain when I find a way of expression that most people can understand.

aslında, sorunun ne olduğunu kodlara bakarak anladım. fakat ingilizce'de gerekli yada gereksiz bir kavramın birden fazla türü olunca anlaşılması zor olabiliyor.

in fact, I understood what the problem was by looking at the codes. but it can difficult to understand when there are multiple types of a necessary or unnecessary concept in English.

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
for (int x=1;x<=5;x+=2)
{
cout <<x<<endl;
}
//1 3 5 7 9 11  amount of number:6 counter of loop: 6-1:5(2-2-2-2-2)
//that is, for loop does not directly recognize or understand this algorithm. Simple but  it does not understand!


That's all I can do now with my knowledge of English! I leave the rest to your experience.
//1 3 5 7 9 11 amount of number:6 counter of loop: 6-1:5(2-2-2-2-2)
//that is, for loop does not directly recognize or understand this algorithm. Simple but it does not understand!


If you expected the output of this:

1
2
3
4
5
6
#include <iostream>
using namespace std;
for (int x=1;x<=5;x+=2)
{
cout <<x<<endl;
}


To produce the numbers 1, 3, 5, 7, 9, 11

I must inform you that you are mistaken about the meaning of the code.

It appears to me that you expect this loop to perform 6 steps, where each step is incremented by 2 (x += 2).

That is not what the for loop does.

The test x <= 5 does not process a number of steps. I tried to convey this point with my post of 3 examples.

The text checks the value of x, and stops when x is no longer <= 5 (less than or equal to 5).

I'm quite convinced that you misunderstand what the for loop does in the test.

It tests the value of x.

When x was incremented to 7 (the 4th entry), x was no longer <= 5, which is how that test is applied.

Since that test failed, the loop ends.

It works correctly.

You are expecting to work in a way that it does not work and was not designed to work.

The loop must be constructed:

for (int x=1;x<=11;x+=2)

To produce the results you expect to see, but again, the test is about the value of x during the loop, not the number of steps to be performed.

On the other hand, if what you are really trying to do is perform 6 steps, you have to count the steps to be performed in the "for" loop.

1
2
3
4
5
6
7
int x = 1;
for ( int s = 1; s <= 6; ++x )
{
 cout << x << " ";

 x += 2;
}


This code counts steps, allowing "x" to become whatever it will become as a result of 6 steps being performed.

The loop is not going to "understand" an algorithm. It is designed to loop until the test condition fails. It doesn't "understand" anything. It does not even require math to operate. It could work on strings, for example.

Although "for" allows multiple increments (separated by comma), and can initialize multiple values, they should not be used that way. It is recognized as a confusing form and most coding standards reject that form.

for( int a=0, b=1; a <= 5 && b <=9; a++, b += 2 )

Although you can fashion a loop that way, it is confusing. Most standards require something like:

1
2
3
4
5
6
int a=0;

for( int b = 1; b <=9 ; b += 2 )
{
 ++a;
}


Now, the termination of the "for" loop is more obvious, the author intends to control the loop on b.

If there is any reason to break the loop on "a", that should be in the braces

1
2
3
4
5
6
7
int a=0;

for( int b = 1; b <=9 ; b += 2 )
{
 ++a;
 if ( a > 5 ) break; // this is an alternate end of the loop
}


Even though the loop can be fashioned to combine "a" and "b" in the "for" clause, it is confusing by comparison.


I have one more way to illustrate this to try to make it clear

This is equivalent to the way the for loop is designed to operate:

1
2
3
4
5
6
7
8
9
10
11
int x = 0; // this is like "for( int x = 0; x < 5; x += 2 ) "

while( true )
{
 if( ! ( x < 5 ) ) break; // this is like "for( int x = 0; x < 5; x += 2)"

 
 // other code here - like the { } below a for loop

 x += 2; // this is like "for( int x = 0; x < 5; x += 2 ); 
}



The point being that the central clause in a "for" loop is like an "if" statement. It is no more "aware" of the algorithm you have in mind than this.

This only "knows" the value of x, not how many times the loop executes.
Last edited on
Pages: 12