Programming contests

20 Programming Exercise for Beginners

January 1, 2019 12:00 AM – December 31, 2019 12:00 AM

Evaluation of a Compound Expression

Create a console application that evaluates the following expression.

The input data is read from the standard input and consist of 5 integers, each between -100 and +100.

Input Specification

The input consists of an integer vector with five elements. All elements of the vector appear in a single line, which is terminated by a newline character.

Sample Input

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
download as text file

Output Specification

The output consists of a single line containing the result of the evaluation terminated by a newline character.

Output for Sample Input

  1. 225
download as text file

Hints and Guide

Based on the input definition, an integer (int type) variable is sufficient to store the results of the calculations.

A standard method is to convert the textual input data to integer with the help of the scanf function declared in the standard input-output library.

The easiest way to implement the sums is to create an iteration. To do so we recommend to use array to represent the vector. Do not forget, that the arrays in C, as in many other programming languages, are indexed from 0. If we wish to keep the original indexing model, the easiest way is to declare an array with 6 member so we can use indexes from 1 up to 5. (Note that the first member of the array indexed by 0 exists but useless.)

The evaluation seems to be simple, but it requires double iteration. As in the expression the iterations require they own variables. e.g., i and j. To store the internal results it is also necessary to declare one another integer variable.

The output can be done by using the printf function. With the help of this function it is easy to convert the integers to decimal form.

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019