-2

Imagine - there's a house with 80 flats. It has 4 floors and 5 blocks. Each block has 4 flats. User is asked to input flat number and Pascal program is supposed to calculate and output flat number. This must be calculated using some kind of formula. The only tip I have is that I have to use div and mod operations.

This is how the house looks like - enter image description here

So far, I've created program, that loops through all 80 flats and after each 16 flats increases block value and after each 4 blocks increases stair.

This is my code:

program project1;
var
  i, floors, blocks, flats, flat, block, floor, blockCounter, floorCounter : integer;
begin
  floors := 4;
  blocks := 5;
  flats := 80;

  while true do
  begin
    write('Flat number: ');
    read(flat);

    block := 1;
    floor := 1;
    blockCounter := 0;
    floorCounter := 0;

    for i := 1 to 80 do
    begin
      blockCounter := blockCounter + 1;
      floorCounter := floorCounter + 1;
      if (floorCounter = 4) then
      begin
         floorCounter := 0;
         floor := floor + 1;
      end;

      if (blockCounter > 16) then
      begin
         block := block + 1;
         blockCounter := 0;
         floorCounter := 0;
         floor := 1;
      end;

      if (i = flat) then
      begin
         writeln('Flat nr. ', flat, ' is in ', floor, '. floor and in ', block, '. block!');
      end;

    end;
  end;
end.

Is there anyone who can help me with this?

Gustavs R
  • 61
  • 1
  • 8
  • 3
    So where are your attempts with mod and div? Did you study them? – Marco van de Voort Oct 11 '16 at 08:18
  • 4
    If you have studied `div` and `mod` and know what they do, calculating the block and floor for a given flat number is actually trivial and can be achieved with two simple assignment statements. Work out how to do it arithmetically on paper and then just code that. Since this is obviously homework, I'm going to leave you to figure it out. – MartynA Oct 11 '16 at 11:12
  • hint: the number 16 is important. – Stuart Oct 11 '16 at 19:27
  • 1
    Have you come across division with remainders before? – David Heffernan Oct 11 '16 at 19:52
  • It is a pitty, that you don't seem to be interested in responding to the comments you have got. There's really nothing more to do here than to vote to close the question, sorry. – Tom Brunberg Oct 12 '16 at 11:03
  • Sorry, I was not able to read these comments. Actually, I haven't been at school for a while, so that's why I can't do this task. Ok, there's nothing hard to understand how `div` works, because it is simple division, but I still do not understand how `mod` works. – Gustavs R Oct 12 '16 at 19:31
  • But you were lurking here after David's comment yesterday. Anyway, it's important to remember though, that `div` is *integer division* and returns a whole number. `mod` returns the remainder. These are easy to look up in the docs or wiki. 15 div 6 returns 2 and 15 mod 6 returns 3. – Tom Brunberg Oct 12 '16 at 22:23

1 Answers1

1

I've finally solved my problem myself. I finally undersood how div works, so I was able to solve this.

program Maja;
var dzivoklis, kapnutelpa, stavs : integer;
begin

  while true do
  begin
    write('Ievadi dzivokla numuru: ');
    read(dzivoklis);

    kapnutelpa := ((dzivoklis - 1) div 16) + 1;
    stavs := (((dzivoklis - 1) mod 16) div 4) + 1;
    writeln('Kapnutelpa: ', kapnutelpa);
    writeln('Stavs: ', stavs);
    writeln();
  end;

end.
Gustavs R
  • 61
  • 1
  • 8