• 0

C#, Parallel Arrays- Passing Array by Reference


Question

I am working on an Assignment that asks me to do this:

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.

 

I keep getting an error stating, "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exist (are you missing a cast?) on
Line 32  Console.WriteLine("The price for item " + (item_name[x]) + "is " + item_price[x]);

as well  as several more errors. How can I fix this?

Here is my code:

using System;

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


namespace Assignment_9
{
    class Program
    {
        double ppoint = 0;
        double multi = 0;
        
        static void Main(string[] args)
        {
            //two arrays
            double[] item_price = { 15.50, 50.99, 25.00, 115.49, 75.25 };
            string[] item_name = { "Widget", "Thingy", "Ratchet", "Clanger", "Fracker" };


            Console.WriteLine();
            printit(item_name, item_price);
            Console.WriteLine();
            changePrices(item_price);
            Console.WriteLine();
            printit(item_name, item_price);
        }


       static void printit(string[] item_name, double[] item_price)
        {
            for (double x = 0; x < item_price.Length; x++)
            {
                Console.WriteLine("The price for item " + (item_name[x]) + "is " + item_price[x]);
            }
        }


        static void changePrices(double[] passed)
        {
            Console.WriteLine("Enter the price cutoff point (eg $15.00) $");
            ppoint = Convert.ToDouble(Console.ReadLine());


            Console.WriteLine("Enter the percentage price change (eg 0.25)");
            multi = Convert.ToDouble(Console.ReadLine());


            for (double x = 0; x < item_price.Length; x++)
            {
                if (x < ppoint)
                {
                    Console.WriteLine(x = (x + (x * multi)));
                }
            }
        }


    }
}

19 answers to this question

Recommended Posts

  • 0
  On 11/05/2015 at 03:03, thehappygoldenlady said:

 

I am working on an Assignment that asks me to do this:

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.

 

I keep getting an error stating, "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exist (are you missing a cast?) on

Line 32  Console.WriteLine("The price for item " + (item_name[x]) + "is " + item_price[x]);

as well  as several more errors. How can I fix this?

Here is my code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Assignment_9
{
    class Program
    {
        double ppoint = 0;
        double multi = 0;
        
        static void Main(string[] args)
        {
            //two arrays
            double[] item_price = { 15.50, 50.99, 25.00, 115.49, 75.25 };
            string[] item_name = { "Widget", "Thingy", "Ratchet", "Clanger", "Fracker" };
 
            Console.WriteLine();
            printit(item_name, item_price);
            Console.WriteLine();
            changePrices(item_price);
            Console.WriteLine();
            printit(item_name, item_price);
        }
 
       static void printit(string[] item_name, double[] item_price)
        {
            for (double x = 0; x < item_price.Length; x++)
            {
                Console.WriteLine("The price for item " + (item_name[x]) + "is " + item_price[x]);
            }
        }
 
        static void changePrices(double[] passed)
        {
            Console.WriteLine("Enter the price cutoff point (eg $15.00) $");
            ppoint = Convert.ToDouble(Console.ReadLine());
 
            Console.WriteLine("Enter the percentage price change (eg 0.25)");
            multi = Convert.ToDouble(Console.ReadLine());
 
            for (double x = 0; x < item_price.Length; x++)
            {
                if (x < ppoint)
                {
                    Console.WriteLine(x = (x + (x * multi)));
                }
            }
        }
 
    }
}
 

 

i'm half drunk right now...

 

but how does the new values in "static void changePrices(double[] passed)" supposed to get back to the main program ?

  • 0

I didn't read most of that, but the error you're getting is because you're using a double for your for loop. Try:

 

   for (int x = 0; x < item_price.Length; x++)
  • 0
  On 11/05/2015 at 03:43, thehappygoldenlady said:

I thought it was supposed to get called by the second printit method in main. Is it not written correctly?

 

When the changePrices method is called from Main you should pass the item_price array by reference

 

that basically sends it as a pointer.. you're sending it by value right now so you're sending a copy of the data.. when you leave the scope of the function you are losing the new values.

  • 0
  On 11/05/2015 at 03:30, virtorio said:

 

I didn't read most of that, but the error you're getting is because you're using a double for your for loop. Try:

   for (int x = 0; x < item_price.Length; x++)

 

Thanks, I made that change and moved down the variable to the changePrices method and get the same error for this line:

                    Console.WriteLine(x = (x + (x * multi)));
 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Assignment_9_Homma
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //two arrays
            double[] item_price = { 15.50, 50.99, 25.00, 115.49, 75.25 };
            string[] item_name = { "Widget", "Thingy", "Ratchet", "Clanger", "Fracker" };
 
            Console.WriteLine();
            printit(item_name, item_price);
            Console.WriteLine();
            changePrices(item_price);
            Console.WriteLine();
            printit(item_name, item_price);
        }
 
       static void printit(string[] item_name, double[] passed)
        {
            for (int x = 0; x < passed.Length; x++)
            {
                Console.WriteLine("The price for item " + (x + 1) + "is " + passed[x]);
            }
        }
 
        static void changePrices(double[] passed)
        {
            double ppoint = 0;
            double multi = 0;
        
            Console.WriteLine("Enter the price cutoff point (eg $15.00) $");
            ppoint = Convert.ToDouble(Console.ReadLine());
 
            Console.WriteLine("Enter the percentage price change (eg 0.25)");
            multi = Convert.ToDouble(Console.ReadLine());
 
            for (int x = 0; x < passed.Length; x++)
            {
                if (x < ppoint)
                {
                    Console.WriteLine(x = (x + (x * multi)));
                }
            }
        }
 
    }
}
  • 0
  On 11/05/2015 at 03:51, thehappygoldenlady said:

 

Thanks, I made that change and moved down the variable to the changePrices method and get the same error for this line:

                    Console.WriteLine(x = (x + (x * multi)));
 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Assignment_9_Homma
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //two arrays
            double[] item_price = { 15.50, 50.99, 25.00, 115.49, 75.25 };
            string[] item_name = { "Widget", "Thingy", "Ratchet", "Clanger", "Fracker" };
 
            Console.WriteLine();
            printit(item_name, item_price);
            Console.WriteLine();
            changePrices(item_price);
            Console.WriteLine();
            printit(item_name, item_price);
        }
 
       static void printit(string[] item_name, double[] passed)
        {
            for (int x = 0; x < passed.Length; x++)
            {
                Console.WriteLine("The price for item " + (x + 1) + "is " + passed[x]);
            }
        }
 
        static void changePrices(double[] passed)
        {
            double ppoint = 0;
            double multi = 0;
        
            Console.WriteLine("Enter the price cutoff point (eg $15.00) $");
            ppoint = Convert.ToDouble(Console.ReadLine());
 
            Console.WriteLine("Enter the percentage price change (eg 0.25)");
            multi = Convert.ToDouble(Console.ReadLine());
 
            for (int x = 0; x < passed.Length; x++)
            {
                if (x < ppoint)
                {
                    Console.WriteLine(x = (x + (x * multi)));
                }
            }
        }
 
    }
}

 

          Console.WriteLine(x + (x * multi));

 

and you're still not passing by reference(the point of this assignment). you will continually get the old values and never the new changed ones.

 

work harder or reconsider major

  • 0
  On 11/05/2015 at 03:45, seta-san said:

When the changePrices method is called from Main you should pass the item_price array by reference

 

that basically sends it as a pointer.. you're sending it by value right now so you're sending a copy of the data.. when you leave the scope of the function you are losing the new values.

 

So should this:

 

printit(item_name, item_price)

 

Look like this instead:

 

printit(item_name, item_price[x])

  • 0
  On 11/05/2015 at 03:58, thehappygoldenlady said:

So should this:

 

printit(item_name, item_price)

 

Look like this instead:

 

printit(item_name, item_price[x])

  static void changePrices(ref double[] passed)

changePrices(ref item_price);

 

did you read your textbook?

 

https://msdn.microsoft.com/en-us/library/szasx730.aspx

  • 0
  On 11/05/2015 at 04:01, seta-san said:

  static void changePrices(ref double[] passed)

changePrices(ref item_price);

 

did you read your textbook?

 

https://msdn.microsoft.com/en-us/library/szasx730.aspx

 

Yes, I did, but I am honestly drawing a blank on how to make this work right. That's why I came here. I really appreciate any the assistance you are able to provide.

  • 0
  On 11/05/2015 at 04:01, seta-san said:

  static void changePrices(ref double[] passed)

changePrices(ref item_price);

 

did you read your textbook?

 

