tags:

views:

561

answers:

9

Here is the problem:

One sheet contains 10 labels. I want to print many labels, but from an arbitrary starting index. Now, I want to find how many sheets would be required to print the labels that the user desires. For example, let's say I took starting index 52 and I want to print 23 labels. Then how do I find the number of sheets required? Does anyone have a formula to count this?

edit Hello guys, thanks for the answers the problem is solved now. thank u very much all.

+4  A: 

Ceil((52-10)/10)

code example

In Delphi this would be

function AmountOfSheets(startingIndex: Integer; labels: Integer): Integer;
const
  ILabelsPerSheet: Integer = 10;    
begin
  Result := Ceil((labels - startingIndex) / ILabelsPerSheet);
end;

Thanks to everyone who commented on getting it right, or to quote jc with a little help from my friends.

Lieven
I've interpreted "starting index" to be the first label position to use on the first page, which your code wouldn't account for...
Rowland Shaw
This code will produce an extra sheet if (labels - startingIndex) is wholly divisible by ILabelsPerSheet. You need to use the ceiling function like Pop Catalin's answer
Mark Pim
Lieven
@Lieven either that or the question is remarkably vague
Rowland Shaw
+1  A: 

The number of sheets required will always be the total you want to do divided by 10, rounded up. If the starting index takes away from the total to be printed it will be the total labels negative the starting index divided by 10, rounded up.

ceil ( Total Labels / 10 )

ceil ( (Total Labels - Index) / 10 )

A PHP example:

$starting_index=0;
$total=52 - $starting_index;
$labels_per_page=10;
$pages=ceil($total / $labels_per_page);
Sam152
+4  A: 

for example i took starting index 10 and i want to print 52 labels.then how i find the no of sheets required

Ceiling( ( 52 - 10 + 1 ) / 10 ) -> pages (Edit: make sure you perform floating point division instead of integer)

( 52 - 10 + 1 ) % 10 -> number of labels on last page

Pop Catalin
Actually, you needn't use floating point division; for positive integers, Ceil((float)x/y) is written in integer arithmetic as (x+y-1)/y
jpalecek
@jpalecek, you are right, that trick gave me a flashback from math classes in high school, I remember my math teacher teaching us that trick :), Cheers.
Pop Catalin
+1  A: 

Presuming the intention is to reuse half used sheets of labels you've got something like

labelsOnFirstPage = labelsPerPage + 1 - StartIndex
if( totalLabels > labelsOnFirstPage )
{
    return 1 + Ceiling( (totalLabels - labelsOnFirstPage) / labelsPerPage )
}
else
{
    return 1;
}
Rowland Shaw
A: 

⌈(52-10+1)/10⌉

vartec
⌈(52-10+1)/10⌉ = ⌈(43)/10⌉ = ⌈4.3⌉ = 5DONE!
Spoike
u cant understand my problem dear what if i took starting index 55 and want to print 5 labels.then how could u count no of sheets?
Rahul Vyas
+4  A: 

The number of pages required is (using integer arithmetics):

(items + pageSize - 1) / pageSize

where items is the number of labels to print and pageSize is the number of labels per page.

The starting index is irrelevant for the number of pages as long as you know how many labels you are going to print. If you print 25 labels it doesn't matter if the starting index is 4 or 827364827364, it's still 25 labels.


Edit:

If you want to use the index to optionally leave labels blank on the first page, i.e. print 19 labels from index 14 to index 32:

Page 1: -- -- -- 14 15 16 17 18 19 20
Page 2: 21 22 23 24 25 26 27 28 29 30
Page 3: 31 32 -- -- -- -- -- -- -- --

Then you first calculate how many labels to leave blank before the first label:

(index - 1) % pageSize

(where % is the modulus (or remainder) operator.)

Then you add these empty items to the number of labels to calculate the actual number of items to print. For the example above it would be 3 + 19 = 22. This would then be used in the calculation above to get the number of pages.

Guffa
but if take starting index 10 and i want to print 52 labels then it's answer shoul be 7(no of sheets) which is i am not getting in your formula
Rahul Vyas
52 labels takes 6 sheets. There must be some confusion as to what the index is used for
basszero
+1  A: 

I'm assuming from your very-hard-to-understand question that your "starting index" is a position on the page where the first label will be printed. Something like this:

1     2
3     4
5     6
7     8
9     10

So then the number of pages you print is the total number of labels you want, minus the labels you print on the first page, divided by the number of labels per page:

int labels_per_page = 10;
int starting_index = 10;
int total_labels = 52;
// Edit to account for a starting_index of more than labels_per_page:
int first_page = labels_per_page - ((starting_index - 1) % labels_per_page);
// first_page has one label
int remaining_labels =  total_labels - first_page;
if (remaining_labels <= 0) { return 1; }
// 51 labels left
int total_pages = ceiling(remaining_labels / labels_per_page) + 1;
// 7 pages total
return total_pages;

You'll need to make sure that first_page is greater than total_labels since there might only be one page.

Welbog
what if i take starting index 25 than the result of first page will be negative
Rahul Vyas
Then take the modulus of it in the size of the page. I'll update my answer.
Welbog
+5  A: 

I guess you mean that you have a predetermined page layout, i.e. if you want to print labels 42 to 67, you have to print the pages with the labels 40-49, 50-59, and 60-69.

All you need to do is find the sheet index of the starting page and the sheet index of the ending page, subtract them, and add 1. The sheet index of label n is floor(n/10) (or just n/10 with integer division). So, if your starting label is n, and you want to print x labels, the number of sheets you need is:

floor((n+x)/10) - floor(n/10) + 1

This all assumes zero-based indices, for sanity (that means that the first label has index 0). If your label numbers are one-based, i.e. the first label has the index 1, then you need to subtract one from n first, so the formula becomes:

floor((n-1+x)/10) - floor((n-1)/10) + 1

This should be easy to understand.

Svante
yes u've got the problem ur answer is absolutely right
Rahul Vyas
+1  A: 

With the same interpretation as Svante, with indices starting at 1, I would write the answer as

nSheets = ceiling((startIndex+nLabels) / 10) - ceiling(startIndex / 10) + 1

For the example given, with 52 labels starting from index 10, we have labels numbered until index 61 (=10+52-1):

                           10 (end of first sheet -> 1 label)
11 12 13 14 15 16 17 18 19 20 (second sheet -> 11 labels)
21 22 23 24 25 26 27 28 29 30 (third sheet -> 21 labels)
31 32 33 34 35 36 37 38 39 40 (fourth sheet -> 31 labels)
41 42 43 44 45 46 47 48 49 50 (fifth sheet -> 41 labels)
51 52 53 54 55 56 57 58 59 60 (sixth sheet -> 51 labels)
61                            (beginning of seventh sheet -> 52 labels)

so that 7 sheets are needed, and we can check

ceiling((10+52)/10)-ceiling(10/10)+1 = ceiling(6.2)-ceiling(1)+1 = 7-1+1 = 7
Fanfan