Results 1 to 3 of 3

Thread: Variable trouble

  1. #1
    Join Date
    Jan 2007
    Posts
    834
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Variable trouble

    Trying to make a multiplier (And a divider so don't take my idea).

    SCAR Code:
    Function MultiplyMultiplication(Prob1: Integer; Prob2: Integer):Integer;
    Var P1,P2,I,Y,K :Integer;
    begin
      P1 := Prob1;
      P2 := Prob2;
      Y := 0;
      K := 0;
      Repeat
      K := Y + P1;
      I := I + 1;
      Until(I = P2)
      Writeln( IntToStr(K) )
      Result := K;
    end;

    Compiles fine. But when i multiply 2*2 it comes out to be 2. Something may be wrong with the variables i suppose? I used For I:= 0 To P2 Do... And thought that was the problem. I talked to soon. Any ideas on how i should work this out?

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

    Default

    Heres a much shorter version:

    Code:
    program New;
    
    function Multiply(Prob1, Prob2: Integer): Integer;
    var
      i : Integer;
    begin
      for i := 1 to Prob2 do
        Result := Result + Prob1;
    end;
    
    begin
      Writeln(Multiply(12, 12));
    end.
    EDit: Btw, your problem was K := Y + P1; Y is always 1. It should have been K := K + P1;

  3. #3
    Join Date
    Jan 2007
    Posts
    834
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks r!ch!e

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
  •