2

I'm new to coding here.
I am trying to use coding to show statistic for Average, Maximum and Minimum.
I have the error there they say I cannot convert decimal[] to int[]. I have commented the error below beside my code. It is one of the last few lines.
I'm required to use Arrays for this project and the values must be in decimal.

Thank you and sorry if I am doing something wrong, I am new.

namespace Group_Project_Final
{
    public partial class LoanRates : Form
    {
        public LoanRates()
        {
            InitializeComponent();
        }

        private double Average (int [] iArray)
        {
            int total = 0;
            double average;

            for(int index=0;index<iArray.Length;index++)
            {
                total += iArray[index];
            }
            average = (double)total / iArray.Length;
            return average;
        }
        private decimal Highest (int [] iArray)
        {
            int highest = iArray[0];

            for (int index =1; index<iArray.Length;index++)
            {
                if(iArray[index]>highest)
                {
                    highest = iArray[index];
                }               
            }
            return highest;
        }
        private decimal Lowest(int [] iArray)
        {
            int lowest = iArray[0];

            for (int index =1; index<iArray.Length; index++)
            {
                if(iArray[index]<lowest)
                {
                    lowest = iArray[index];
                }
            }
            return lowest;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Home form1 = new Home();
            form1.Show();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            decimal [] rates = { 1.60m, 1.65m ,1.62m, 1.55m, 1.68m, 1.58m };

            int index = 0;
            decimal highestRate; 
            decimal lowestRate;
            double averageRate;

            while(index<rates.Length)
            {
                decimal.ToInt32(rates[index]);
                index++;
            }

            highestRate = Highest(rates); //i am having the error here where they say "cannot simply convert decimal[] to int[] 
            lowestRate = Lowest(rates);
            averageRate = Average(rates); 

            txtAvg.Text = averageRate.ToString();
            txtHighest.Text = highestRate.ToString();
            txtLowest.Text = lowestRate.ToString();
        }
    }
}
5
  • 1
    Exactly as the error states: Highest needs an int[] but rates is a decimal[]. In addition, unless this is for learning purposes then use linq's Min,Max and Average methods. Commented Aug 5, 2018 at 5:52
  • Also, see how decimal.ToInt32() actually works. Commented Aug 5, 2018 at 6:14
  • I would update the definition of Average(int [] iArray) to Average(decimal [] dArray). All your logic is fine, the only issue is that you are trying to convert between decimal[] and integer[]. Commented Aug 5, 2018 at 7:25
  • As an aside, please don't prefix names with their type (iArray). That's commonly called Hungarian Notation, and is widely considered a bad practice. In C# you explicitly have to declare the type of the parameter (int[]), so including the type as a prefix of the name is redundant. It also means you have to rename variables if you change their type (e.g. in my previous comment), and it is generally a pain to work with. Commented Aug 5, 2018 at 7:29
  • Why does your Average method return a double, but the others return a decimal? I'd make them all return decimal for consistency and because your source values are decimals. Also note that if you did successfully convert the values to Int32 that you lose the fractional part of each number.. Is that what you intend? All your method would return 1.0 in that case because ToInt32 simply truncates the decimal. Commented Aug 5, 2018 at 9:51

3 Answers 3

4

Just putting it out there,

You could reduce 90% of your code by just using the BCL methods of linq.

It solves your casting and type problems instantly

using System.Linq;

...

private void button7_Click(object sender, EventArgs e)
{
   decimal[] rates = { 1.60m, 1.65m, 1.62m, 1.55m, 1.68m, 1.58m };
   txtAvg.Text = rates.Max().ToString();
   txtHighest.Text = rates.Min().ToString();
   txtLowest.Text = rates.Average().ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you need decimal Array, change your function according to decimal.

private double Average(decimal[] iArray)
{
  decimal total = 0;
  double average;

  for (int index = 0; index < iArray.Length; index++)
  {
    total += iArray[index];
  }
  average = (double)total / iArray.Length;
  return average;
}
private decimal Highest(decimal[] iArray)
{
  decimal highest = iArray[0];

  for (int index = 1; index < iArray.Length; index++)
  {
    if (iArray[index] > highest)
    {
      highest = iArray[index];
    }
  }
  return highest;
}
private decimal Lowest(decimal[] iArray)
{
  decimal lowest = iArray[0];

  for (int index = 1; index < iArray.Length; index++)
  {
    if (iArray[index] < lowest)
    {
      lowest = iArray[index];
    }
  }
  return lowest;
}

Comments

2

Right now your functions Lowest and Highest take in int arrays and not decimal arrays as you are giving it. Right now your line decimal.ToInt32(rates[index]); does nothing as it is just solves for the equivalent int value of that index but doesn't assign it anywhere and thus just gets lost. You'd need to either create a new integer array and add each value to it through your loop instead, something like this:

int[] integerRates = new int[6];
while(index<rates.Length)
        {
            integerRates[index] = decimal.ToInt32(rates[index]);
            index++;
        }
highestRate = Highest(integerRates); 
lowestRate = Lowest(integerRates);
averageRate = Average(integerRates); 

Or change the input parameter of your other functions to accept decimal arrays instead (as per Masoomian's answer), which would also deem your while loop redundant. This is likely the better way of handling it as you won't lose accuracy on your decimals.

The best solution would be to use Linq as TheGeneral suggested. Although I suspect this is for a school project where they may want to teach loops and if statements.

2 Comments

I'd think the whole decimal.ToInt32 part, even if assigned somewhere, would be unwanted anyways. The fractional portion of each number would be lost and the OP clearly has non-integral (ex. 1.65) values in their source
@pinkfloydx33 You're right, dropping it is certainly better. I just wanted to show how that function worked and include a solution which basically solved it in the way she envisioned it (just w.o the bug).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.