I need help with the code on this one. I keep getting value errors with my code
ID: 3544863 • Letter: I
Question
I need help with the code on this one. I keep getting value errors with my code as it will not compile. I will award points to the best answer who comes up with the solution without any errors. I am developing this code within MS Visual C# 2010. I am getting errors on lines 24 and 27 but need help with the overall code as well because it is not clean.
Here is my question:
Develop a C# console application that implements two parallel arrays in Main, one to hold double values called item_price and the other to hold strings called item_name. Each array will hold 5 values. Use an initializer to fill the item_price array with the values 15.50, 50.99, 25.00, 115.49, 75.25. Use an initializer to fill the item_name array with the values "Widget", "Thingy", "Ratchet", "Clanger", "Fracker". This application will use a method to change the corresponding prices for items based on a price point and a multiplier which will require double value types. The inputs for the price and multiplier are input from the console.
Create two static methods, one called changePrices and one called printit. When the changePrices method is called from Main you should pass the item_price array by reference, the price point and price difference values input from the console to it. The changePrices method should loop through the item price array and check the price point to determine the increase in price for each price array element. The basic computation is:
If the current price is less than the price point then set the price equal to the current price plus the current price times the price multiplier
In the printit method print the item name and the corresponding item price to the console as shown in the output example below.
The price for item Widget is $15.50
The price for item Thingy is $50.99
The price for item Ratchet is $25.00
The price for item Clanger is $115.49
The price for item Fracker is $75.25
Enter the price cutoff point (eg $15.00) $60.00
Enter the percentage price change (eg 0.25) 0.15
The price for item Widget is $17.83
The price for item Thingy is $58.64
The price for item Ratchet is $28.75
The price for item Clanger is $115.49
The price for item Fracker is $75.25
Press any key to continue . . .
As shown in the sample output, your program will first use the printit method to print the parallel arrays as they are and then they will take inputs for the price point and the price change multiplier and then after the changePrices method has been executed run the printit method once again to show any changed prices.
Explanation / Answer
Here is your complete C# program, Please let me know if you need help and rate. Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayStatic
{
class Program
{
static void Main(string[] args)
{
double[] item_price = new double[5] { 15.50, 50.99, 25.00, 115.49, 75.25 };
string[] item_name = new string[5] { "Widget", "Thingy", "Ratchet", "Clanger", "Fracker" };
double pricePoint, multiplier;
// Show the initial prices
printit(item_price, item_name);
System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowCurrencySymbol;
// Input the pricePoint
Console.Write("Enter the price cutoff point (eg $15.00) ");
double.TryParse(Console.ReadLine(), style, System.Globalization.CultureInfo.CurrentCulture , out pricePoint);
// Input the multiplier
Console.Write("Enter the percentage price change (eg 0.25) ");
double.TryParse(Console.ReadLine(), out multiplier);
changePrices(ref item_price, pricePoint, multiplier);
printit(item_price, item_name);
// Let the program wait
Console.Write("Press enter to continue...");
Console.ReadLine();
}
static void changePrices(ref double[] item_price, double pricePoint, double multiplier)
{
for(int i = 0; i < item_price.Length; i++)
{
if (item_price[i] < pricePoint)
{
item_price[i] *= (1 + multiplier);
}
}
}
static void printit(double[] item_price, string[] item_name)
{
for (int i = 0; i < item_price.Length; i++)
{
Console.WriteLine("The price for item {0} is {1:c}", item_name[i], item_price[i]);
}
}
}
}