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...
Use c++language Use the pseudocode description below to write a program that uses an if, else...
Use c++language Use the pseudocode description below to write a program that uses an if, else if, else decision structure for a program that will determine if someone lives in Boston. 1. display message that describes what the program will do. 2. ask the user to input an answer to the question: Do you live in Boston?. 3. if they entered 'y', display a message confirming that they live in Boston. 4. if they entered 'n' , display a message...
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...
Using C language, and describe the algorithm design All the sample is correct Q3 Problem description...
Using C language, and describe the algorithm design All the sample is correct Q3 Problem description Tim was born in a leap year, so he would like to know when he could have his birthday party. Could you tell him? Given a positive integers Y indicating the starting year, and a positive integer N, your task is to tell the N-th leap year from year Y. Note: if year Y is a leap year, then the 1st leap year is...
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...
1.Describe the problem at IFG as succinctly as you can. Use this description to identify the...
1.Describe the problem at IFG as succinctly as you can. Use this description to identify the main stakeholders. 2. IFG can’t afford the resources to identify, define, cleanse, and validate all of its data. On the other hand, building yet another data mart to address a specific problem worsens the data situation. Propose a solution that will enable IFG to leverage a key business problem/opportunity using their BI tools that does not aggravate their existing data predicament. Please provide 3-4...
Using C language, and describe the algorithm design Q2 Problem description n (n is odd) people...
Using C language, and describe the algorithm design Q2 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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT