-1

I know it ain't pretty but i am still rather new at programming. After i input the n1, n2 and n3 and press calculate the form gets an "Program not responding"

it does so both in Debug and Release mode and i have no visible error messages.Anyone who can help me? it is for an assignment due tomorrow, and right now i cannot see a fix, so any help is much appreciated.

If you need a copy of the code to check, please send me a pm.

With kind regards. Mathias

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace I3_REVISI
{
    public partial class Newton : Form
{
    public static double n1, n2, n3, x_NEWTON, Initialization, x1, approx1, approx2, approx3, approx4, approx5, approx6;
    public Newton()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Show();
        this.Hide();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox11_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        textBox6.Text = "";
        textBox10.Text = "";
        textBox13.Text = "";
        textBox3.Text = "";
        Newton1.Text = "";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        n1 = Convert.ToDouble(textBox6.Text);
        n2 = Convert.ToDouble(textBox10.Text);
        n3 = Convert.ToDouble(textBox13.Text);
        // Initialization If value is positive the start value is xinitialization -1 f(0) = xinitialization-1

        for (x_NEWTON = 0; x1 >= 0; x_NEWTON++)
        {
            x1 = n1 * Math.Pow(x_NEWTON, 2) + n2 * x_NEWTON + n3;
            Initialization = x1 - 1;
        }

        // Formula:   x1 = x0 - ( f(x0) / f'(x0) )
            approx1 = Initialization - ((n1*Math.Pow(Initialization,2)+n2*Initialization+n3) / (n1*2*Initialization-n2));
        approx2 = approx1 - ((n1 * Math.Pow(approx1, 2) + n2 * approx1 + n3) / (n1 * 2 * approx1 - n2));
        approx3 = approx2 - ((n1 * Math.Pow(approx2, 2) + n2 * approx2 + n3) / (n1 * 2 * approx2 - n2));
        approx4 = approx3 - ((n1 * Math.Pow(approx3, 2) + n2 * approx3 + n3) / (n1 * 2 * approx3 - n2));
        approx5 = approx4 - ((n1 * Math.Pow(approx4, 2) + n2 * approx4 + n3) / (n1 * 2 * approx4 - n2));
        approx6 = approx5 - ((n1 * Math.Pow(approx5, 2) + n2 * approx5 + n3) / (n1 * 2 * approx5 - n2));

        Newton1.Text = ("" + approx6);

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox10_TextChanged(object sender, EventArgs e)
    {

    }
}
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86

1 Answers1

0
  1. check your input value datatype and prevent input string or value to cause crash your formula like division by zero
  2. change your for loop index from value to less tan a value

    for (int i =0 ; i<10)

  3. and keep your code in try catch block and pass exception parameter to see your error.

    try
    {
        n1 = Convert.ToDouble(textBox6.Text);
        n2 = Convert.ToDouble(textBox10.Text);
        n3 = Convert.ToDouble(textBox13.Text);
        f(0) = xinitialization-1;
    
        for (x_NEWTON = 0; x1 >= 0; x_NEWTON++)
        {
            x1 = n1 * Math.Pow(x_NEWTON, 2) + n2 * x_NEWTON + n3;
            Initialization = x1 - 1;
        }
    
            approx1 = Initialization - ((n1*Math.Pow(Initialization,2)+n2*Initialization+n3) / (n1*2*Initialization-n2));
        approx2 = approx1 - ((n1 * Math.Pow(approx1, 2) + n2 * approx1 + n3) / (n1 * 2 * approx1 - n2));
        approx3 = approx2 - ((n1 * Math.Pow(approx2, 2) + n2 * approx2 + n3) / (n1 * 2 * approx2 - n2));
        approx4 = approx3 - ((n1 * Math.Pow(approx3, 2) + n2 * approx3 + n3) / (n1 * 2 * approx3 - n2));
        approx5 = approx4 - ((n1 * Math.Pow(approx4, 2) + n2 * approx4 + n3) / (n1 * 2 * approx4 - n2));
        approx6 = approx5 - ((n1 * Math.Pow(approx5, 2) + n2 * approx5 + n3) / (n1 * 2 * approx5 - n2));
    
        Newton1.Text = ("" + approx6);
    }
    catch (exception ex)
    {
        messagebox.show(ex.message);
    }
    
Markus Deibel
  • 1,261
  • 20
  • 26
  • 1. Checking user input is good (even though you are not checking it in your code sample), but this is definitely not the reason of infinite loop. 2. what is _tan_ value? you mean ten? But there maybe more then ten iterations required? 3. Putting `try/catch` everywhere is bad. – vasily.sib Nov 26 '18 at 10:42
  • Also, as for now this looks more like a comment, than an answer. Can you improve it? – vasily.sib Nov 26 '18 at 10:44