Skip to content

prog-1/test-2-2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

Mid-term test 2 (variant 2)

Fork the repository, add/edit files in the directories in order to complete the test. The completed test must be submitted as a GitHub pull request to prog-1/test-2-2 repository.

The test consists of four tasks (see below).

1. Code comprehension

Explain erlaeuterung program and complete the tasks. You may execute and modify the program.

The program contains special comments:

  • QUESTION N: requires an answer provided in the following line prefixed with ANSWER N:. E.g.

    // QUESTION 1: What is []int?
    // ANSWER 1: Creates a variable named `x` of type slice of ints.
    var x []int
  • TASK N: requires an action described. In the following example we expect you to rename f1 into something like printSlice - a function which has a better name:

    // TASK 1: Provide a better name for the function.
    func f1(s [][]int) {
       for _, row := range s {
          for _, v := range row {
             fmt.Printf("%3d", v)
          }
          fmt.Println()
       }
    }

2. Progression (coding task)

Write a program that finds the nth term (n is a non-negative integer) of the progression

  1. Create the program in the directory progression.
  2. Implement the logic in a function declared as func nthTerm(n uint) int.
  3. Implement main() function that
    1. reads n from the keyboard
    2. calls nthTerm function
    3. prints a value returned from the function.
  4. Create a file with tests for the program. The tests must cover all representative inputs.

Example 1

Enter n: 1
The nth term is 2.

Explanation:

nthTerm(1) must return 2, because .

Example 2

Enter n: 4
The nth term is 38.

nthTerm(4) must return 38, because

3. Filter (coding task)

Write a program that removes all integers divisible by 5 from a slice of integers.

  1. Write the program in the directory filter.
  2. Implement the logic in a function declared as func filter(s []int) []int.
  3. Implement main() function that
    1. reads a slice from the keyboard (a user provides the number of elements in a slice and the element values)
    2. calls filter function
    3. prints the filtered slice.
  4. Create a file with tests for the program. The tests must cover all representative inputs.

Example 1

Enter the number of elements in a slice: 2
Enter the elements: 10 -15
The filtered slice: []

filter([]int{-10, -15}) returns []int{}, because all integer in the slice are divisible by 5, and are removed from the slice.

Example 2

Enter the number of elements in a slice: 5
Enter the elements: -1 0 -5 15 4
The filtered slice: [-1 4]

filter([]int{-1, 0, -5, 15, 4}) returns []int{-1, 4}. 0, -5 and 15 are integers divisible by 5 and are removed from the slice.

4. Generation (coding task)

Write a program that generates a matrix of values such as:

  • the last row is filled with 0 except for the last element, which is 1;
  • any other element of the table is the sum of all elements below it (in the same column) and to the right (in the same row).
  1. Write the program in the directory matrix.
  2. Implement the logic in a function declared as func gen(width, height int) [][]int.
  3. Implement main() function that reads width and height from the keyboard and prints the generated matrix.
  4. Create a file with tests for the program. The tests must cover all representative inputs.

Example

Enter width and height: 5 4
Result:
 130  53  21   8   4
  36  16   7   3   2
   8   4   2   1   1
   0   0   0   0   1

Explanation for row=0, col=2: 21 = (vertical) 7+2+0 + (horizontal) 8+4

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Languages