[Question 1] ISC 2017 Computer Practical Paper Solved – Box Packing
ISC 2017 box packing program solved with algorithm, explanation, Java program and equivalent Python code.
Click here to download the complete ISC 2017 Computer Science Paper 2 (Practical).
Question:
A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity (i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the sample data and some random data:
Example 1
INPUT : N = 726
OUTPUT :
48 x 15 = 720
6 x 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
Example 2
INPUT : N = 140
OUTPUT :
48 X 2 = 96
24 x 1 = 24
12 x 1 = 12
6 x 1 = 6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6
Example 3
INPUT : N = 4296
OUTPUT : INVALID LENGTH
Algorithm:
Step 1: Start.
Step 2: Input the number of boxes N.
Step 3: If N is less than 1 or greater than 1000, display INVALID INPUT and stop.
Step 4: Store the carton capacities 48, 24, 12 and 6 in descending order.
Step 5: For each carton capacity, divide the remaining boxes by the capacity to find how many cartons of that size are needed.
Step 6: Display the capacity, number of cartons and number of boxes packed by that capacity.
Step 7: Replace N by the remainder after using that carton capacity.
Step 8: If boxes still remain after using all standard carton sizes, count one extra carton for them.
Step 9: Display the total number of boxes and total number of cartons.
Step 10: Stop.
Explanation:
The box packing problem asks for the minimum practical break-up of cartons using fixed carton sizes. Since the carton sizes are 48, 24, 12 and 6, the best approach is to use the largest carton first and then move to the smaller sizes. This greedy method works here because each carton size is a multiple of the smaller sizes. The program stores the carton capacities in descending order and processes them one by one.
For each carton size, integer division gives the number of cartons that can be completely filled. The remainder then becomes the number of boxes still left to pack. The program prints only carton sizes that are actually used. After all standard carton sizes have been checked, if any boxes are still left, one extra carton is counted for those remaining boxes. Finally, the original number of boxes and the total cartons required are displayed. This matches the practical output format and keeps the calculation simple enough for an ISC program.
For the sample value 815, the program first uses sixteen cartons of 48, packing 768 boxes and leaving 47. It then uses one carton each of 24, 12 and 6, leaving 5 boxes. Since 5 cannot fill any standard carton size, one additional carton is counted for the remaining boxes. This dry run also explains why the original input is stored separately: after repeated division and remainder operations, the working value changes, but the final output still needs to display the original number of boxes.
Programming Code:
/**
* The class BoxPacking_ISC2017 inputs number of boxes to be packed
* and display the break-up of the cartons used in descending order of capacity
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2017 Question 1
*/
import java.util.*;
class BoxPacking_ISC2017
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of boxes to be packed : ");
int N = sc.nextInt();
if(N<1 || N > 1000)
{
System.out.println("INVALID INPUT");
}
else
{
int cart[] = {48, 24, 12, 6};
int copy = N;
int totalCart = 0,count = 0;
System.out.println("OUTPUT :");
for(int i=0; i<4; i++)
{
count = N / cart[i];
if(count!=0)
{
System.out.println("\t"+cart[i]+"\tx\t"+count+"\t= "+cart[i]*count);
}
totalCart = totalCart + count;
N = N % cart[i];
}
if(N>0)
{
System.out.println("\tRemaining Boxes "+N+" x 1 = "+N);
totalCart = totalCart + 1;
}
else
{
System.out.println("\tRemaining Boxes\t\t= 0");
}
System.out.println("\tTotal number of boxes = "+copy);
System.out.println("\tTotal number of cartons = "+totalCart);
}
}
}Equivalent Python Program:
n = int(input("Enter number of boxes to be packed : "))
if n < 1 or n > 1000:
print("INVALID INPUT")
else:
cartons = [48, 24, 12, 6]
original = n
total_cartons = 0
print("OUTPUT :")
for carton in cartons:
count = n // carton
if count != 0:
print(" " + str(carton) + " x " + str(count) + " = " + str(carton * count))
total_cartons += count
n = n % carton
if n > 0:
print(" Remaining Boxes " + str(n) + " x 1 = " + str(n))
total_cartons += 1
else:
print(" Remaining Boxes = 0")
print(" Total number of boxes =", original)
print(" Total number of cartons =", total_cartons)Output:
Enter number of boxes to be packed : 815 OUTPUT : 48 x 16 = 768 24 x 1 = 24 12 x 1 = 12 6 x 1 = 6 Remaining Boxes 5 x 1 = 5 Total number of boxes = 815 Total number of cartons = 20
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.