8
using System;

public class Newton_Ralphson
{
    public double compute(double x,int n,double[] P)            // x, degree, coefficients 
    {
        double pol = 0;
        for (int i = 0;i < n;i++)
        {
            for (int j = 1; j < n - i; j++)
                x *= x;
                pol += P[n - i] * x;
        }
        pol += P[0];
        return pol;
    }

public double[] secondary_Pol(int n, double[] P)            //slope
{
    double[] secP = new double[n - 1];
    for (int i = 0; i < n - 1; i++) 
        secP[i] = P[i + 1];
    return secP;
}

public double Main()
{
    Console.WriteLine("Estimate the solution for a nth degree polynomial!!! ");
    int n = 0;
    Console.WriteLine("Enter the degree of the polynomials! ");
    n = Int32.Parse(Console.ReadLine());
    double[] coefficients = new double[n+1];

    for(int i = 0;i < n+1;i++)
    {
        Console.WriteLine("Enter the coefficients ");
        Console.Write("coefficients of x to the power of {0}: ", n - i);         //decending coefficients 
        coefficients[n-i] = Convert.ToDouble(Console.ReadLine());
    }


    Console.WriteLine("Initial value is: ");
    double xint = Convert.ToDouble(Console.ReadLine());

    Console.WriteLine("How many times does the function go through? ");
    int precision = Convert.ToInt32(Console.ReadLine());
    double polyValue = 0;

    for (int j = 0; j < precision; j++)
    {
        polyValue = compute(xint, n, coefficients);                              //y0 in y = slope(x-x0) + y0
        double slope = compute(xint, n - 1, secondary_Pol(n, coefficients));
        xint = (-polyValue / slope) + xint;
    }
    Console.WriteLine(xint);
    Console.ReadLine();
    return 0;
}

}

When I add 'static' into the line: public static double Main() there is another error showed up

error CS0120: An object reference is required for the non-static field, method, or property 'Newton_Ralphson.compute(double, int, double[])'

error occurred when the two other function executed

user2864740
  • 60,010
  • 15
  • 145
  • 220
Hung Nguyen
  • 301
  • 1
  • 2
  • 11
  • 1
    I think, `Main` must be `void`. Also, check the properties of your project and uncheck `enable application framework` – T.S. Feb 23 '14 at 04:14
  • 2
    @T.S. - No, `Main` can return `int` or `void`. See this [MSDN](http://msdn.microsoft.com/en-us/library/ms228506%28v=vs.90%29.aspx) link, for example. – Tim Feb 23 '14 at 04:20
  • 1) *add* `static` to Main 2) fix *those* errors (search on SO for what causes them and how to fix them) 3) go back and fix the next error about Main - always, try to take a step forward. You'll eventually get to the end. – user2864740 Feb 23 '14 at 04:36

3 Answers3

5

Please see C# entry point function

C# application, Main() must be the entry point.

The reason is because that's what the designers of the language decided to be what to look for as an entry point for your program. They could just as well have used a totally different approach to find the entry point, e.g. using meta data, or instantiating an object for you (Which would require a parameterless constructor). Another reason for naming it void main() is that it's intuitive for users coming from other languages.

Use:

public class Newton_Ralphson
{
    public static double compute(double x, int n, double[] P) // x, degree, coefficients 
    {
        double pol = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 1; j < n - i; j++)
                x *= x;
            pol += P[n - i]*x;
        }
        pol += P[0];
        return pol;
    }

    public static double[] secondary_Pol(int n, double[] P) //slope
    {
        double[] secP = new double[n - 1];
        for (int i = 0; i < n - 1; i++)
            secP[i] = P[i + 1];
        return secP;
    }

    public static void Main()
    {
        Console.WriteLine("Estimate the solution for a nth degree polynomial!!! ");
        int n = 0;
        Console.WriteLine("Enter the degree of the polynomials! ");
        n = Int32.Parse(Console.ReadLine());
        double[] coefficients = new double[n + 1];

        for (int i = 0; i < n + 1; i++)
        {
            Console.WriteLine("Enter the coefficients ");
            Console.Write("coefficients of x to the power of {0}: ", n - i); //decending coefficients 
            coefficients[n - i] = Convert.ToDouble(Console.ReadLine());
        }


        Console.WriteLine("Initial value is: ");
        double xint = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("How many times does the function go through? ");
        int precision = Convert.ToInt32(Console.ReadLine());
        double polyValue = 0;

        for (int j = 0; j < precision; j++)
        {
            polyValue = compute(xint, n, coefficients); //y0 in y = slope(x-x0) + y0
            double slope = compute(xint, n - 1, secondary_Pol(n, coefficients));
            xint = (-polyValue/slope) + xint;
        }
        Console.WriteLine(xint);
        Console.ReadLine();

    }
}

1-Change public double Main() to public static void Main()

2-change public double[] secondary_Pol(int n, double[] P) to public static double[] secondary_Pol(int n, double[] P)

3-Change public double compute(double x,int n,double[] P) to public static double compute(double x, int n, double[] P)

Community
  • 1
  • 1
  • +1 for a correct answer and extra-mile points about making the `secondary_Pol` and `compute` methods `static` - i.e. mitigating the next problem that will predictably arise! :) – J0e3gan Feb 23 '14 at 04:36
3

Add static to all methods. You cannot call a non static method from the static method, without providing object reference. As an alternative you can create an instance of a class and invoke the method:

var c = new Newton_Ralphson();
c.compute();
Mikl X
  • 1,199
  • 11
  • 17
  • +1 for mitigating the next problem that would arise after fixing `Main`'s declaration - i.e. noting that the other methods need to be declared `static` too. Still, returning `double` from `Main` is a problem. Basically, if you and I combined answers, we would arive at @ShahroozJefri's. :} – J0e3gan Feb 23 '14 at 04:43
2

Creating a new C# console app in Visual Studio will show you what you generally need:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) // static with a void (or int) return type
        {
        }
    }
}

Main must be declared static.

double Main is definitely a problem. See an older MSDN article on what a C# main method needs - specifically a void or int return type.

Main's string[] param for args is optional - i.e. can be omitted. See the aforementioned MSDN article for confirmation.

Finally, adding public to Main's declaration is fine but unnecessary and typically skipped.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80