Question

C++ Program Farmer Pete will provide a number of sheep and a number of chocolate bars....

C++ Program

Farmer Pete will provide a number of sheep and a number of chocolate bars. He needs your help to write a program to calculate how many bars each sheep gets and how many chocolate bars are left over. To avoid that some sheep be upset, Farmer Pete would like to ensure that at any give time all sheep would receive the same number of chocolate bars. However, if sheep get too much chocolate it is unhealthy for them. So one sheep can only consume up to 4 chocolate bars before they get sick. Use functions for input/output and each calculation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

void readData(int &numberOfsheeps , int &numberOfBars){
   cout<<"Enter number of sheeps : ";
   cin>>numberOfsheeps;
  
   cout<<"Enter number of chocolate bars: ";
   cin>>numberOfBars;
}
void calculate(int numberOfsheeps ,int numberOfBars ,int &remainingBars ,int &BarsGivenToEachSheep){
   int num = numberOfBars/numberOfsheeps;
   if(num <= 4){
       BarsGivenToEachSheep = num;
       remainingBars = numberOfBars - BarsGivenToEachSheep*numberOfsheeps;
   }
   else{
       BarsGivenToEachSheep = 4;
       remainingBars = numberOfBars - BarsGivenToEachSheep*numberOfsheeps;
   }
}
void display(int remainingBars ,int BarsGivenToEachSheep){
   cout<<"chocolate bars each sheep gets : "<<BarsGivenToEachSheep<<endl;
   cout<<"chocolate bars are left over : "<<remainingBars<<endl;
}

int main(){
   int numberOfSheeps , numberOfBars;
   int remainingBars , BarsGivenToEachSheep;
   readData(numberOfSheeps , numberOfBars);
   calculate(numberOfSheeps , numberOfBars , remainingBars , BarsGivenToEachSheep);
   display(remainingBars , BarsGivenToEachSheep);
   return 0;  
}

Add a comment
Know the answer?
Add Answer to:
C++ Program Farmer Pete will provide a number of sheep and a number of chocolate bars....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program C++ (Write comments for each code)Generate a number from 1 to 10, inclusive....

    Write a program C++ (Write comments for each code)Generate a number from 1 to 10, inclusive. Prompt the user to guess an integer number. If the guess is incorrect, indicate to the user whether it is too low or too high. Then, prompt the user to guess again. If the guess is correct, indicate the correct answer and how many tries it took the get the correct answer.

  • Kaprekar C++ program a. Write a c++  code that takes any number between 0 and 9999 as...

    Kaprekar C++ program a. Write a c++  code that takes any number between 0 and 9999 as an input value, and returns the number of iterations it takes that number to reach 6174. Also, it needs to indicate if the number does not converge to 6174. Run the program with three different numbers of your choosing to show that it works. b. Write a second c++ code using the first. However, this new program will calculate how many iterations it takes...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • Assignment Overview This programming assignment is intended to demonstrate your knowledge of the following:  Writing...

    Assignment Overview This programming assignment is intended to demonstrate your knowledge of the following:  Writing a while loop  Writing a for loop  Writing a while loop with a sentinel value Chocolate Coupons Foothill Fro-cho, LLC, gives customers a coupon every time they purchase a chocolate bar. After they earn a certain number of coupons, they qualify for a free chocolate bar, which they may use toward the purchase of a single chocolate bar. Usually, 7 is the...

  • This C++ Program should be written in visual studio 2017 You are to write a program...

    This C++ Program should be written in visual studio 2017 You are to write a program that can do two things: it will help users see how long it will take to achieve a certain investment goal given an annual investment amount and an annual rate of return; also, it will help them see how long it will take to pay off a loan given a principal amount, an annual payment amount and an annual interest rate. When the user...

  • I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...

    I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...

  • Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one...

    Convert Project #1 into a GUI program, following the following requirements: Provide two text fields: one receives the length of the sequence, and the second receive the sequence (each number separated by a comma). There may, or may not, be space in between the comma and the number element. Provide a clear button that has an event listener attached to it. When pressed, the two text fields are emptied. The event listener should be defined and implemented as an anonymous...

  • C++ programming help. The only change you have to make is in current program, where it...

    C++ programming help. The only change you have to make is in current program, where it says "your code goes here", do not make any changes to any other files, they are just there to show you. This code should be written for arbitrary data, not specifically for this sequence of data. The only place you need to write a code is marked YOUR CODE GOES HERE. You have been supplied a file with data in it (data2.txt). The line...

  • the goal is to create a c++ programs with small functions where the main delegates series...

    the goal is to create a c++ programs with small functions where the main delegates series of functions where the real work takes place. In this programming assignment, you are not allowed to use global variables. Avoid using break (unless working with a switch statement). Limit your functions to no more than 30 statements of code (not including comments or blank lines). Never use a “return” in the middle of a loop! Program Assignment: When I went to Paris over...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT