1

I'm studying for finals so I've been having problems with this assignment and I'm just not understanding what I'm doing wrong, I was hoping some user here could help me out. I have to write an array that will computer average using a .Length property to find out how many elements are in my array, with a for(;;) loop. This is my code so far, and it works without the numbers I need to use.

static double ComputeSum(int []  intArray)
{
    int intsum = 0;
    int intCounter;

    for (intCounter=0; intCounter<intArray.Length; intCounter++)
    {
        intsum += intArray[intCounter];
    }
    return intsum;
}

static double ComputeAverage(int [] intArray)
{
    return (double)ComputeSum(intArray) / intArray.Length;
}

the numbers I have to use though are all doubles, and I'm not sure how to fix this.

here are the numbers

int[] numbers = new int[9.7, 2.2, 4.3, 1.7, 5.6, 3.6, 4.5, 1.3, 4.6, 3.0];

if this is too vague I can answer questions, and I appreciate any help I can get.

5
  • 4
    Use double instead of int Commented Dec 9, 2015 at 3:02
  • an integer array with double values; Commented Dec 9, 2015 at 3:02
  • 2
    What is the question? Why do you think this is wrong? Does it compile (hint, it doesn't, can you spot why?)? Commented Dec 15, 2015 at 11:12
  • 2
    You shouldn't change the question after learning what was wrong. The question and its answer now makes no sense, if the answer is to use double, the question makes no sense since you're apparently using double. But I see now that you changed the question. The way it stands now has little to no value for future visitors. Voting to close. Commented Dec 15, 2015 at 11:15
  • My apologies, won't happen again. Commented Dec 15, 2015 at 11:19

3 Answers 3

3

Use double in your code:

static void Main(string[] args)
{
    double[] numbers = new[] { 9.7, 2.2, 4.3, 1.7, 5.6, 3.6, 4.5, 1.3, 4.6, 3.0 };
    double average = ComputeAverage(numbers);
}

static double ComputeAverage(double[] array)
{
    return ComputeSum(array) / array.Length;
}

static double ComputeSum(double[] array)
{
    double sum = 0;
    int intCounter;

    for (intCounter = 0; intCounter < array.Length; intCounter++)
    {
        sum += array[intCounter];
    }
    return sum;
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Nicholas is correct, both denominator must be in double data type social.msdn.microsoft.com/forums/vstudio/en-US/…
Thanks a bunch for all the help! I understand perfectly what I did wrong, also need to change to double[] moreNumbers = new double[] { 9.7, 2.2, 4.3, 1.7, 5.6, 3.6, 4.5, 1.3, 4.6, 3.0 };
You can use an expression body for your ComputeAverage method as well. static double ComputeAverage(double[] array) => ComputeSum(array) / array.Length;
Because this is incorrect? Not only does he not have to change a single data type in his question, you've copied the one thing that was syntactically wrong, the declaration and initialization of numbers. If one of the operands in a division is a double, a floating point division will be done even if the other operand is an int. In other words, fix the declaration of numbers and call ComputeAverage with it and it'll return the average. (and no, it was not me that downvoted, merely offering a plausible explanation)
Ah, he changed the question, voting to close then.
|
0

You can simply do the same with the following method:

static double ComputeAverage(double[] doubleArray)
{
    return (double)doubleArray.Sum() / doubleArray.Length;
}

and the input will be :

double[] numbers = new double[9.7, 2.2, 4.3, 1.7, 5.6, 3.6, 4.5, 1.3, 4.6, 3.0];

Comments

0
static double ComputeAverage(double[] doubleArray)
{
    for (;;) return doubleArray.Sum() / doubleArray.Length;
}
  • using a .Length property ✓
  • with a for(;;) loop ✓
  • have fun ✓

Comments

Your Answer

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