In: Computer Science
Write a C program to calculate the number of fence panels needed over a given distance. The fence must be created using two difference size panels which have a 2 foot length difference between them (ie. panels could be 2’ and 4’ or 3’ and 5’ or 5’and 7’). Your program should request the total length of the fence and the smaller panel's size. Your program will then calculate the length of the larger panel and output the number of each size panel necessary for the given length of fence. If there is a length of fence that is different from the two panel sizes, you should consider that to be a special size panel that is necessary to complete the length of fence. Output the length of that special panel. The examples below show different inputs and the expected outputs.
Here is an example of running your program with different
inputs.
NOTE: Bold text represents input that someone will type
into your program.
Measurement of the fencing needed (feet): 50 Enter the size of smaller panel in feet: 3 The larger panel will be 5 feet. Happy Building!! The fence will need 6 qty 3 foot panel(s) and 6 qty 5 foot panel(s). Plus a special order 2 foot panel. ----------------------------------- Measurement of the fencing needed (feet): 50 Enter the size of smaller panel in feet: 4 The larger panel will be 6 feet. Happy Building!! The fence will need 5 qty 4 foot panel(s) and 5 qty 6 foot panel(s). ----------------------------------- Measurement of the fencing needed (feet): 2 Enter the size of smaller panel in feet: 1 The larger panel will be 3 feet. Happy Building!! The fence will need 2 qty 1 foot panel(s) and 0 qty 3 foot panel(s). ----------------------------------- Measurement of the fencing needed (feet): 52 Enter the size of smaller panel in feet: 3 The larger panel will be 5 feet. Happy Building!! The fence will need 7 qty 3 foot panel(s) and 6 qty 5 foot panel(s). Plus a special order 2 foot panel. ----------------------------------- Measurement of the fencing needed (feet): 0 Please enter an integer > 0: 54 Enter the size of smaller panel in feet: -34 Please enter an integer > 0: 4 The larger panel will be 6 feet. Happy Building!! The fence will need 6 qty 4 foot panel(s) and 5 qty 6 foot panel(s). |
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 !!
Measurement of the fencing needed (feet): 52 Enter the size of smaller panel in feet: 3 The larger panel will be 5 feet. Happy Building!! The fence will need 7 qty 3 foot panel(s) and 6 qty 5 foot panel(s). Plus a special order 2 foot panel.
The above example is incorrect, special order should be 1 foot panel = 52 - 7*3 - 6*5;
===========================================================================
#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);
}
}
=================================================================