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...
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...
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...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Introduction to java: Gambling Objectives: •Performing decisions using if and switch statements. •Using relational and logical...
Introduction to java: Gambling Objectives: •Performing decisions using if and switch statements. •Using relational and logical operators •Working with random numbers Write an application that simulates a gambling slot machine. In the application, perform the following tasks: 1. Prompt the player to enter the amount of money that the player is willing to gamble. 2. Create three random numbers in range from one to four to represent three of the following four objects (Apple, Orange, Cherry and Pineapple). 3. Compare...
In MATLAB, the script should use both for and while loops. Game Mode: Randomly generates 10...
In MATLAB, the script should use both for and while loops. Game Mode: Randomly generates 10 unique (e.g. non-repeating) multiplication problems that involve multiplying the integer selected in the previous step by an integer value between 2 and 12, inclusive, after which the following report should be displayed in the Command Window. You correctly answered __ out of __ problems (__%) involving __s. The number of correctly answered problems appears in the first blank, the total number of problems appears...
± Introduction to Solubility and the Solubility Product Constant Learning Goal: To learn how to calculate...
± Introduction to Solubility and the Solubility Product Constant Learning Goal: To learn how to calculate the solubility from Kspand vice versa. Consider the following equilibrium between a solid salt and its dissolved form (ions) in a saturated solution: CaF2(s)⇌Ca2+(aq)+2F−(aq) At equilibrium, the ion concentrations remain constant because the rate of dissolution of solid CaF2 equals the rate of the ion crystallization. The equilibrium constant for the dissolution reaction is Ksp=[Ca2+][F−]2 Ksp is called the solubility product and can be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT