lundi 27 novembre 2017

Solving Linear and Non-Linear Systems of Equations with No Solution (Java)

Both of the following pieces of code have something wrong with the math. One solves a linear system of equations in standard form and the other solves systems of quadratic equations. There seems to be something wrong with the last sections where it checks if the answer is no solution or not. Please help

Solving Systems of Linear Equations in Standard Form Program:

import javax.swing.JOptionPane;
public class StandardForm
{
     public static void StandardForm( String[] Args[] ) {   
       String inserta1;
       String insertb1;
       String insertc1;
       String inserta2;
       String insertb2;
       String insertc2;
       double a1;
       double b1;
       double c1;
       double a2;
       double b2;
       double c2;

       System.out.println("Here you can learn how to solve linear systems of equations in Standard Form:");
       System.out.println("ax+by=c");
       System.out.println("Here are some practice problems.");
       System.out.println("Once you have completed them, use the program to check you answer.");
       System.out.println("Once you are totally finished, type stop for any of the values to exit the program.");
       System.out.println("1. 2x+y=1 and x-2y=8");
       System.out.println("2. 3x+2y=4 and x+2y=-4");
       System.out.println("3. 4x+y=1 and x+y=-2");
       System.out.println("4. x+3y=-9 and 5x+3y=3");
       System.out.println("5. x+2y=4 and 3x+2y=8");
       System.out.println("6. 5x-3y=-12 and 2x+3y=-9");
       System.out.println("7. x-y=-1 and 2x+y=4");
       System.out.println("8. 2x-y=-2 and 2x-y=3");
       System.out.println("9. x+y=-6 and 4x+y=3");
       System.out.println("10. x-7y=-21 and 13x-7y=63");
       while ( true ) {
       inserta1 =
        JOptionPane.showInputDialog( "Enter the a value of the first equation." );
       if( inserta1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb1 =
        JOptionPane.showInputDialog( "Enter the b value of the first equation." );
       if( insertb1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc1 =
        JOptionPane.showInputDialog( "Enter the c value of the first equation." );
       if( insertc1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       inserta2 =
        JOptionPane.showInputDialog( "Enter the a value of the second equation." );
       if( inserta2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb2 =
        JOptionPane.showInputDialog( "Enter the b value of the second equation." );
       if( insertb2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc2 =
        JOptionPane.showInputDialog( "Enter the c value of the second equation." );
       if( insertc2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       a1 = Double.parseDouble( inserta1 );
       b1 = Double.parseDouble( insertb1 );
       c1 = Double.parseDouble( insertc1 );
       a2 = Double.parseDouble( inserta2 );
       b2 = Double.parseDouble( insertb2 );
       c2 = Double.parseDouble( insertc2 );

       double x = (b2 * a2 - b1 * c2)/(a1 * a2 - b1 * c1);
       double y = (a1 * c2 - b2 * c1)/(a1 * a2 - b1 * c1);
       double inf = Double.POSITIVE_INFINITY;
       if((a1*x)+(b1*y) != c1 || (a2*x)+(b2*y) != c2) {
           JOptionPane.showMessageDialog(
         null, "This system has no solution.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else if(x == inf && y == inf) {
            JOptionPane.showMessageDialog(
         null, "This system has infinately many solutions.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else {
           JOptionPane.showMessageDialog(
         null, "Your answer is ( " + x + ", " + y + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
    }   
}
}

Solving Systems of Quadratic Equations Program:

import javax.swing.JOptionPane;
public class Quadratic
{
       public static void StandardForm( String[] Args[] ) {   
       String inserta1;
       String insertb1;
       String insertc1;
       String inserta2;
       String insertb2;
       String insertc2;
       double a1;
       double b1;
       double c1;
       double a2;
       double b2;
       double c2;

       System.out.println("Here you can learn how to solve systems of quadratic equations in standard form:");
       System.out.println("y=ax^2+bx+c");
       System.out.println("Here are some practice problems.");
       System.out.println("Once you have completed them, use the program to check you answer.");
       System.out.println("Once you are totally finished, type stop for any of the values to exit the program.");
       System.out.println("1. Problem");
       System.out.println("2. Problem");
       System.out.println("3. Problem");
       System.out.println("4. Problem");
       System.out.println("5. Problem");
       System.out.println("6. Problem");
       System.out.println("7. Problem");
       System.out.println("8. Problem");
       System.out.println("9. Problem");
       System.out.println("10. Problem");
       while ( true ) {
       inserta1 =
        JOptionPane.showInputDialog( "Enter the a value of the first equation." );
       if( inserta1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb1 =
        JOptionPane.showInputDialog( "Enter the b value of the first equation." );
       if( insertb1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc1 =
        JOptionPane.showInputDialog( "Enter the c value of the first equation." );
       if( insertc1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       inserta2 =
        JOptionPane.showInputDialog( "Enter the a value of the second equation." );
       if( inserta2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb2 =
        JOptionPane.showInputDialog( "Enter the b value of the second equation." );
       if( insertb2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc2 =
        JOptionPane.showInputDialog( "Enter the c value of the second equation." );
       if( insertc2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       a1 = Double.parseDouble( inserta1 );
       b1 = Double.parseDouble( insertb1 );
       c1 = Double.parseDouble( insertc1 );
       a2 = Double.parseDouble( inserta2 );
       b2 = Double.parseDouble( insertb2 );
       c2 = Double.parseDouble( insertc2 );

       double finala = a2 - a1;
       double finalb = b2 - b1;
       double finalc = c2 - c1;
       double x1 =((Math.sqrt(Math.pow(finalb,2)-(4*finala*finalc))-finalb)/2);
       double x2 =(-finalb + Math.sqrt(Math.pow(finalb,2)-(4*finala*finalc)))/2;
       double y1 = (-a1*x1+c1)/b1;
       double y2 = (-a1*x2+c1)/b1;
       double inf = Double.POSITIVE_INFINITY;
       if((a2*x1)+(b2*y1) != c2 && (a2*x2)+(b2*y2) != c2) {
           JOptionPane.showMessageDialog(
         null, "This system has no solution.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else if((a2*x1)+(b2*y1) == c2 && (a2*x2)+(b2*y2) != c2) {
             JOptionPane.showMessageDialog(
         null, "Your answer is ( " + x1 + ", " + y1 + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );           
        }
       else if((a2*x1)+(b2*y1) != c2 && (a2*x2)+(b2*y2) == c2) {
             JOptionPane.showMessageDialog(
         null, "Your answer is ( " + x2 + ", " + y2 + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );           
        }
       else if(x1 == inf && y1 == inf || x2 == inf && y2 == inf) {
            JOptionPane.showMessageDialog(
         null, "This system has infinately many solutions.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else {
           JOptionPane.showMessageDialog(
         null, "Your answers are ( " + x1 + ", " + y1 + " ) and ( " + x2 + ", " + y2 + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
    }   
}
}

simplification of algorithm that uses if statements to incorporate efficient iteration

I have the following code that has a small error but otherwise performs what I believe is an insertion sort. I have two questions 1. Why is the index error occurring on running this? 2. How can I, for teaching and learning purposes, simplify the entire code, to incorporate the use of for loops. I would like someone to explain the places in which they are repetition and how they correspond to a solution that uses loops. *I realise that I need to use a nested loop using i and j, but can't quite work out how. I have added the outer level loop that provides i for the length of the loop.

Code:

def main():
  #test with 6125
  #test with 0142
  #test with 4312
  #test with 5432

  list=[4,2,6,1]
  print(list)
  for i in range(len(list)):
    #checking first element
    if list[i+1]<list[i]:
      temp1=list[i]
      list[i]=list[i+1]
      list[i+1]=temp1
      print("first iteration:",list)
    #Checking second element
    if list[i+2]<list[i]:
        temp1=list[i]
        list[i]=list[i+2]
        list[i+2]=temp1
        print("second iteration:",list)

    if list[i+2]>list[i]:
      if list[i+2]<list[i+1]:
        temp2=list[i+1]
        list[i+1]=list[i+2]
        list[i+2]=temp2
        print("second iteration else #1:",list)
    #Checking Third element
    if list[i+3]<list[i]:
        temp1=list[i]
        temp2=list[i+1]
        temp3=list[i+2]
        list[i]=list[i+3]
        list[i+1]=temp1
        list[i+2]=temp2
        list[i+3]=temp3

        print("third iteration:",list)
    elif list[i+3]>list[i]:
      if list[i+3]<list[i+1]:
        temp1=list[i+1]
        temp2=list[i+2]
        temp3=list[i+3]
        list[i+1]=temp2
        list[i+2]=temp3
        list[i+3]=temp1
        print("third iteration else #1:",list)
      elif list[i+3]>list[i+1]:
        if list[i+3]<list[i+2]:
          temp3=list[i+2]
          list[i+2]=list[i+3]
          list[i+3]=temp3
          print("third iteration else #2!",list)
main()

Output #1:

#Test with [4, 2, 6, 1]
('first iteration:', [2, 4, 6, 1])
('third iteration:', [1, 2, 4, 6])
**IndexError:** list index out of range on line 54 in main.py

Output Test #2

#Test with [5, 4, 3, 2]
('first iteration:', [4, 5, 3, 2])
('second iteration:', [3, 5, 4, 2])
('second iteration else #1:', [3, 4, 5, 2])
('third iteration:', [2, 3, 4, 5])

A trinket to the code can be found here (for online fixes) http://ift.tt/2n6zycI

Note: I realise python can perform this without using the temporary variables, but I wish to implement the solution using them. Please, in any answers, use the existing and my originally provided code to break down.

If else doesn't work in Perl

So, I made a program in Perl where the user should type in a username. If he types in "admin", the message "Welcome, admin!". In other cases, the console output will be: "The username is incorrect." Here is my code:

use utf8;

print "Username: ";
$username = <STDIN>;
if ($username eq "admin")
{
    print "Welcome, admin!";
}
else
{
    print "The username is incorrect.";
}

The problem is that whatever the user inputs, the program continues, going on the else branch. So, even if he types in: "admin", the console's output will be: "The username is incorrect."

Why does this happen?

And, another question: why do I need to put braces to surround the if-else instructions? It is only one instruction in each branch.

c++ If statement comparing pointer with int

enter image description here I'm trying to compare pointer value with int. Their value is the same, u can check it where my mouse is but still IF statement doesn't work. Can u tell me what I'm doing wrong and how can I solve this problem.

power-shell if statement assistance

My scripting knowledge is zero. I am working with a power shell script and need help.

sys.fn_hadr_is_primary_replica ('test01')

returns a 1 for true or 0 for false. When this returns a zero I need it to execute.

alter availability group TEST01 failover

can anyone help me put these two pieces together?

Python loop and if statment weird issues

Ok, I dont understand what is happening. I am looping over output from my query. A pretty normal loop and if. The issue is when I try to do results[o] < 30. It does not print out the variable. There are column data less than 30 for some reason they don't print. Even if I print a string. Nothing happens. The else statement will print but not the if. On my flask template I have similar code and it works. What am I doing incorrectly. The results[0] is of type int. So I don't know what the issue is. I am not getting any errors.

    for recordset in return_stored_procedure:
        results.append(dict(zip(sp_column_names, recordset)))
        if results[0]['days_column'] <= 30:
           print variable
        else:
           print variable

How come React.js Conditional rendering doesn't work iOS 9 and below?

I have multiple components that follow this rendering structure if I am fetching data from a database. Basically, on componentWillMount() I fetch data from my API, while this is happening a spinner gets displayed on the screen, which is the else if statement. Once data is loaded the if statement is supposed to run which works on my desktop, and iPhone 5 & iPad mini on iOS 11. However, if I test my website on my iPhone 6, and iPad which are on iOS 9.3.3. The website gets stuck on the second else if statement. Basically, the spinner just stays, and never switches to show the data when it loads. Is this an iOS 9 issue or is it based on how I wrote my rendering function?

Sample code that doesn't work

Code that does work

renderPage(){
    if(this.state.productDataLoaded && this.state.productData){
        return(
            <div className="single-brand">
                <h1 className="ui header">Shopping Bag</h1>
                <div className="page-container ui container">
                    {this.renderClothing()}
                </div>
            </div>
        )
    }else if(this.state.productDataLoaded === false ){
        return(
            <div className="ui active inverted dimmer">
                <div className="ui indeterminate text loader">Preparing Files</div>
            </div>
        )
    }else{
        return(
            // returns 404
        )
    }
}
render(){
    return(
        <section id="cart">
            {this.renderPage()}
        </section>
    )
}