Question

In: Computer Science

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

  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

*)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.


Related Solutions

introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
Using Python 3 The primary objective of this assignment is to reinforce the concepts of string...
Using Python 3 The primary objective of this assignment is to reinforce the concepts of string processing. Part A: Even or Odd For this first part, write a function that will generate a random number within some range (say, 1 to 50), then prompt the user to answer if the number is even or odd. The user will then input a response and a message will be printed indicated whether it was correct or not. This process should be repeated...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the...
Programming II: C++ - Programming Assignment Fraction Object with Operator Overloads Overview In this assignment, the student will write a C++ program that implements a “fraction” object. When writing the object, the student will demonstrate mastery of implementing overloaded operators in a meaningful way for the object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Mathematical Modeling - Fractions · Operator Overloading – Binary Operators (Internal Overload) · Operator Overloading – Binary Operator (External...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered...
COP 2800, Java Programming Assignment 6-1 Using Java create a program using the “Methods” we covered this week. come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. Your program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in...
Introduction Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers. Classes in C++ and Java can represent anything in the real world. This assignment is to write a compiler for Z++ programming language. The Z++ Programming Language Your program will test if an expression entered by the application user...
This is for Java programming. Please use ( else if,) when writing the program) Write a...
This is for Java programming. Please use ( else if,) when writing the program) Write a java program to calculate the circumference for the triangle and the square shapes: The circumference for: Triangle = Side1 + Side2 +Sid3 Square = 4 X Side When the program executes, the user is to be presented with 2 choices. Choice 1 to calculate the circumference for the triangle Choice 2 to calculate the circumference for the square Based on the user's selection the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT