solutions/cw4/pascal.while
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 29 Nov 2024 18:59:32 +0000
changeset 976 e9eac62928f5
parent 959 64ec1884d860
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
959
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
rows := 13;
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
coef := 1;
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
i := 0;
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
while (i < rows) do {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  space := 1;
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  while (space <= rows - i) do {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
      write("  ");
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
      space := space + 1
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  };
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
  j := 0;
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  while (j <= i) do {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
      if ((j == 0) || i == 0) then {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
        coef := 1
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
      } else {
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
        coef := (coef * ((i - j) + 1)) / j
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
      };
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
      if (coef < 10) then write("   ") else 
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
      if (coef < 100) then write("  ") else
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
      if (coef < 1000) then write(" ")
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
      else skip;
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
      write(coef);
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
      j := j + 1
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  };
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
  write("\n");
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
  i := i + 1
64ec1884d860 updated and added pascal.while file
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}