https://msdn.microsoft.com/en-us/library/szasx730.aspx

Regarding arrays, the array reference is passed by value, but the array contents is not. If you're just changing the contents of the array it is not necessary pass by reference. It's only necessary if you're creating a new array and want to pass it back. 

  • 0
  On 11/05/2015 at 04:43, thehappygoldenlady said:

So how can I make this line properly pass by reference:

 

Console.WriteLine(x = (x + (x * multi)));

 

your not passing ANYTHING in that statement except to the "WriteLine" function.

 

also you're trying to assign a value in that statement. you can't do that.

never mind that last part. apparently you can. i'd never done it before.. i'd still consider it bad form but whatever.

  • 0
  On 11/05/2015 at 04:54, thehappygoldenlady said:

So what should I do instead?

 

You're not using x correctly, x contains the current index of the for loop, but not the value you want to work with. You need to read a value from the array and write it back to the array using x.

       for (int x = 0; x < passed.Length; x++)
            {
                if (passed[x] < ppoint)
                {
                    passed[x] = (passed[x] + (passed[x] * multi)));
                }
  • 0
  On 11/05/2015 at 04:59, virtorio said:

 

You're not using x correctly, x contains the current index of the for loop, but not the value you want to work with. You need to read a value from the array and write it back to the array using x.

       for (int x = 0; x < passed.Length; x++)
            {
                if (passed[x] < ppoint)
                {
                    passed[x] = (passed[x] + (passed[x] * multi)));
                }

 

Thank you, I had tried a form of that but had too much info in each area where you have passed, which made more errors. I appreciate it greatly!!!!!

  • 0
  On 11/05/2015 at 05:03, thehappygoldenlady said:

Thank you, I had tried a form of that but had too much info in each area where you have passed, which made more errors. I appreciate it greatly!!!!!

 

And thank you for the explanation.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Libtards will be so excited! 🤣
    • I agree, if Intel wants to make those 50% margins, they really should stick to sockets longer, someone is more likely to upgrade their CPU when they don't need to purchase a motherboard, then looking at maybe RAM, might as well buy a whole new PC at that point, then before you know they've talked themselves out of the whole thing.
    • not sure why people care about the developers so much. let them do whatever they want. if it succeeds, it can benefit us. if it doesn't, then who cares, your life will go on. i'm glad there are still people who do things without thinking whether the output will be productive to society...
    • this man really knows how to embarass americans! i thought he was platforming on "bringing back respect to america!". really sad to see the grip he has over working class americans. he could (and i might argue already has) spit on the working class and half would kiss his shoe. at least this you gotta give him credit for. who else can do this with applause and cheer.
    • Get this 27-inch ASUS VA279QG 120Hz monitor for dirt-cheap by Taras Buria If you are on a very tight budget but you still want to upgrade your monitor to something more exciting than a standard 60Hz office monitor, ASUS has a perfect deal for you. The VA279QG is currently available for as little as $109, and for this money, you get quite a lot of a monitor. The ASUS VA279QG is a big 27-inch IPS monitor with a classic FullHD resolution and a wide 178-degree viewing angle. It can operate with a refresh rate of 120Hz, which is more than enough for buttery-smooth gaming. And since the monitor is FullHD, you will be able to see high refresh rates in more games since your GPU will have to render fewer pixels at 120Hz. Besides, the monitor supports variable refresh rate (VRR), a feature that can further reduce stutters by dynamically adjusting the refresh rate to your FPS. Other display specs include a 1ms MPRT response time, 16.7 million colors, 99% sRGB coverage, and a typical brightness of 300 nits. It is also covered with an anti-glare coating to reduce reflections. Ports-wise, you get one DisplayPort 1.2, one HDMI 1.4, one VGA, and one headphone jack. There are also two 2W speakers, but set your expectation right—these are unlikely to blow your mind with high-quality audio. Finally, there is a VESA 100 mount and a cutout in the base, which lets you mount your phone, namecard, or other small items for extra convenience. 27-inch ASUS VA279QG 120Hz IPS Gaming Monitor - $109 | 22% off on Amazon US This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      652
    2. 2
      ATLien_0
      269
    3. 3
      +FloatingFatMan
      176
    4. 4
      Michael Scrip
      155
    5. 5
      Steven P.
      136
  • Tell a friend

    Love Neowin? Tell a friend!