Home > Mobile >  Have to square each number in the array and then sum the results together. What do I do wrong?
Have to square each number in the array and then sum the results together. What do I do wrong?

Time:07-30

public static int SquareSum(int[] numbers)
{ 
    int sum = 0;
    for (int i = 0; i < numbers.Length; i  ) {
        sum  = Math.Sqrt(numbers[i]);
    }
        
    return sum;
}

I get a message

The name 'Math' does not exist in the current context

CodePudding user response:

You need to add

using System;

At the beginning of your class.

Doc https://docs.microsoft.com/en-us/dotnet/api/system.math?view=net-6.0

CodePudding user response:

use using System; :

using System;

or press Ctrl . when you are on Math then hit Enter.

CodePudding user response:

Since Math is the part of "System" namespace, you can either add the following line in your file:

using System;

OR rewrite the line as:

//sum  = Math.Sqrt(numbers[i]);
sum  = System.Math.Sqrt(numbers[i]);

Further, as the Math.Sqrt() return type is double, you need to change SquareSum() and the variable sum types to double or use casting alternatively.

  •  Tags:  
  • c#
  • Related