Develop a C# console application that implements two parallel arrays in Main, on
ID: 3857929 • Letter: D
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, next 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
//C# code
using System;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//one to hold double values called
double[] item_price = new double[] {15.50, 50.99, 25.00, 115.49, 75.25};
//You can do the arrays this way its just longer:
//double[] item_price = new double[5];
//item_price[0] = 15.50;
//and all the others
//other to hold strings called
string[] item_name = new string[] {"Widget", "Thingy", "Ratchet", "Clanger", "Fracker"};
printit(item_price, item_name);
//The inputs for the price point and multiplier are input from the console:
Console.WriteLine("Enter the price cutoff point (eg $15.00)");
double pricePoint = double.Parse(Console.ReadLine());
Console.WriteLine("Enter the percentage price change (eg 0.25) ");
double multiplier = double.Parse(Console.ReadLine());
Console.WriteLine();//make a space so that the formatting looks nice
//Note that arrays (and all other *objects*) are ALWAYS passed by reference, so you don't have to explicitly use the 'ref' keyword
//When the changePrices method is called from Main you should pass the item_price array by reference
changePrices(item_price, pricePoint, multiplier);
printit(item_price, item_name);
Console.WriteLine();//make a space so that the formatting looks nice
Console.WriteLine("Press any key to continue . . .");
Console.ReadLine();
}
static void changePrices(double[] item_price, double pricePoint, double priceMuliplier)
{
//loop through the item price array
for (int i = 0; i < item_price.Length; i++)
{
//check the price point to determine the increase in price for each price array element.
if (item_price[i] < pricePoint) //if the current price is less than the price point
//set the price equal to the current price plus the current price times the price multiplier:
item_price[i] = item_price[i] + item_price[i] * priceMuliplier;
}
}
static void printit(double[] item_prices, string[] item_names)
{
for (int i = 0; i < item_prices.Length; i++)
{
Console.WriteLine(String.Format("The price for item {0} is ${1:0.00}",item_names[i], item_prices[i]));
}
}
//call a static method to calculate the total for the minutes and pass to it the two arrays, and the variables found, area and minutes all by reference
static void calculateCost(string[] areaCodes, double[] perMinuteRates, ref Boolean foundFlag, ref string selectedCode, ref string totalMinutes)
{
//Prompt the user to enter an area code.
Console.WriteLine("What area code are you calling?");
selectedCode = Console.ReadLine();
//Loop through the area array to find a match to the entered area code.
for (int i = 0; i < areaCodes.Length; i++)
{
//When the code is found in the array:
if (selectedCode == areaCodes[i])
{
//ask the user for the length of time for a call in minutes:
Console.WriteLine("How many minutes is your call");
totalMinutes = Console.ReadLine();
foundFlag = true; //set the flag to true so we know that something was found and won't tell the user that it was an invalid input
Console.WriteLine("Your phone call to area {0} costs ${1} per minute", selectedCode, perMinuteRates[i]);
Console.WriteLine("For {0} minutes the total is ${1}", totalMinutes, perMinuteRates[i] * double.Parse(totalMinutes));
break; //we found a match so leave the loop
}
}
//If we go throught the entire loop and don't find a match we'll end up here:
if(foundFlag != true)
Console.WriteLine("Sorry - no calls allowed to area {0}", selectedCode);
Console.WriteLine("Press any key to continue . . .");
Console.ReadLine(); //This readline is so that the console doesn't instantly close
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.