-5

I am doing a program that involves integer and float number.Let say I want to calculate atx={1,1/2,2,3,4} and want to use for-loop. But I know the condition of increment

for(x=1;x<=4;x++)

as x++=x+1.

I want to find the iteration at x={1,2,3,4} and at x={1/2}. But I do not have idea how to modify the for-loop statement; either to make the increment of 0.5 or 1. But if I set 0.5, I will get the answers for 5/2 and 7/2 instead.

Mazaya Jamil
  • 285
  • 1
  • 4
  • 7
  • 1
    You need a book - http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – T.C. Jun 09 '14 at 05:57
  • What do you mean by "two different conditions of increment"? There's only one condition in the middle along with initial assignment and step assignment – phuclv Jun 09 '14 at 06:03
  • Your question is not really clear, could you explain once what it is you actually want to do? – Kartik_Koro Jun 09 '14 at 06:10
  • With "two different conditions of increment" he probably means that the increment between values 1, 2, 3 and 4 is one, while the "increment" from 1 to 0.5 is -0.5. – Rudy Velthuis Jun 09 '14 at 08:47
  • yes. it is true @RudyVelthuis. – Mazaya Jamil Jun 09 '14 at 08:53
  • Then it may be better to edit your question, to make this clear. Note that in a for loop, you can't have different increments. But since you are incrementing the index, and not the values, that is not necessary, as the index should increment by 1 anyway. You will have to detect the differences of the values in the array by yourself (keep a record of the previous difference and if it is different, act upon that fact). – Rudy Velthuis Jun 09 '14 at 08:57

2 Answers2

1

This should work:

for (int i=1; i<=4; i++) {
    for (float j=i; j<=std::max(1.6, i); j+=0.5) {
        cout << j << endl;
    }
}

The above code will print the desired sequence.

Oliv
  • 10,221
  • 3
  • 55
  • 76
0

If 0.5 is the only anomaly in your list of numbers, then it can be done using something like this:

for (float x = 1; x < 5; x += (x < 2 ? .5f : 1))
    std::cout << x << std::endl;

This should - if I didn't make any mistake - print the following:

1 1.5 2 3 4

However, this is really hard to read and depending on your conditions it might get even more complicated. This not only makes the code hard to read, it also adds overhead and possibly slows down processing. As such, you should - based on your values - really consider shekhar's solution.

Instead of modifying x in the last part of the for(), you can also do it inside the loop to make it more readable:

for (float x = 1; x < 5;) {
    std::cout << x << std::endl;
    if (x < 2)
        x += .5f;
    else
        ++x;
}
Mario
  • 35,726
  • 5
  • 62
  • 78
  • May I know what does the (std::cout << x << std::endl;) stands for? @Mario – Mazaya Jamil Jun 09 '14 at 06:17
  • It really just prints the number to the console (window). – Mario Jun 09 '14 at 06:18
  • Thus this means that I do not have to declare `float x` before begin the **for-loop**? – Mazaya Jamil Jun 09 '14 at 06:52
  • You can do it as part of the first parameter (the way I did). The declaration is valid everywhere inside that block, not just inside the round brackets. – Mario Jun 09 '14 at 07:33
  • Therefore, by declaring it once in the first **for-loop**, I does not need to do the same command on the next loop. Is that what you want to say? I am sorry, I quite a slow learner. @Mario – Mazaya Jamil Jun 09 '14 at 07:50
  • You can (re-)name your iterating variable for each and every block. Like next time you may call it `y`, `i`, or something completely different. – Mario Jun 09 '14 at 08:59