Question

In: Computer Science

c++ code problems : A farmer buys 100 animals for$100.00.  The animals include at least one...

c++ code problems : A farmer buys 100 animals for $100.00.  The animals include at least one cow, one pig, and one chicken, but no other kind.

If a cow costs $10.00, a pig costs $3.00, and a chicken costs $0.50, how many of each did he buy?

please using a loop.

Solutions

Expert Solution

C++ code with complete explanation in the comments:

#include
using namespace std;
int main()
{
   int max_cow=100/10;//max number of cows that can be bought in 100 dollar
   int max_pig=100/3;//max number of pigs that can be bought in 100 dollar
   for(int i=1;i<=max_cow;i++)//to keep count of cows
   {
       for(int j=1;j<=max_pig;j++)//to keep count ofpigs
       {
           int cost=i*10+j*3;
           int remaining =100-i-j;//number of chickens
           if(remaining%2==0)//this is because if chickens are in odd number then their cost will become fraction
           {//and as it should be integer
               int costofchicken=remaining/2;
               cost+=costofchicken;
               if(cost==100)
               cout<            }
       }
   }
   return 0;
}

Output:


Related Solutions

A farmer raises only sheep and geese. She wants to raise at least 16 animals, including...
A farmer raises only sheep and geese. She wants to raise at least 16 animals, including 7 sheep. She spends $34 to raise a sheep and $15 to raise a goose, and she has $1020 availble for this project. Write a system of inequalities that describe all the given conditions. Graph the feasible region.
Would you make separated this code by one .h file and two .c file? **********code************* #include...
Would you make separated this code by one .h file and two .c file? **********code************* #include <stdlib.h> #include <stdbool.h> #include <stdio.h> #include<time.h> // Prints out the rules of the game of "craps". void print_game_rule(void) { printf("Rules of the game of CRAPS\n"); printf("--------------------------\n"); printf("A player rolls two dice.Each die has six faces.\n"); printf("These faces contain 1, 2, 3, 4, 5, and 6 spots.\n"); printf("After the dice have come to rest, the sum of the spots\n on the two upward faces is...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Today one U.S. farmer produces enough to feed: a. 100 people b. 20 people c. 5...
Today one U.S. farmer produces enough to feed: a. 100 people b. 20 people c. 5 people d. 1000 people
c# Please write unit tests for this code, at least one or two that I can...
c# Please write unit tests for this code, at least one or two that I can understand how to write it. using System; namespace PrimeNumberFactors { class Program { static void Main(string[] args) { Console.Write("Enter a number you want to check: ");    if (int.TryParse(Console.ReadLine(), out int num)) { PrimeFactors(num); } else { Console.WriteLine("Invalid input!!"); } Console.ReadKey(); } public static void PrimeFactors(int num) { Console.Write($"Prime Factors of {num} are: ");    while (num % 2 == 0) { Console.Write("2 ");...
Please Write Code in C++ and include the correct #include <header> and not a catchall such...
Please Write Code in C++ and include the correct #include <header> and not a catchall such as bits/stdc: Write a recursive, string-valued function, replace, that accepts a string and returns a new string consisting of the original string with each blank replaced with an asterisk (*) Replacing the blanks in a string involves: Nothing if the string is empty Otherwise: If the first character is not a blank, simply concatenate it with the result of replacing the rest of the...
Write a 100 to 200-word statement connected to Social Media, include at least one specific reference...
Write a 100 to 200-word statement connected to Social Media, include at least one specific reference (okay if it is from another source). Make sure to include two positives and two downsides to using Social Media in Healthcare.  
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argb, char *argv[]){ char ch; int upper=1; if(argb>1){ if(strcmp(argv[1],"-s")==0){ upper=1; } else if(strcmp(argv[1],"-w")==0){ upper=0; } } while((ch=(char)getchar())!=EOF){ if(upper){ printf("%c",toupper(ch)); } else{ printf("%c",tolower(ch)); } } return 0; } ======================================================== #include <stdio.h> #include <stdlib.h> int calcRange(int array[],int size){ int max=array[1]; int min=array[0]; int a=0; for(a=1;a<size;a++){ if(array[a]>max){ max=array[a]; } } for(a=1;a<size;a++){ if(array[a]<min){ min=array[a]; } } printf("%d-%d=%d\n",max,min,max-min); } int main(int arga, char **argv){...
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include...
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; class TowersOfHanoi { friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers); public: //constructor TowersOfHanoi(); //custom methods unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout); private: //custom methods void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux); //temporary variable unsigned _n_moves; //data const unsigned _n_towers; stack<unsigned>* _tower; }; #include "TowersOfHanoi.h" // ostream& operator<<(ostream& sink, const...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT