Results 1 to 2 of 2

Thread: Help with Integer types

  1. #1
    Join Date
    Jul 2008
    Location
    US
    Posts
    104
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Help with Integer types

    Okay I haven't been very involved with scripting lately. If I do get a break, I sleep lol. Basketball is rough. I forgot a lot of the data types. so can someone help me with this. This solves for the point of two equations. I forgot what its called. Don't really worry about the body of the code. It can be really confusing. I'm pretty sure my errors are in the titles and returns of my functions and procedures
    Code:
    Ex:
    
    (5x+3y=5
    (4x+2y=7
    
    Find the Intersection. 
    
         By the way, the variables I'm maliputlating are A, B, C. Ax+By=C. It uses elimination. 
         I plan on making a calculator for use with these, Quadratic functions/models, And such...
    Here's the code:

    SCAR Code:
    program SolutionFinder_2Var;

    // Format
    // A(x)+B(y)=C

    // Input
    // A
    // B
    // C


    // First Line
    Const A1 = 5;
    Const B1 = 3;
    Const C1 = 6;
    // Second Line
    Const A2 = 4;
    Const B2 = 5;
    Const C2 = 2;


    {--------Dont Touch Below This-------------}
    Var AX, AY: Extended;
        Line, FixedEquations: Array of Array of Integer;



    Procedure WriteEquation(TPA: Array of Integer; Name: String);
    Var X,Y,C: Integer;
    Begin
      X := TPA[0];
      Y := TPA[1];
      C := TPA[2];
      WriteLn('Name: ' + Name);
      Writeln(IntToStr(X)+'X'+ IntToStr(Y)+'Y'+ '=' + IntToStr(C));
    End;

    // Changes Both Equations, To Cancel Out the X's
    Function FixEquationXs(TPA1, TPA2: Array of Integer): Array of Array of Integer;
    Var F, S: Integer;
        FirstE, SecondE: Array of Integer;
        Answer: Array of Array of Integer;
    Begin
      // Solves by timing the Y's together. Causing them to alway cancel out
      // Multiplying The First Equation, Bu TPA2's Y value
      S := TPA2[1];
      FirstE[0] := (TPA1[0]*S);
      FirstE[1] := (TPA1[1]*S);
      FirstE[2] := (TPA1[2]*S);
      F := TPA1[1];
      SecondE[0] := (TPA2[0]*F);
      SecondE[0] := (TPA2[0]*F);
      SecondE[0] := (TPA2[0]*F);
      // Need Y's to be Opposites
      SecondE[0] := (SecondE[0]*-1);
      SecondE[0] := (SecondE[1]*-1);
      SecondE[0] := (SecondE[2]*-1);
      Answer := [FirstE, SecondE];


      WriteLn('Equations awaiting For Elimination');
      WriteEquation(FirstE, 'First Equation');
      WriteEquation(FirstE, 'First Equation');

      Result := Answer;
    End;



    // Sets up Equations, Enabling the Elimination of Y Values
    Function SolveForXStep1(TPA1, TPA2: Array of Integer): Array of Array of Integer;
    var  Answer: Array of Array of Integer;
    Begin
      If (not(TPA1[0] = TPA2[0])) Then   // If x's aren't the same, to begin
      Begin
         If (Abs(TPA1[0]) = Abs(TPA2[0])) Then    // If the problem is Ready to go!
         Begin
           Answer[0] := TPA1;
           Answer[1] := TPA2;
           Result := Answer;
           Exit;
         End;
         // Equations need to be fixed!!!
         Answer := FixEquationXs(TPA1, TPA2);
         Result := Answer;
         Exit;
       End;
       // Y's are the same. Need to Be opposites
       //Answer[0] := [TPA1[0], TPA1[1], TPA1[2]];
       Answer[0][0] := TPA1[0];
       Answer[0][1] := TPA1[1];
       Answer[0][2] := TPA1[2];
       
       Answer[1][0] := (TPA2[0]* -1);
       Answer[1][1] := (TPA2[1]* -1);
       Answer[1][2] := (TPA2[2]* -1);
       Result := Answer;
    End;

    Function SolveForX(Equations: Array of Array of Integer): Integer;
    Var X, Y, C, Answer: Integer;
    Begin
      X := (Equations[0][0] + Equations[1][0]);
      Y := (Equations[0][1] + Equations[1][1]);
      C := (Equations[0][2] + Equations[1][2]);
      // In Case of Both Variables Cancle Out
      If ((X=0) And (Y=0)) Then
        If (C=0) Then
          Begin
          WriteLn(' Answer: Infinent Solutions');
          TerminateScript;
        End Else
        Begin
          WriteLn(' Answer: No Solutions');
          TerminateScript;
        End;
      // In case we didn't use elimination right. Rele, me donig bad scripting
      If (Not(Y=0)) Then
      Begin
        WriteLn('The Eqations did not get solved for X');
        TerminateScript;
      End;
      Answer := (C/X);
      Result := Answer;
    End;

    Function SolveForY(xVal: Integer): Integer;
    Var xTotal,xWork, yVal, cVal, cWork,  Answer: Integer;
    Begin
      xTotal := (xVal*A1);
      yVal   := B1;
      cVal   := C1;
      xWork  := (xTotal*-1);
      cWork  := (cVal+xWork);
      Answer := (cWork/yVal);
      Result := Answer;
    End;

    Procedure WriteSolution(X, Y: Integer);
    Begin
      WriteLn('Solution = ' + '('+ IntToStr(X)+ ',' + IntToStr(Y) + ')');
    End;

    begin
    // Setup Variables
    Line[0][0] :=  A1;
    Line[0][1] :=  B1;
    Line[0][2] :=  C1;

    Line[1][0] :=  A2;
    Line[1][1] :=  B2;
    Line[1][2] :=  C2;
    //-------------

    FixedEquations :=  SolveForXStep1(Line[0], Line[1]);  // Changes equations to have oppisite values
    AX := SolveForX(FixedEquations);    //  Solves to find X value
    AY := SolveForY(AX); [B]E[/B]               //  Solves to find Y value
    WriteSolution(AX, AY);              //  Prints it out
    end.

    At the moment, a error shows right above ( At the bolded {E} )

  2. #2
    Join Date
    Oct 2006
    Posts
    500
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Code:
    Function SolveForY(xVal: Integer): Integer;
    Solve for Y takes an integer, yet you are passing an extended var.

    Code:
    Var AX, AY: Extended;
    AY := SolveForY(AX);
    Just change to:

    Code:
    Function SolveForY(xVal: Extended): Extended;
    Var xTotal,xWork, yVal, cVal, cWork,  Answer: Extended;
    Code:
    Procedure WriteSolution(X, Y: Integer);
    This takes an integer, yet you pass an extended.

    Edit: I don't have enough time to go through this wholly but here's something that compiles/may work. Look through it:

    Code:
    program SolutionFinder_2Var;
    
    // Format
    // A(x)+B(y)=C
    
    // Input
    // A
    // B
    // C
    
    
    // First Line
    Const A1 = 5;
    Const B1 = 3;
    Const C1 = 6;
    // Second Line
    Const A2 = 4;
    Const B2 = 5;
    Const C2 = 2;
    
    
    {--------Dont Touch Below This-------------}
    Var AX, AY: Integer;
        FixedEquations: Array of Array of Integer;
        Line: Array [0..1] of Array[0..2] of Integer;
    
    
    
    Procedure WriteEquation(TPA: Array of Integer; Name: String);
    Var X,Y,C: Integer;
    Begin
      X := TPA[0];
      Y := TPA[1];
      C := TPA[2];
      WriteLn('Name: ' + Name);
      Writeln(IntToStr(X)+'X'+ IntToStr(Y)+'Y'+ '=' + IntToStr(C));
    End;
    
    // Changes Both Equations, To Cancel Out the X's
    Function FixEquationXs(TPA1, TPA2: Array of Integer): Array of Array of Integer;
    Var F, S: Integer;
        FirstE, SecondE: Array [0..2] of Integer;
        Answer: Array of Array of Integer;
    Begin
      // Solves by timing the Y's together. Causing them to alway cancel out
      // Multiplying The First Equation, Bu TPA2's Y value
      S := TPA2[1];
      FirstE[0] := (TPA1[0]*S);
      FirstE[1] := (TPA1[1]*S);
      FirstE[2] := (TPA1[2]*S);
      F := TPA1[1];
      SecondE[0] := (TPA2[0]*F);
      SecondE[0] := (TPA2[0]*F);
      SecondE[0] := (TPA2[0]*F);
      // Need Y's to be Opposites
      SecondE[0] := (SecondE[0]*-1);
      SecondE[0] := (SecondE[1]*-1);
      SecondE[0] := (SecondE[2]*-1);
      Answer := [FirstE, SecondE];
    
    
      WriteLn('Equations awaiting For Elimination');
      WriteEquation(FirstE, 'First Equation');
     // WriteEquation(FirstE, 'First Equation');
    
      Result := Answer;
    End;
    
    
    
    // Sets up Equations, Enabling the Elimination of Y Values
    Function SolveForXStep1(TPA1, TPA2: Array of Integer): Array of Array of Integer;
    var  Answer: Array of Array of Integer;
    Begin
      If (not(TPA1[0] = TPA2[0])) Then   // If x's aren't the same, to begin
      Begin
         If (Abs(TPA1[0]) = Abs(TPA2[0])) Then    // If the problem is Ready to go!
         Begin
           Answer[0] := TPA1;
           Answer[1] := TPA2;
           Result := Answer;
           Exit;
         End;
         // Equations need to be fixed!!!
         Answer := FixEquationXs(TPA1, TPA2);
         Result := Answer;
         Exit;
       End;
       // Y's are the same. Need to Be opposites
       //Answer[0] := [TPA1[0], TPA1[1], TPA1[2]];
       Answer[0][0] := TPA1[0];
       Answer[0][1] := TPA1[1];
       Answer[0][2] := TPA1[2];
    
       Answer[1][0] := (TPA2[0]* -1);
       Answer[1][1] := (TPA2[1]* -1);
       Answer[1][2] := (TPA2[2]* -1);
       Result := Answer;
    End;
    
    Function SolveForX(Equations: Array of Array of Integer): Integer;
    Var X, Y, C, Answer: Integer;
    Begin
      X := (Equations[0][0] + Equations[1][0]);
      Y := (Equations[0][1] + Equations[1][1]);
      C := (Equations[0][2] + Equations[1][2]);
      // In Case of Both Variables Cancle Out
      If ((X=0) And (Y=0)) Then
        If (C=0) Then
          Begin
          WriteLn(' Answer: Infinent Solutions');
          //TerminateScript;
        End Else
        Begin
          WriteLn(' Answer: No Solutions');
          //TerminateScript;
        End;
      // In case we didn't use elimination right. Rele, me donig bad scripting
      If (Not(Y=0)) Then
      Begin
        WriteLn('The Eqations did not get solved for X');
        //TerminateScript;
      End;
      Answer := (C/X);
      Result := Answer;
    End;
    
    Function SolveForY(xVal: Integer): Integer;
    Var xTotal,xWork, yVal, cVal, cWork,  Answer: Integer;
    Begin
      xTotal := (xVal*A1);
      yVal   := B1;
      cVal   := C1;
      xWork  := (xTotal*-1);
      cWork  := (cVal+xWork);
      Answer := (cWork/yVal);
      Result := Answer;
    End;
    
    Procedure WriteSolution(AX, AY: Integer);
    Begin
      WriteLn('Solution = (' + IntToStr(AX)+ ',' + IntToStr(AY) + ')');
    End;
    
    begin
    // Setup Variables
    Line[0][0] :=  A1;
    Line[0][1] :=  B1;
    Line[0][2] :=  C1;
    
    Line[1][0] :=  A2;
    Line[1][1] :=  B2;
    Line[1][2] :=  C2;
    //-------------
    
    FixedEquations :=  SolveForXStep1(Line[0], Line[1]);  // Changes equations to have oppisite values
    AX := SolveForX(FixedEquations);    //  Solves to find X value
    AY := SolveForY(AX); //E               //  Solves to find Y value
    WriteSolution(AX, AY);              //  Prints it out
    end.
    Last edited by Rick; 11-14-2009 at 12:41 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •