In: Computer Science
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.
Below is the code for the above problem:
marssupplier.c
#include <stdio.h>
#define maxSupplier 100
int main()
{
double supplier[maxSupplier]; //array to store price of each
supplier
int i = 0; //i represent number of supplier
int condition = 1; //to break the loop
while(condition)
{
printf("what is the price for supplier #%d\n",i+1);
scanf("%lf",&supplier[i]); //storing price in the array
i++; //increase number of suppllier
printf("Is there another supplier to consider\n");
char choice;
scanf("\n%c",&choice);
if(choice == 'n' || choice =='N') //if user enter n, break the
loop
{
condition = 0;
}
}
int min = 0; //index number of best supplier
for(int j = 1; j<i; j++) //traverser all the suppllier
{
if(supplier[j]<supplier[min]) //if any suppllier has lower price
than current lower price
{
min = j;
}
}
printf("Supllier #%d had the best price at
$%.2lf\n",min+1,supplier[min]);
return 0;
}
OUTPUT: