Question

In: Computer Science

Use C language Problem 1: Buy cheese (Level 1) • Problem description You have M dollors...

Use C language

Problem 1: Buy cheese (Level 1)
• Problem description
You have M dollors and want to buy as much cheese as possible. There is a cheese shop with
two kinds of cheese. The unit prices of the both cheese are p1 and p2, the total amount of them
are a1 and a2. You can buy either cheese or both, but the amount you buy cannot exceed its total
amount. Write a program to output the largest amount of cheese you can buy.
• Input & output requirements
All the integers variables (M, p1, a1, p2, and a2) are taken from user input. The input begins
with a line containing one non-negative interger M, and followed by two lines, each line
contains two integers, unit price and total amount.
The output should be a real number accurate up to 2 decimal places.
• Sample results
Sample 1
Please input M: 2↲
Please input p1 and a1: 1 2↲
Please input p2 and a2: 2 2↲
The maximum amount of cheese is 2.00
Sample 2
Please input M: 3↲
Please input p1 and a1: 2 2↲
Please input p2 and a2: 1 1↲
The maximum amount of cheese is 2.00

Solutions

Expert Solution

Note:AS PER THE QUESTION THE ANSWER CAN BE FRACTIONAL/FLOAT ALSO.

EXAMPLE;

4
1 3
2 2

ANSWER IS 3.50.(3 items from 1st type and 0.5 items from second)

#include <stdio.h>

void main()
{
int a1,a2,p1,p2;
float dollars,ans;
scanf("%f",&dollars);
scanf("%d %d",&p1,&a1);
scanf("%d %d",&p2,&a2);
if(p1<p2) //will consider lowest unit price first
{
  
while(a1 && dollars>=p1)
{
dollars=dollars-p1;
ans=ans+1;a1--;
}
if(a1>0) //still items left
{   
printf("%.2f",ans+dollars/p1);
return;
}
while(a2 && dollars>=p2) //if first type exhausted,will go for next
{
dollars=dollars-p2;
ans=ans+1;a2--;
}
if(a2>0)
{
printf("%.2f",ans+dollars/p2);
return;
}
}

else //same process but p1>=p2;either above or this part!!!
{
while(a2 && dollars>=p2)
{
dollars=dollars-p2;
ans=ans+1;a2--;
}
if(a2>0)
{
printf("%.2f",ans+dollars/p2);printf("secondYes");
return;
}
while(a1 && dollars>=p1)
{
dollars=dollars-p1;printf("Yes");
ans=ans+1;a1--;
}
if(a1>0)
{   
printf("%.2f",ans+dollars/p1);
return;
}
}

printf("%.2f",ans);
return;
}


Related Solutions

Use C language Level 2 Problem description n (n is odd) people sitting around a round...
Use C language Level 2 Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two...
Class object in C++ programming language description about lesson inheritance multi level example.
Class object in C++ programming language description about lesson inheritance multi level example.
Language C++ *note* Your function count should match those given in the description; you have no...
Language C++ *note* Your function count should match those given in the description; you have no need for extra functions beyond that, and you should not have less. -Write a function, doTheRegression(), that takes as input a variable of type integer and returns a value of type double. It computes and returns 80% of the value given as the amount after reduction. -In your main function, write code that asks the user what the original amount is, and if they...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students...
Problem: Make linkedList.h and linkList.c in Programming C language Project description This project will require students to generate a linked list of playing card based on data read from a file and to write out the end result to a file. linkedList.h Create a header file name linkedList Include the following C header files: stdio.h stdlib.h string.h Create the following macros: TRUE 1 FACES 13 SUITS 4 Add the following function prototypes: addCard displayCards readDataFile writeDataFile Add a typedef struct...
Translate the following C code into M4K assembly language. You do not have to use the...
Translate the following C code into M4K assembly language. You do not have to use the frame pointer, just use $sp if you need to use the stack. You do not have to show the stack initialization nor stack cleanup. If you need a specific value for an address, just make an assumption. int A; main() { int B = 5; B = A+B }; // main //Disassembly starts here !main() { //stack and frame pointer init // you do...
Be sure to use only C for the Programming Language in this problem. Before we start...
Be sure to use only C for the Programming Language in this problem. Before we start this, it is imperative that you understand the words “define”, “declare” and “initialize” in context of programming. It's going to help you a lot when following the guidelines below. Let's begin! Define two different structures at the top of your program. be sure to define each structure with exactly three members (each member has to be a different datatype). You may set them up...
Using C language Problem description n (n is odd) people sitting around a round table playing...
Using C language Problem description n (n is odd) people sitting around a round table playing a game. In this situation, everyone has a left neighbour and a right neighbour. At the beginning, each of them is holding a whiteboard with an integer number. Now, they are playing a game consisting of several rounds. In each round, everyone will first look at the numbers of his/her left neighbour and right neighbour, then calculate the average of the two numbers, replace...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
For this problem, use an annual interest rate of 4%. On 1/1/2020, you buy a perpetuity...
For this problem, use an annual interest rate of 4%. On 1/1/2020, you buy a perpetuity paying you $10,000 at the beginning of each year, commencing on 1/1/2020.  (Recall that a perpetuity is an annuity that does not end.) (a)        Calculate the present value of the perpetuity as of 1/1/2020. (b)       After receiving exactly ten payments, you exchange the perpetuity on 1/1/2030 for an annuity paying $x at the beginning of each year for 20 years, commencing on 1/1/2030.  (Note:  Since you have received exactly...
Done in C language using mobaxterm if you can but use basic C This program is...
Done in C language using mobaxterm if you can but use basic C This program is broken up into three different functions of insert, delete, and main. This program implements a queue of individual characters in a circular list using only a single pointer to the circular list; separate pointers to the front and rear elements of the queue are not used. The linked list must be circular.      The insert and remove operations must both be O(1)    You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT