A sawmill operates a robot controlled power saw to cut planks into standard-lengths. The machine can be pre-set to the number of standard-lengths to be cut and the length of a standard-length. In addition, it maintains a record of the number of standard-lengths cut, the number of planks used and the total amount of timber that was too short to produce a standard-length.
Design a program to simulate the action of the machine using numbers entered from the keyboard to represent planks. The values on the counters should be printed before the program terminates.
C++:
#include <stdio.h> int main(int argc, char *argv[]){ int iStandardLength, iAmount, iPlankLength, iUnused = 0, iCut = 0; // get the standard length printf("Enter the standard-length in centimeters:\n>"); scanf("%d", &iStandardLength); // get the amount to cut printf("Enter the number of standard-lengths to cut:\n>"); scanf("%d", &iAmount); // loop until enough standard lengths have been made while (iCut < iAmount) { // get the plank size from the operator printf("Enter a plank into the machine and enter the length in centimeters:\n>"); scanf("%d", &iPlankLength); // if it would make too many if (iCut + (iPlankLength / iStandardLength) > iAmount) { // don't slice up bits that could be used for something iUnused += iPlankLength - ((iAmount - iCut) * iStandardLength); // just make enough iCut = iAmount; } // if it wouldn't make enough else { // make as many as possible iCut += iPlankLength / iStandardLength; // store the rest iUnused += iPlankLength % iStandardLength; } // show the totals so far printf("%d total standard-lengths cut. %dcm total unused timber.\n", iCut, iUnused); } return 0;}Here's an example run through the program:
C:\code>sawmill
Enter the standard-length in centimeters:
>5
Enter the number of standard-lengths to cut:
>7
Enter a plank into the machine and enter the length in centimeters:
>13
2 total standard-lengths cut. 3cm total unused timber.
Enter a plank into the machine and enter the length in centimeters:
>4
2 total standard-lengths cut. 7cm total unused timber.
Enter a plank into the machine and enter the length in centimeters:
>15
5 total standard-lengths cut. 7cm total unused timber.
Enter a plank into the machine and enter the length in centimeters:
>23
7 total standard-lengths cut. 20cm total unused timber.