Question

In: Computer Science

Write a C program to calculate the number of fence panels needed over a given distance....

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).

Solutions

Expert Solution

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);
   }
  
  
}

=================================================================


Related Solutions

Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Using C++ code, write a program to convert a distance d given in inches (in) to...
Using C++ code, write a program to convert a distance d given in inches (in) to centimeters (cm), where d is input by a user via the keyboard and 1in = 2.54cm. Your submission should include screenshots of the execution of the program using the values 1.5, 4 and 6.75.
Hello, I am attempting to write a program which calculates the amount of fence pieces needed...
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...
Write the equation needed to calculate the distance between the centers of the balls, R, as...
Write the equation needed to calculate the distance between the centers of the balls, R, as a function of the locations of the center of each ball where X is the left ball
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
Calculate the launch velocity needed to successfully land the cannon ball at each distance, given the...
Calculate the launch velocity needed to successfully land the cannon ball at each distance, given the following conditions: launch angle (θ) was 27.5°, the launch height (yi) was 9.59 m, the height of the landing pad (yf) was 2.50 m, and the range (R) is the cannon-landing site distance. Cannon-landing site distance (m) Launch Velocity (m/s) 68 75 245
Write a c program to calculate the factorial of a number using recursion    [8] Question five...
Write a c program to calculate the factorial of a number using recursion    [8] Question five Write a stack algorithm to POP an item                                                         [6] What does FRONT and REAR signify in a queue?                                                                 [6] Write an algorithm for a dequeue operation                                                                       [8]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT