0
$\begingroup$

So I am working on a program that does a scrolling effect. the image is 1200pixels wide, and each time it scrolls, it should move an average of 2.67 pixels. However I am using a function that does not accept decimal points. so it has to move either 2 or 3. I know there will be 450 total steps. So my question is there way way to find a series of exact number of 2 and 3s I will need to get 2.67 as an average, and know that there are 450 numbers in the set.

$\endgroup$
3
$\begingroup$

If you have 1/3 of the numbers in your sequence as 2 and 2/3 as 3, then you will have the average you are looking for.

So you would want to have a collection of 150 2's and 300 3's. This gives you an average of 2.6666 which rounds to 2.67.

$\endgroup$
0
$\begingroup$

Here's some sample code that calls yourFunction 450 times, each time passing either the value 2 or the value 3, and the sum of the parameters passed to the 450 function calls will be 1200. It also scrolls about as smoothly as you can, that is, any $n$ iterations of the loop will scroll a total of about $n \times 8/3$ pixels.

for (int i = 1; i <= 450; ++i)
{
  int previous_total_scrolled = 1200 * (i - 1) / 450;
  int new_total_scrolled      = 1200 * i / 450;
  int scrolling_increment     = new_total_scrolled - previous_total_scrolled;
  yourFunction(scrolling_increment);
}

This is just to give you an idea about how the integer arithmetic works for this problem. You need to adapt this to your desired program control flow, of course, and it can be optimized as well. (For example, in an actual program I probably wouldn't want to keep recomputing previous_total_scrolled when I know I computed the exact same result during the last iteration of the loop.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.