Results 1 to 5 of 5

Thread: Pascal - Random number twodimensional array.

  1. #1
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default Pascal - Random number twodimensional array.

    I have twodimensional array that contains random numbers from 0 to 50.
    Attention to line 25. At the moment it prints out the highest number out of the random array. How can i print out the smallest?


    Code:
    program programname;
    uses crt;
    var
    m: array[1..3, 1..3] of integer;
    s2k,k, r, l, i, j: integer;
    
    begin
    clrscr;
    
    randomize;
    
    for i:=1 to 3 do for j:=1 to 3 do m[i,j]:=random(51);
    writeln ('Elements:');
    for i:=1 to 3 do
    begin
    for j:=1 to 3 do write(m[i,j]:3);
    writeln;
    end;
    
    for i:=1 to 3 do
    
    for j:=1 to 3 do
    begin
    
    if l < m[i,j] then
    begin
    l:= m[i][j];
    r:= i;
    k:= j;
    end;
    
    end;
    
    writeln ('Smallest number is ',l,' and its on ',r, ' row and ',k,' column);
    
    s2k:=0;
    for i:=1 to 3 do s2k:=s2k + m[3,i];
    
    writeln ('3. line sum: ', s2k);
    readln;
    end.

  2. #2
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Formatted it a bit, cause I had some trouble reading it.

    pascal Code:
    program programname;

    uses
      crt;

    var
      m: array[1..3, 1..3] of integer;
      s2k, k, r, l, i, j: integer;
     
    const
      Highest: Boolean;

    begin
      clrscr;

      randomize;

      for i := 1 to 3 do
        for j := 1 to 3 do
          m[i, j] := random(51);
      writeln ('Elements:');
      for i:=1 to 3 do
      begin
        for j:=1 to 3 do
          write(m[i,j]:3);
        writeln;
      end;

      for i := 1 to 3 do

        for j := 1 to 3 do
        begin

          if ((l < m[i,j]) = Highest) then
          begin
            l:= m[i][j];
            r:= i;
            k:= j;
          end;

        end;

      writeln ('Smallest number is ',l,' and its on ',r, ' row and ',k,' column'); //added '

      s2k := 0;
      for i := 1 to 3 do
        s2k := s2k + m[3, i];

      writeln ('3. line sum: ', s2k);
      readln;
    end.
    Last edited by masterBB; 08-07-2012 at 08:49 PM. Reason: added solution
    Working on: Tithe Farmer

  3. #3
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    It doesnt work? Shows that lowest number is 0 and its on row 0, line 0

  4. #4
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    pascal Code:
    program programname;

    uses
      crt;

    var
      m: array[1..3, 1..3] of integer;
      s2k, k, r, l, i, j: integer;
     
    const
      Highest: Boolean;

    begin
      clrscr;

      randomize;

      for i := 1 to 3 do
        for j := 1 to 3 do
          m[i, j] := random(51);
      writeln ('Elements:');
      for i:=1 to 3 do
      begin
        for j:=1 to 3 do
          write(m[i,j]:3);
        writeln;
      end;

      if not(Highest) then
        l := 51;

      for i := 1 to 3 do

        for j := 1 to 3 do
        begin

          if ((l < m[i,j]) = Highest) then
          begin
            l:= m[i][j];
            r:= i;
            k:= j;
          end;

        end;

      writeln ('Smallest number is ',l,' and its on ',r, ' row and ',k,' column'); //added '

      s2k := 0;
      for i := 1 to 3 do
        s2k := s2k + m[3, i];

      writeln ('3. line sum: ', s2k);
      readln;
    end.

    fix'd
    Working on: Tithe Farmer

  5. #5
    Join Date
    Dec 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    3 Post(s)

    Default

    Thank you ! Works.

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
  •