In: Computer Science
Hello, I am attempting to write a program which calculates the amount of fence pieces needed to achieve a certain distance. The program should take in the amount of fencing needed from the user, then ask them the size of the smaller piece. The fencing is to be made up of two sizes of fencing, the smaller being two feet less than the larger. Then, the program will tell the user how many of each piece is needed. However, I am having trouble understanding how the calculation should be performed. The assignment gives examples, so any help interpreting it would be appreciated.
A user enters that they need 50 feet of fencing and the smaller piece must be 3 feet. The program determines that the larger panel will be 5 feet. Then, the program determines that 6 3-foot pieces and 6 5-foot pieces are needed, plus an additional 2-foot piece.
A user enters that they need 50 feet of fencing and the smaller piece must be 4 feet. The program determines that the larger panel will be 6 feet. Then, the program determines that 5 4-foot pieces and 5 6-foot pieces are needed.
A user enters that they need 2 feet of fencing and the smaller piece must be 1 feet. The program determines that the larger panel will be 3 feet. Then, the program determines that 2 1-foot pieces and 0 3-foot pieces are needed.
A user enters that they need 52 feet of fencing and the smaller piece must be 3 feet. The program determines that the larger panel will be 5 feet. Then, the program determines that 7 3-foot pieces and 6 5-foot pieces are needed, plus an additional 1-foot piece.
Thank you!
Thanks for the question.
Below is the code you will be needing Let me know if you have
any doubts or if you need anything to change.
Thank You !!
===========================================================================
#include<stdio.h>
#include <math.h>
int get_fence_length(){
int length;
printf("Measurement of the fencing needed (feet):
");
scanf("%d",&length);
while(length<=0){
printf("Please enter an integer
> 0: ");
scanf("%d",&length);
}
return length;
}
int get_panel_length(){
int length;
printf("Enter the size of smaller panel in feet:
");
scanf("%d",&length);
while(length<=0){
printf("Please enter an integer
> 0: ");
scanf("%d",&length);
}
return length;
}
int main(){
int length = get_fence_length();
int smaller_panel_length = get_panel_length();
int larger_panel_length =
smaller_panel_length+2;
int equalCount =
length/(2*smaller_panel_length+2);
int smaller_count=0,larger_count=0,
special_length=0;
if(equalCount*(smaller_panel_length+larger_panel_length)==length){
smaller_count=larger_count=equalCount;
special_length =0;
}else{
int total =
equalCount*(smaller_panel_length+larger_panel_length);
int remaining = length -
total;
if(remaining<smaller_panel_length){
smaller_count=larger_count=equalCount;
special_length=remaining;
}else{
smaller_count=
equalCount+1;
larger_count =
(length -
smaller_count*smaller_panel_length)/larger_panel_length;
special_length =
length - smaller_count*smaller_panel_length -
larger_count*larger_panel_length;
}
if(special_length>=smaller_panel_length){
smaller_count
+=special_length/smaller_panel_length;
special_length%=smaller_panel_length;
}
}
printf("The larger panel will be
%d\n\n",larger_panel_length);
printf("Happy Building!!\n\n");
printf("The fence will need %d qty %d foot panel(s)
and %d qty %d foot panel(s)",
smaller_count,smaller_panel_length,larger_count,larger_panel_length
);
if(special_length!=0){
printf("\nPlus a special order %d
foot panel.\n",special_length);
}
}
===================================================================