Question

In: Computer Science

Objectives To reinforce the use of If-Else statements To learn how to use while loops Introduction:...

Objectives

  1. To reinforce the use of If-Else statements
  2. To learn how to use while loops

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

  1. The user will use ‘Y’ to indicate there is at least one more supplier to consider.
  2. The user will use ‘N’ to indicate that there are no more suppliers to consider.
  3. Each suppliers’ price will be a positive real number.

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.

Solutions

Expert Solution

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:


Related Solutions

Assignment #3 Introduction to C Programming – COP 3223 Objectives To reinforce the use of If-Else...
Assignment #3 Introduction to C Programming – COP 3223 Objectives To reinforce the use of If-Else statements To learn how to use while loops 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...
You were introduced to control structures - such as IF-ELSE statement, WHILE and FOR loops. Describe...
You were introduced to control structures - such as IF-ELSE statement, WHILE and FOR loops. Describe what they do and why are control structures important to programming?
How do I change my if statements into while and for loops, so that I can...
How do I change my if statements into while and for loops, so that I can make my bot go straight and turn exactly 12 times. /*********************************************************************** * Exp7_1_RotaryEncoder -- RedBot Experiment 7_1 * * Knowing where your robot is can be very important. The RedBot supports * the use of an encoder to track the number of revolutions each wheels has * made, so you can tell not only how far each wheel has traveled but how * fast...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
Python Programming This assignment will give you practice with interactive programs, if/else statements, collections, loops and...
Python Programming This assignment will give you practice with interactive programs, if/else statements, collections, loops and functions. Problem Description A small car yard dealing in second hand cars needs an application to keep records of cars in stock. Details of each car shall include registration(rego), model, color, price paid for the car (i.e. purchase price) and selling price. Selling price is calculated as purchased price plus mark-up of 30%. For example, Toyota Corolla bought for $20,000 will have the selling...
This assignment will acquaint you with the use of while loops and boolean expressions. You will...
This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
In the given instruction, I am to use while loops only. The goal is to prompt...
In the given instruction, I am to use while loops only. The goal is to prompt the user to select an acceptable input. If the wrong input is given, the program forces the user to select an appropriate input. The program also keeps running until the user chooses to exist by selecting a very specific input which in my case is the upper or lower case "E". The problem is even after selecting upper or lower case "E", my program...
use python Although clumsy, the if statements and if-else statements can be used to achieve the...
use python Although clumsy, the if statements and if-else statements can be used to achieve the same effects as if-elif-else statements. Rewrite the speed3 function: def speed3():    kph=float(input('What is the speed in kph?'))    mph=0.621371*kph print('The speed is',mph,'mph.') if mph>80:    print('You are WAY over the speed limit. Your fine is $200!') elif 65<mph<=80: print('You are over the speed limit (65mph) . Slow down1'). elif 30<=mph<=65: print('You are within the speed limit. Good job!') else:   print('You are too slow....
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I need it in C++. Given an input string, set result to a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceded or followed by a letter -- so for example the "is" in "this" does not count.   • for input of "is test" → "is not test" • for input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT