[Exercise 4] | [Content] | [Exercise 6] |
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
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.Help: Define the symbol (macro) ENGLISH by preprocessor directive
CodeBlocks: lcm.cbp, lcm.c #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"); #endifTry 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
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 conditiondo { } while(cond)
.Note: If data is entered incorrectly, unread characters stay stored in the buffer of the standard input (keyboard). Thescanf
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
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 0Example of output:
Help: the skeleton of the code:
The total count of numbers: 5 The count of numbers between 3 and 10: 2
The minimum value: -1int x; scanf("%f",&x); while(x != 0) { ... scanf("%f",&x); }Solution:
CodeBlocks: sequence.cbp, sequence.c
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:
Solution:
CodeBlocks: even.cbp, even.c
CodeBlocks: even.cbp, even.c
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 ... EndStore sizes of sides in two variables. Number of chosen item from the menu store in variable ofint
type and read it byscanf
, handle the menu usingswitch
.Prepared skeleton:
Solution:
CodeBlocks: rectangle.cbp, rectangle.c
CodeBlocks: rectangle.cbp, rectangle.c
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 overcin
stream.cin >> a; if (cin.fail()) { cerr << "Error message"; return -1; }Solution:
CodeBlocks: lcm3.cbp, lcm3.cpp
[Exercise 4] | [Content] | [Exercise 6] |