Question

In: Computer Science

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 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!

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 !!


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

#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

Whenever I am attempting to write a simple program on C++ I get an error message...
Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
I am attempting to write a script using bash on Linux. My program must save the...
I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file. I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and...
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
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...
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
Write a program that calculates the amount a person would earn over a period of time...
Write a program that calculates the amount a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should display a table showing the salary for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Input Validation: Do not...
Hello I am needing an example of how to write an assembly (MIPS) code that with...
Hello I am needing an example of how to write an assembly (MIPS) code that with will ask the user for two numbers then for addition or multiplication by typing in + or * into the command prompt. For example if I type in the number 2 and 5 then + The code should add the sum between the two numbers like 2 + 3 + 4 + 5 = 14. If multiplication is implemented then it will do the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT