lOMoARcPSD| 59735516
Homework lecture 7
Introduction to Algorithms
1. Given a list of real numbers, your task is to write a program to sort these numbers
increasingly.
Input: The file ‘numbers.txt’ consists of n real numbers separated by spaces or new line
characters.
Output: The sorted numbers are written to file ‘numbers.sorted’, two numbers are
separated by a space character.
numbers.txt numbers.sorted
3 5 2 2 1 8 1 2 2 3 5 8
Tips: you can apply the selec 琀椀 on sort algorithm (presented at the lecture 7) to do this
exercise.
2. Given a matrix A of m rows (numbered from 1 to m) and n columns (numbered from 1
to n) containing integer numbers, your task is to write a program to find the rectangle
with the largest sum.
Input: The file ‘matrix.txt’ consists of m + 1 lines. The first line consists of m and n. The
next m lines each has n integer numbers separated by a spaces.
Output: Write to file ‘matrix.out’ 5 numbers: r1 c1 r2 c2 s indicating that the rectangle
from (r1, c1) to (r2, c2) has the largest sum (i.e. s).
Example
Tips: let S (r1, c1, r2, c2) is the suma
琀椀
on of the rectangle's elements. We see that
S(r1,c1,r2,c2)= S(1,1,r2,c2) - S(1,1,r1-1,c2)- S(1,1,r2,c1-1)+ S(1,1,r1,c1)
3. Given two integer number X and Y, your task is to write a program to find the greatest
common divisor of X and Y using recursion.
Input: Input come from keyboard containing two number X and Y
Output: Ouput are written to screen contaning the greatest common divisor.
Matrix.tx
t
Matrix.ou
t
3
5
-1
-1 -1 -1
-1
2 -2 1
2 -1 -1 -1 -
4 2 5
lOMoARcPSD| 59735516
Example:
Keyboard
Screen
10
10
50
4. Given an integer number n, your task is to list all binary number of length n.
Input: The number n comes from the Keyboard
Output: Binary numbers of length n are written to the screen each in one line.
Example:
Tips: Read and understand the pseudo code of the excercise presented at the lecture 7.
5. Given an integer number n, your task is to list all permutations of length n.
Input: The number n comes from the Keyboard
Output: Permutations of length n are written to the screen each in one line.
Example:
3
Keyboard
Screen
123
132
213
231
312
321
Keyboar
Scree
3
00
0
00
1
01
0
01
1
10
0
10
1
11
0
11
1
Tips: a permuta
琀椀
on of n elements can be generated by a permuta
琀椀
on of n-1 elements associa
琀椀
ng to a number from 1 to n n . Therefore, this exercise and the fourth exercise can be solved the
same method.

Preview text:

lOMoAR cPSD| 59735516 Homework lecture 7
Introduction to Algorithms
1. Given a list of real numbers, your task is to write a program to sort these numbers increasingly.
Input: The file ‘numbers.txt’ consists of n real numbers separated by spaces or new line characters.
Output: The sorted numbers are written to file ‘numbers.sorted’, two numbers are
separated by a space character. numbers.txt numbers.sorted 3 5 2 2 1 8 1 2 2 3 5 8
Tips: you can apply the selec 琀椀 on sort algorithm (presented at the lecture 7) to do this exercise.
2. Given a matrix A of m rows (numbered from 1 to m) and n columns (numbered from 1
to n) containing integer numbers, your task is to write a program to find the rectangle with the largest sum.
Input: The file ‘matrix.txt’ consists of m + 1 lines. The first line consists of m and n. The
next m lines each has n integer numbers separated by a spaces.
Output: Write to file ‘matrix.out’ 5 numbers: r1 c1 r2 c2 s indicating that the rectangle
from (r1, c1) to (r2, c2) has the largest sum (i.e. s). Example Matrix.tx t Matrix.ou t 3 5 1 5 4 2 5 -1 -1 -1 -1 2 -1 2 -2 1 3 2 -1 -1 -1 - 1
Tips: let S (r1, c1, r2, c2) is the suma 琀椀 on of the rectangle's elements. We see that
S(r1,c1,r2,c2)= S(1,1,r2,c2) - S(1,1,r1-1,c2)- S(1,1,r2,c1-1)+ S(1,1,r1,c1)
3. Given two integer number X and Y, your task is to write a program to find the greatest
common divisor of X and Y using recursion.
Input: Input come from keyboard containing two number X and Y
Output: Ouput are written to screen contaning the greatest common divisor. lOMoAR cPSD| 59735516 Example: Keyboard Screen 10 10 50
4. Given an integer number n, your task is to list all binary number of length n.
Input: The number n comes from the Keyboard
Output: Binary numbers of length n are written to the screen each in one line. Example: Keyboard Scree n 3 00 0 00 1 01 0 01 1 10 0 10 1 11 0 11 1
Tips: Read and understand the pseudo code of the excercise presented at the lecture 7.
5. Given an integer number n, your task is to list all permutations of length n.
Input: The number n comes from the Keyboard
Output: Permutations of length n are written to the screen each in one line. Example: Keyboard Screen 123 132 213 231 312 321 3
Tips: a permuta 琀椀 on of n elements can be generated by a permuta 琀椀 on of n-1 elements associa
琀椀 ng to a number from 1 to n n . Therefore, this exercise and the fourth exercise can be solved the same method.