[Exercise 4] [Content] [Exercise 6]

Exercise 5


Task 5.1

Create a program, which computes the area and circumference of rectangle. The program writes a message with request to enter lengths of two sides of the rectangle and then, it writes results. Try debugging - set breakpoint, view watch window and execute the program step by step.

Solution:

CodeBlocks:area.cbp, area.c


Task 5.2

Modify following example lcm.c which calculates the least common multiple. Create two language versions - English version and your mother tongue's one. Use conditional assembly and suitable directives of preprocessor.
CodeBlocks:lcm.cbp, lcm.c
Help: Define the symbol (macro) ENGLISH by preprocessor directive #define ENGLISH and use conditional assembly for example by this way:
#ifdef ENGLISH
printf("The least common multiple of two integers\n");
#else
printf("Your mother tongue message\n");
#endif

Try run only preprocessor with compiler option -E, i.e. run compiler directly from shell: gcc lcm.c -E. The source code after preprocessing is put on the screen. If you want to redirect standard output to the file, write: gcc lcm.c -E > preprocessed_code.c. The output is stored in the file preprocessed_code.c.

Solution:

Variant 1:
 CodeBlocks:lcm1.cbp, lcm1.c
Variant 2:
 CodeBlocks:lcm2.cbp, lcm2.c


Task 5.3

Modify the program calculating area and circumference in the task 5.1 to repeat entering data if it is put incorrectly (solve it by adding the loop with condition do { } while(cond).
Note: If data is entered incorrectly, unread characters stay stored in the buffer of the standard input (keyboard). The scanf function reads these characters ones more in the next execution of the loop and returns error. The loop becomes neverending. The buffer must be cleared in this case. Because of the last character is ('\n'), the buffer can be cleared by this loop: while(getchar()!='\n');. The semicolon is there instead of command (the body of the loop is empty, the semicolon is empty command).

Solution:

CodeBlocks:area2.cbp, area2.c

Task 5.4

Create a program, which reads a sequence of integers separated by white character (space, tabulator or Enter). The sequence is terminated by zero. The program calculates how many numbers was entered, how many numbers was in interval <3;10> and find the minimum. Terminated zero is not taken as the part of the sequence.

Example of input:
2 -1 3 5 20 0

Example of output:
The total count of numbers: 5
The count of numbers between 3 and 10: 2
The minimum value: -1

Help: the skeleton of the code:
int x;
scanf("%f",&x);
while(x != 0)
{
  ...
  scanf("%f",&x);  
}

Solution:

CodeBlocks:sequence.cbp, sequence.c

Task 5.5

Create a program which reads two integers a and b. It checks if a is less than b; if not, it swaps these values. The program writes all even numbers between a and b including a and b, if they are even.

Help: Use for loop. Initialize the loop variable by a if it is even else by a+1 - use conditional expression.

Prepared skeleton:

CodeBlocks:even.cbp, even.c
Solution:
CodeBlocks:even.cbp, even.c

Task 5.6

Create a program that calculates area and circumference of rectangle. The program shall be interactive with simple menu:
1 ... Entering sides
2 ... Area
3 ... Circumference
4 ... End
Store sizes of sides in two variables. Number of chosen item from the menu store in variable of int type and read it by scanf, handle the menu using switch.

Prepared skeleton:

CodeBlocks:rectangle.cbp, rectangle.c
Solution:
CodeBlocks:rectangle.cbp, rectangle.c

Task 5.7

Modify code which computes the least common multiple. Use streams instead of printf/scanf functions.

CodeBlocks:lcm3.cbp, lcm3.cpp

Help: Test if input data are entered correctly by calling fail the method over cin stream.

cin >> a;
if (cin.fail())
{
  cerr << "Error message";
  return -1;
}

Solution:

CodeBlocks:lcm3.cbp, lcm3.cpp


[Exercise 4] [Content] [Exercise 6]