[Exercise 5] [Content] [Exercise 7]

Exercise 6


Task 6.1

Create a program that reads size of arithmetical vectors n, then it allocates dynamically three arrays of the size n. The program reads two arithmetical vectors from the standard input and store them into arrays. It computes the sum of these two vectors, stores the sum into the third vector and prints all vectors.

Run the program to inspire: sum_of_vect.exe

Solution:

CodeBlocks:sum_of_vect_dyn.cbp, sum_of_vect_dyn.c


Task 6.2

Create a program which reads a sequence of integers from the standard input terminated by -1. The count of values is not known in advance. Store values into dynamically allocated array; if the size of the array is not sufficient, reallocate the memory.

Solution:

CodeBlocks:dyn_array2a.cbp, dyn_array2a.c
CodeBlocks:dyn_array2b.cbp, dyn_array2b.c
CodeBlocks:dyn_array3.cbp, dyn_array3.c


Task 6.3

Finish the following program. The program reads text (max. 80 character) from the standard input. Then, one character is entered. Find this character in the text. If it exists, print character in the previous position and all characters following the searched one cipher by Cesar cypher (move three positions in the alphabet). Use pointers and pointer arithmetic.

CodeBlocks:text.cbp, text.c

Solution:

CodeBlocks:text.cbp, text.c


Task 6.4

Create a program which allocates dynamically two-dimensional array to store lower triangular matrix. The input data is n, where n is the cout of rows. The size for rows allocate exactly. The fill matrix according the picture and print the matrix. Don't forget to deallocate.

Solution:

CodeBlocks:triang_matrix.cbp, triang_matrix.c


Homework

Create a program that prints all prime numbers from 2 to n (n is read from the keyboard). Use Sieve of Erathostenes algorithm. Allocate the array dynamically.

Solution using static array:
CodeBlocks:eratos.cbp, eratos.c
Solution:
CodeBlocks:eratos.cbp, eratos.c

Task 6.5

Complete following program - define function that calculates area of the circle and call it in main.

CodeBlocks:area_example.cbp, area_example.c

Solution:

CodeBlocks:area.cbp, area.c


Task 6.6

There are defined two procedures imaxmin, fmaxmin searching maximum and minimum. Overload procedures and replace pointer parameters by reference parameters.

CodeBlocks:overloadingp.cbp, overloadingp.cpp

Solution:

CodeBlocks:overloading.cbp, overloading.cpp


Task 6.7

Create recursive and non-recursive (using loop) functions to compute the n-th number of Fibonacci sequence.

F0 = 0
F1 = 1
Fn = Fn-1 + Fn-2

Solution:

CodeBlocks:fibo.cbp, fibo.c


Task 6.8

The sum of money is given. We have set of coins of values: 20, 10, 5, 2, 1. Create recursive procedure change_money that prints how to change the sum with the minimum count of coins. Then try to create a non-recursive version.

Example:

Sum: 57
Coins: 20 20 10 5 2
Solution:
CodeBlocks:coins.cbp, coins.c


[Exercise 5] [Content] [Exercise 7]