Question

In: Computer Science

Code in C# please. Write a program that will use the greedy algorithm. This program will...

Code in C# please.

Write a program that will use the greedy algorithm.

This program will ask a user to enter the cost of an item.

This program will ask the user to enter the amount the user is paying.

This program will return the change after subtracting the item cost by the amount paid.

Using the greedy algorithm, the code should check for the type of bill.

Example:

Cost of item is $15.50 User pays a $20 bill

$20 - $15.50 = $4.50

Greedy algorithm will then return the user change in bills by checking every bill starting from $20

$20 <= $4.50? no skip

$10 <= $4.50? no skip

$5 <= $4.50? no skip

$1 <= $4.50? yes store $1 and subtract from total

$1 <= $3.50? yes store $1 and subtract from total. Etc

Final output should be User will get 4 dollar bills and 2 quarters.

Solutions

Expert Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            float cost,pay;
            int [] Array={0,0,0,0,0};
            float [] A={20,10,5,1,0.25F};
            int size = A.Length;
            Console.WriteLine("Enter cost of an item: ");
            cost = float.Parse(Console.ReadLine());

            Console.WriteLine("\nEnter amount the user is paying: ");
            pay = float.Parse(Console.ReadLine());

            float remain=pay-cost;
          
            for(int i=0;i<size;i++)
            {
                if(remain>=A[i])
                {
                    int temp=(int)(remain/A[i]);
                    remain-=temp*A[i];
                    Array[i]=temp;
                }
            }
            int doller = 0;
            for(int i=0;i<size;i++)
            {
                Console.WriteLine(Array[i] + " of " + A[i]);
                doller += (int)Array[i] * (int)A[i];
              
            }
            Console.WriteLine("\ntotal=" + doller + " doller And " + Array[size - 1] + " quarters");
            Console.ReadKey();
        }
    }
}


//sample output


Related Solutions

CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling...
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT