Exercise 3
Added Exercise 3 with solutions and README to Repository
This commit is contained in:
23
Informatik_I/Exercise_3/Task_2/README.md
Normal file
23
Informatik_I/Exercise_3/Task_2/README.md
Normal file
@@ -0,0 +1,23 @@
|
||||
_This task is a mixed text/programming task. You need to update the content of loop.cpp according to the instruction provided in the file. For the code part, please check the autograder output._
|
||||
|
||||
# Task
|
||||
|
||||
Considering the snippet
|
||||
|
||||
```cpp
|
||||
for (;;) {
|
||||
int i1, i2;
|
||||
std::cin >> i1 >> i2;
|
||||
std::cout << i1 + i2 << "\n";
|
||||
int again;
|
||||
std::cout << "Again? (0/1)\n";
|
||||
std::cin >> again;
|
||||
if (again == 0) break;
|
||||
}
|
||||
```
|
||||
|
||||
1. Describe what it computes.
|
||||
|
||||
1. Decide which of the other two kind of loops would fit better than the one it is currently using, and describe why.
|
||||
|
||||
1. Rewrite the snippet into the loop you specified in (2). This part is autograded. Note: print the control message "Again? (0/1)" using the same format used in the snippet.
|
48
Informatik_I/Exercise_3/Task_2/loop.cpp
Normal file
48
Informatik_I/Exercise_3/Task_2/loop.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "loop.h"
|
||||
#include <iostream>
|
||||
|
||||
// Fill out the file with the required answers
|
||||
|
||||
/*
|
||||
|
||||
Subtask 1: describe what the code snippet computes
|
||||
|
||||
It is a very simple calculator which adds up the 2 number which are inputed by
|
||||
the user.
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
Subtask 2: decide which of the other two kind of loops would fit better, and
|
||||
describe why.
|
||||
|
||||
|
||||
A while loop would be sufficient for the task of this code snippet, since we
|
||||
only have to check, if the user wants to use the calculator again or not.
|
||||
|
||||
Of course, as in the last task we could look at the edge case. We could also
|
||||
add an if expression to elimnate the posibility to input other numbers other
|
||||
than 0 and 1 for the again variable.
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
Subtask 3: update the function below by rewriting the snippet into the
|
||||
loop you specified in (2).
|
||||
Note: print the control message "Again? (0/1)" using the same format used
|
||||
in the snippet.
|
||||
*/
|
||||
|
||||
void loop() {
|
||||
|
||||
int again = 1;
|
||||
|
||||
while (again != 0) {
|
||||
int i1, i2;
|
||||
std::cin >> i1 >> i2;
|
||||
std::cout << i1 + i2 << "\n";
|
||||
std::cout << "Again? (0/1)\n";
|
||||
std::cin >> again;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user