MC102

Computer Algorithms and Programming

Fifth List of Exercises

The exercises on this list cover classes until 29/04.
  1. Make a recursive function to find an element in a vector. The function must return the element index in the vector or -1 if it does not exist.
  2. Make a recursive routine to print the elements of a vector, in order of the lowest index first.
  3. Make a recursive routine to print the elements of a vector, in order of the largest index first.
  4. Perform a recursive routine to detect whether a string is a palindrome or not. Ignore all spaces in the string.
  5. Given a map in the form of an array of characters, make a recursive routine that finds and prints a path between position [1,1] and position [10,10]. An example of a map is shown below. On the map, the asterisks (*) represent walls, the green corner represents the start point and the blue corner the end point.
      
     *
      
      
      
      
      
      
      
      


     *

     *
     *

     *
     *
     *
     *


     *



     *

     *
     *
     *


     *


     *



     *
     *


     *

     *





     *

     *

     *







     *

     *



     *
     *
     *
     *
     *

     *
     *
     *










     *
     *
     *
     *
     *
     *
     *
     *
     *


  6. Make a recursive program that prints on the screen all the 4 letter strings that follow the following rule:
    • The first letter is a consonant
    • The second letter is a vowel
    • The third letter is a consonant
    • The fourth letter is a vowel
  7. Make a recursive program that looks for a given element in a vector of integers. Your recursive function must receive a function of the following type:
    Type LocalFunction = Function (e: integer): Boolean;
    Your procedure / function must receive a function as described as a parameter and use it to find the element. The function passed as a parameter returns true whether the indicated element is the desired element and false otherwise.
  8. Given the record:
    Product Type = Record
    name: string [40];
    price: real;
    stock: real;
    End;
    Make an ordering procedure that uses functions as a parameter and also define the comparison functions so that it is possible to order a vector of elements of type Product as follows:
    • In ascending order of name
    • In descending order of name
    • Ascending price
    • In decreasing order of price
    • In ascending order of stock and, in case of a tie, in ascending order of name
    • In decreasing order of stock and, in the event of a tie, in decreasing order of price
    Include in your program an example of calling the ordering routine for each of the functions indicated.
  9. Make a program to read several records described in exercise 8 from a file called products.txt. After reading, show each element read on the screen. Describe the format in which you expect the data to be stored in the file.
  10. Make a program that asks the user the name of a file, open that file and count how many lines have the word tests.
  11. Make a program that opens two files (first.txt e second.txt), where each file contains an ordered list of student names and writes to another file (third.txt) the names of all students in an orderly manner.
  12. Make a program that reads a text from a file and removes any spaces you find from that file. Write the answer in another text file.
  13. Make a program that reads text from a file and capitalizes all letters. Record the response in another file.
  14. Make a program that reads text from a file and changes all letters to lowercase. Record the response in another file.
  15. Combine exercises 13 and 14 using functions / procedures passed as a parameter.