In: Computer Science
Assignment #3
Introduction to C Programming – COP 3223
Objectives
Introduction: Mission to Mars
Your friend has been playing a new Mars Colony simulator nonstop! They are always talking about how cool it would be if they could be a on the first real-life mission to Mars! To amuse your friend, you have decided to create a series of programs about the possible first colony on Mars.
Problem: Where Can We Get the Best Deal? (marssupplier.c)
Now that we know how much fuel we need and how much equipment we can take, we need to determine which supplier will give us the best deal on what we need to purchase. We will poll a number of suppliers to see what kind of pricing they can give us and select the supplier who has the best deal.
In this program, we want to ask the user for information about suppliers. We can assume that there will be at least one supplier, but we will not know ahead of time how many suppliers there might be. After each supplier’s information, ask the user if there is another supplier to consider.
For each supplier, ask the user for the deal that the supplier is willing to offer. Keep track of the best possible deal and which supplier (identified as a number: 1, 2, 3, etc.) is offering it. After all the suppliers have been considered, tell the user which supplier offered the best deal.
Input Specification
Output Specification
For each supplier, prompt the user with:
What is the price for supplier #X?
To ask about additional suppliers, prompt the user with:
Is there another supplier to consider?
For the final print out, tell the user which supplier they should select, including the best price rounded to two decimal places:
Supplier #X had the best price at $Y.YY.
Output Samples
Below are some sample outputs of running the program. Note that these samples are NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above. In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: When you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity’s sake.)
Sample Run 1
What is the price for supplier #1?
500.49
Is there another supplier to consider?
N
Supplier #1 had the best price at $500.49.
Sample Run 2
What is the price for supplier #1?
250.39
Is there another supplier to consider?
Y
What is the price for supplier #2?
500.49
Is there another supplier to consider?
Y
What is the price for supplier #3?
178.72
Is there another supplier to consider?
Y
What is the price for supplier #4?
300.00
Is there another supplier to consider?
N
Supplier #3 had the best price at $178.72.
*)In every execution of the while loop we asking user to add a supplier deal. If the deal value is better than the previous deal , we are updating the best deal.
Better deal means the price is lesser than the previous deal's prices.
This way the total space complexity in overall execution of the program is reduced to O(1) to find the best deal.
#include <stdio.h>
/*
id -> will keep track of the current supplier id
supplierId -> is keeping track of supplier id with best
default
flag -> It is used to indicate that no further supplier is
entered
*/
int main() {
int flag=1;
float price,minprice;
int id=0,supplierId;
char response='Y';
while(flag){
id++;
printf("What is the price for supplier
#%d?\n",id);
scanf("%f",&price);
if(id==1)
minprice=price;
if(price<=minprice){
minprice=price;
supplierId=id;
}
printf("Is there another supplier to
consider?\n");
scanf(" %c", &response);
if(response=='Y')
flag=1;
else
flag=0;
}
printf("Supplier #%d had the best price at
$%.2f",supplierId,minprice);
return 0;
}
If you have any doubt regarding the code or the logic,or you want some modifications , just put a comment on my answer, i would be happy to help.