MC102

Computer Algorithms and Programming

Third List of Exercises

The exercises on this list cover classes until 27/03.
  1. Write a program that reads 2 10 x 10 matrices from the keyboard (X and Y) and then presents the following list of options to the user: (A) Add the two matrices (Z = X + Y); (B) Subtract the two matrices (Z = X - Y); (C) Multiply the two matrices (Z = X * Y); (D) Create a matrix with the largest elements of each matrix (Z[i,j] = MAX(X[i,j], Y[i,]); (E) Create a matrix with the smallest elements of each matrix (Z[i,j] = MIN(X[i,j], Y[i,j]). (F) End the program. After presenting the list of options, read a character from the keyboard, execute the selected option and show the resulting matrix. Repeat until the user chooses the letter F. Suggestion: Use procedures to break your program into parts.
  2. Have you ever played "Minefield"? Make a program that reads a 20 x 20 matrix of characters from the keyboard. Each character can be: * (asterisk) represents a bomb at the read coordinate; - (dash) represents a non-bomb location. Create an array of 20 x integers 20 that contains for each position [i,j] the number of bombs in the vicinity Print this matrix on the screen. Each position has a maximum of 8 neighbors (diagonals count). See example for a 5x5 matrix:
  3. Entrada:
    *
    -
    -
    -
    -
    -
    -
    -
    -
    -
    *
    *
    *
    *
    *
    -
    -
    -
    -
    *
    * - - - *
    Output:
    0
    1
    0
    0
    0
    3
    4
    3
    3
    2
    1
    2
    2
    3
    2
    3
    4
    3
    5
    3
    0
    1
    0
    2
    1

  4. How many numbers will the program below print on the screen?
    Program Peers;
    Var counter : Integer;

    Function NumeroPar(number : Integer) : Boolean;
    Begin
    NumeroPar := (number mod 2) = 0;
    End;

    Begin
    For counter := 1 To 100 Do
    If (ParNumber(count)) Then
    WriteLn(counter);
    End.
  5. What will the program below print on the screen?
    Program Lines;
    Var counter : Integer;

    Procedure PrintLine(line : Integer);
    Var counter : Integer;
    Begin
    For counter := 1 To line Do
    Write(counter);
    WriteLn;
    End;

    Begin
    For counter := 1 To 10 Do
    PrintLine(counter);
    End.
  6. What does the program below do? How many times will the AcertaPosicao procedure be executed? How many times will the Swap procedure be executed?
    Program Numbers;
    Const LIMIT = 5;
    Var counter : Integer;
    numbers : Array[1..LIMIT] Of Integer;

    Procedure Swap(x, y : Integer);
    Temporary Var : Integer;
    Begin
    temporary := numbers[x];
    numbers[x] := numbers[y];
    numbers[y] := temporary;
    End;

    Procedure AcertaPosicao(position : Integer);
    Var index : Integer;
    Begin
    For index := position + 1 To LIMIT Do
    If (numbers[index] < numbers[position]) Then
    Exchange(position, index);
    End;

    Procedure LeNumbers;
    Var index : Integer;
    Begin
    WriteLn('Enter ', LIMIT, ' numbers: ');
    For index := 1 To LIMIT Do
    ReadLn(numbers[index]);
    End;

    Procedure ShowNumbers;
    Var index : Integer;
    Begin
    Write('The result is: ');
    For index := 1 To LIMIT Do
    Write(numbers[index]:6);
    WriteLn;
    End;

    Begin
    LeNumbers;
    For counter := 1 To 4 Do
    AcertaPosicao(counter);
    ShowNumbers;
    End.
  7. The function Media, which averages the elements of the vector, and Contagem, which counts how many numbers in the array are above a given value, are incomplete in the program below. Make an implementation for them.
    Statistical Program;
    Const LIMIT = 100;
    Var counter : Integer;
    numbers : Array[1..LIMIT] Of Integer;

    FunctionMedia: Integer;
    { Put your code here }

    Function Count(number : Integer) : Integer;
    { Put your code here }

    Procedure LeNumbers;
    Var index : Integer;
    Begin
    WriteLn('Enter ', LIMIT, ' numbers: ');
    For index := 1 To LIMIT Do
    ReadLn(numbers[index]);
    End;

    Begin
    LeNumbers;
    WriteLn('The mean of the numbers entered is: ', Media);
    WriteLn('Exist ', Count(Media), ' numbers above average.');
    End.
  8. Write a program that reads a 10 x 10 matrix from the keyboard and then prints the position and value of the smallest number in the matrix on the screen. Tip: Do you really need a matrix to implement this program?
  9. Write a program that reads a 10 x 10 matrix from the keyboard and then prints the average of the elements in each row and also the average of the entire matrix on the screen. Also print at the end of your program the number of numbers above the matrix average. Tip: Use procedures and functions to simplify your program. Take advantage of procedures and functions you've done before. Is there any similarity between this program and number 6?
  10. Write a program that reads a 15 x 15 matrix from the keyboard and sorts the elements of each row in the matrix. Print the resulting matrix. Tip: Can you take advantage of any procedure or function you've already done?
  11. Do a program similar to exercise number 8, except that it also counts how many primes were entered in the matrix. Tip: Create the role Primo which takes a number as a parameter and returns a Boolean indicating whether the number is prime or not.