Exercise 6
Added Exercise 6
This commit is contained in:
parent
88e0b5ed69
commit
bfc1a7a2b0
140
Informatik_I/Exercise_6/Task_1/README.org
Normal file
140
Informatik_I/Exercise_6/Task_1/README.org
Normal file
@ -0,0 +1,140 @@
|
||||
#+TITLE: Task 1: Const and reference types
|
||||
#+AUTHOR: JirR02
|
||||
|
||||
/This task is a text based task. You do not need to write any program/C++ file: the answer should be written in main.md (and might include code fragments if questions ask for them)./
|
||||
|
||||
*Note:* You can safely ignore any undefined behavior due to integer overflows.
|
||||
|
||||
* Task
|
||||
|
||||
Consider the following functions:
|
||||
|
||||
1.
|
||||
|
||||
#+begin_src cpp
|
||||
int foo(int i) {
|
||||
return ++i;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
2.
|
||||
|
||||
#+begin_src cpp
|
||||
int foo(const int i) {
|
||||
return ++i;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
3.
|
||||
|
||||
#+begin_src cpp
|
||||
int foo(int& i) {
|
||||
return ++i;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
4.
|
||||
|
||||
#+begin_src cpp
|
||||
int& foo(int i) {
|
||||
return ++i;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
5.
|
||||
|
||||
#+begin_src cpp
|
||||
const int& foo(int& i) {
|
||||
return ++i;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
For each function, answer the following questions:
|
||||
|
||||
a) Does the function attempt to modify const variables? If so, give the reason why (max. 15 words).
|
||||
|
||||
If no, answer the following (otherwise put N/A):
|
||||
|
||||
b) Variable scopes: are the output and side effects of the function well-defined, i.e., free of undefined behavior? If not, give the reason why (max. 15 words).
|
||||
|
||||
c) If the answer in b) is yes, give the precise post condition for the corresponding function foo. In particular, indicate what side effects the function may have.
|
||||
|
||||
* Solution
|
||||
|
||||
#+begin_src md
|
||||
1.
|
||||
|
||||
```c++
|
||||
int foo(int i) {
|
||||
return ++i;
|
||||
}
|
||||
```
|
||||
|
||||
a) Yes and no, if depends if the input is assigend to a constant or not but generally no.
|
||||
|
||||
b) Yes
|
||||
|
||||
c) `foo` returns the increment by 1 of `i`. The side effect of this function is, that the variable `i` is only accessible inside the function `foo`. To make `i` accessible, one has to use the function as follows: `int b = foo(i)`
|
||||
|
||||
|
||||
2.
|
||||
```c++
|
||||
int foo(const int i) {
|
||||
return ++i;
|
||||
}
|
||||
```
|
||||
|
||||
a) Yes, since `i` is defined as a constant variable the function foo increments `i`.
|
||||
|
||||
b) N/A
|
||||
|
||||
c) N/A
|
||||
|
||||
|
||||
3.
|
||||
|
||||
```c++
|
||||
int foo(int& i) {
|
||||
return ++i;
|
||||
}
|
||||
```
|
||||
|
||||
a) Yes and no, it depends if the reference is assigned to a constant variable or not but generally no.
|
||||
|
||||
b) Yes
|
||||
|
||||
c) The variable `i` which is referenced to another variable outside of the function will be incremented. Since it's referenced the variable is not only accessible within the function.
|
||||
|
||||
|
||||
4.
|
||||
|
||||
```c++
|
||||
int& foo(int i) {
|
||||
return ++i;
|
||||
}
|
||||
```
|
||||
|
||||
a) Yes and no, if depends if the input is assigend to a constant or not but generally no.
|
||||
|
||||
b) Yes
|
||||
|
||||
c) Since the whole function is a referenced, the variable which is inputted into the function will not be incremented. Instead the variable `i` will only be in scope of the function `foo` so as in task two, to use the function one has to use it as follows: `int b = foo(i);`
|
||||
|
||||
5.
|
||||
|
||||
```c++
|
||||
const int& foo(int& i) {
|
||||
return ++i;
|
||||
}
|
||||
```
|
||||
|
||||
a) Yes and no, if depends if the input is assigend to a constant or not but generally no.
|
||||
|
||||
b) Yes
|
||||
|
||||
c) This function will increment the input and return it as a constant.
|
||||
#+end_src
|
||||
|
||||
-----
|
||||
|
||||
Made by JirR02 in Switzerland 🇨🇭
|
100
Informatik_I/Exercise_6/Task_2/README.org
Normal file
100
Informatik_I/Exercise_6/Task_2/README.org
Normal file
@ -0,0 +1,100 @@
|
||||
#+TITLE: Task 2: Number of Occurrences
|
||||
#+AUTHOR: JirR02
|
||||
|
||||
* Task
|
||||
|
||||
Write a program using a vector to perform the following:
|
||||
|
||||
1. Read integer values from standard input and insert them in a vector until a negative integer is encountered;
|
||||
1. Read an extra non-negative integer n;
|
||||
1. Output the number of occurrences of n encountered during phase (1).
|
||||
|
||||
Your solution *must* split the sub-tasks of the program (reading the vector, counting the occurrences, and maybe other sub-tasks) into separate functions. Those functions *must* use the proper reference parameters to access the vector.
|
||||
|
||||
*Note:* we consider solutions as incorrect if they use library functions (or data structures) beyond the ones discussed in the lectures so far to perform the actual computation.
|
||||
|
||||
* Input
|
||||
|
||||
A sequence of of integer values containing a single negative integer, in penultimate position.
|
||||
|
||||
Example:
|
||||
|
||||
#+begin_src shell
|
||||
0 1 1 2 3 5 8 13 21 -1 1
|
||||
#+end_src
|
||||
|
||||
* Output
|
||||
|
||||
The number of occurrences of the last integer in the sequence before the negative integer.
|
||||
|
||||
Example: for the input given above, the number of occurrences of 1 before -1 occurs is
|
||||
|
||||
#+begin_src shell
|
||||
2
|
||||
#+end_src
|
||||
|
||||
* Reminder - How to use vector
|
||||
|
||||
To define a vector (for example of integer numbers), you can use the following constructor
|
||||
|
||||
#+begin_src cpp
|
||||
std::vector<int> my vec = std::vector<int>(length, init_value);
|
||||
#+end_src
|
||||
|
||||
You can insert element at the back of the vector with:
|
||||
|
||||
#+begin_src cpp
|
||||
my_vec.push_back(number);
|
||||
#+end_src
|
||||
|
||||
To access the elements of the vector you have to use the random access ([]) operator:
|
||||
|
||||
#+begin_src cpp
|
||||
std::cout << "First element: " << my_vec[0];
|
||||
#+end_src
|
||||
|
||||
* Solution
|
||||
|
||||
#+begin_src cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void readvec(std::vector<int>& vec, int& num) {
|
||||
vec.push_back(num);
|
||||
}
|
||||
|
||||
int occvec(std::vector<int>& vec, int& num, int& count) {
|
||||
for (unsigned long i = 0; i < vec.size(); ++i) {
|
||||
if (vec[i] == num)
|
||||
++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
int i;
|
||||
int con;
|
||||
int count = 0;
|
||||
std::vector<int> vec;
|
||||
|
||||
std::cin >> i;
|
||||
|
||||
while (i >= 0) {
|
||||
readvec(vec, i);
|
||||
std::cin >> i;
|
||||
}
|
||||
|
||||
std::cin >> con;
|
||||
|
||||
occvec(vec, con, count);
|
||||
|
||||
std::cout << count;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
-----
|
||||
|
||||
Made by JirR02 in Switzerland 🇨🇭
|
100
Informatik_I/Exercise_6/Task_3/README.org
Normal file
100
Informatik_I/Exercise_6/Task_3/README.org
Normal file
@ -0,0 +1,100 @@
|
||||
#+TITLE: Task 3: [Hidden tests] Sorting by Swapping
|
||||
#+AUTHOR: JirR02
|
||||
|
||||
* Task
|
||||
|
||||
Write a program that performs the following:
|
||||
|
||||
1. Read a non-negative integer
|
||||
2. Read \(n\) integers
|
||||
3. Sort the \(n\) integers read in (2) in decreasing order. To that end, we suggest the following method:
|
||||
a. Store the \(n\) integers in a vector
|
||||
b. Iterate through the vector, compare each pair of adjacent values and swap them if they are in the wrong order (i.e increasing order)
|
||||
c. Repeat step (2) as long as swap are performed.
|
||||
4. Output the sorted values.
|
||||
|
||||
Your solution *must* isolate the sorting functionality of the program into a separate function, using a reference parameter with the right constness to access the vector. Other sub-tasks of the program may be isolated as well.
|
||||
|
||||
*Note:* We consider solutions as incorrect if they use library functions (or data structures) beyond the ones discussed in the lectures so far to perform the actual computation. In particular, library sorting functions are forbidden as they defeat the purpose of the exercise.
|
||||
|
||||
*Note:* The function you implement *must* use pass by reference.
|
||||
|
||||
* Input
|
||||
|
||||
A sequence of integers, preceded by its length.
|
||||
|
||||
Example:
|
||||
|
||||
#+begin_src shell
|
||||
7 3 8 6 3 4 6 5
|
||||
#+end_src
|
||||
|
||||
* Output
|
||||
|
||||
The input sequence, minus the length, in decreasing order. The integers are separated by a single space.
|
||||
|
||||
#+begin_src shell
|
||||
8 6 6 5 4 3 3
|
||||
#+end_src
|
||||
* Hidden test cases
|
||||
|
||||
This exercise has some hidden test cases. You will see whether these tests passed or failed, but you will not see the test input or the expected output.
|
||||
|
||||
* Persistent input
|
||||
|
||||
To help you in testing your code, you can enter your custom test input in input.txt. When running the program, you can let CodeExpert read the input from the file.
|
||||
|
||||
Write some program input in input.txt. Run the program as usual. Type f (no spaces) in the terminal window and press Enter. The program will use the contents of input.txt as input. Using this feature is entirely optional and does not affect your score.
|
||||
|
||||
* Solution
|
||||
|
||||
#+begin_src cpp
|
||||
#include <iostream>
|
||||
// Enable vectors
|
||||
#include <vector>
|
||||
|
||||
void readvec(std::vector<int>& vec, int& length) {
|
||||
for (int j = 0; j < length; ++j) {
|
||||
int i;
|
||||
std::cin >> i;
|
||||
vec[j] = i;
|
||||
}
|
||||
}
|
||||
|
||||
void sortvec(std::vector<int>& vec, int& length) {
|
||||
for (int j = length - 1; j > 0; --j) {
|
||||
for (int i = 0; i < j; ++i) {
|
||||
if (vec[i] < vec[i+1]) {
|
||||
int prev = vec[i+1];
|
||||
int next = vec[i];
|
||||
vec[i] = prev;
|
||||
vec[i+1] = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printvec(std::vector<int>& vec, int& length) {
|
||||
for (int j = 0; j < length; ++j)
|
||||
std::cout << vec[j] << " ";
|
||||
}
|
||||
|
||||
int main() {
|
||||
int length;
|
||||
std::cin >> length;
|
||||
|
||||
std::vector<int> values(length);
|
||||
|
||||
readvec(values, length);
|
||||
|
||||
sortvec(values, length);
|
||||
|
||||
printvec(values, length);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
-----
|
||||
|
||||
Made by JirR02 in Switzerland 🇨🇭
|
279
Informatik_I/Exercise_6/Task_4/README.org
Normal file
279
Informatik_I/Exercise_6/Task_4/README.org
Normal file
@ -0,0 +1,279 @@
|
||||
#+TITLE: Task 4: Vector and matrix operations
|
||||
#+AUTHOR: JirR02
|
||||
|
||||
*Note:* All occurrences of the word /vector/ in this assignment without an explicit =std::= prefix mean vector in the mathematical sense.
|
||||
|
||||
*Task:*
|
||||
|
||||
Write a program that implements the multiplication of integer vectors and matrices using =std::vectors=. The program must support the following three possible operations:
|
||||
|
||||
1. Dot product of two vectors (also called scalar product or inner product)
|
||||
1. Matrix-vector product (application of a matrix to a column vector)
|
||||
1. Matrix product
|
||||
|
||||
Implement the following program structure:
|
||||
|
||||
1. Query for the two operands from the input: the left operand first, then the right operand.
|
||||
1. If none of the three operators described above can be applied, output error then end the program.
|
||||
1. If the two operands have mismatched sizes (e.g. the dot product of a 2D vector and a 3D vector), output error then end the program.
|
||||
1. Output the result of the operation then end the program.
|
||||
|
||||
Note: We already provide a partial implementation of this program in =main.cpp=.
|
||||
|
||||
*Recommendations:*
|
||||
|
||||
1. We provide functions to read and write matrices, vectors, and scalars in io.h. Use them!
|
||||
1. Use one dimensional =std::vectors= to represent vectors and two dimensional =std::vectors= to represent matrices.
|
||||
1. Use functions to implement the three different operations. Use references with proper constness to access =std::vectors= from functions.
|
||||
1. You may assume that all input vector/matrices have non-zero dimensions, and that input will not contain values big enough to cause overflow.
|
||||
|
||||
*Format:*
|
||||
|
||||
The format to represent scalars/vectors/matrices in input/output is the following:
|
||||
|
||||
1. A character among s, v and m to denote whether the data represent a scalar, a vector or a matrix (scalar cannot occur in input for this task).
|
||||
2. A sequence of integers giving the dimensions of the value:
|
||||
a. Nothing for scalar
|
||||
b. A single integer giving the length for vectors
|
||||
c. Two integers giving respectively the number of rows and the number of columns for matrices
|
||||
3. A new line
|
||||
4. A sequence of integers giving the content of the value:
|
||||
a. The scalar itself for scalars
|
||||
b. The content of the vector in order for vectors, on a single line
|
||||
c. The content of the matrix in row-major order for matrices (the content of the first row in column order first, then the second row, etc). Each row should be placed on a distinct line.
|
||||
|
||||
Note: Check =io.h= and =io.cpp=. The functions provided there can handle a large part of the input/output format.
|
||||
|
||||
* Examples
|
||||
|
||||
*Dot product of two 3D vectors*
|
||||
|
||||
#+begin_src shell
|
||||
Left operand
|
||||
v 3
|
||||
1 2 3
|
||||
Right operand
|
||||
v 3
|
||||
3 2 1
|
||||
s
|
||||
10
|
||||
#+end_src
|
||||
|
||||
*Multiply 3x2 matrix by 2D vector*
|
||||
|
||||
#+begin_src shell
|
||||
Left operand
|
||||
m 3 2
|
||||
1 2
|
||||
3 4
|
||||
5 6
|
||||
Right operand
|
||||
v 2
|
||||
2 1
|
||||
v 3
|
||||
4 10 16
|
||||
#+end_src
|
||||
|
||||
*Multiply 2x4 matrix 4x3 matrix*
|
||||
|
||||
#+begin_src shell
|
||||
Left operand
|
||||
m 2 4
|
||||
1 2 3 4
|
||||
5 6 7 8
|
||||
Right operand
|
||||
m 4 3
|
||||
12 11 10
|
||||
9 8 7
|
||||
6 5 4
|
||||
3 2 1
|
||||
m 2 3
|
||||
60 50 40
|
||||
180 154 128
|
||||
#+end_src
|
||||
|
||||
*Multiply 2x3 matrix by 4D vector*
|
||||
|
||||
#+begin_src shell
|
||||
Left operand
|
||||
m 2 3
|
||||
1 2 3
|
||||
4 5 6
|
||||
Right operand
|
||||
v 4
|
||||
4 3 2 1
|
||||
error
|
||||
#+end_src
|
||||
|
||||
*Note:* The scalar values should be returned only by scalar products. Even if a matrix-vector multiplication or a matrix-matrix multiplication results in a single value, it should be represented by a single-element vector or matrix. Similarly, if the result of a matrix-matrix multiplication looks like a vector, it should still be represented as a matrix, with one dimension equal to 1.
|
||||
|
||||
* Solution
|
||||
|
||||
#+begin_src cpp
|
||||
#include <iostream>
|
||||
// Enable std::vectors
|
||||
#include <vector>
|
||||
|
||||
#include "io.h"
|
||||
|
||||
using vec = std::vector<int>;
|
||||
using mat = std::vector<vec>;
|
||||
|
||||
// POST: result of dot product of v1 and v2 is printed to standard output,
|
||||
// or "error" if dimensions are not compatible
|
||||
|
||||
void vector_scalar_product(const vec& v, const int& s) {
|
||||
vec res;
|
||||
res = v;
|
||||
for (unsigned long j = 0; j < v.size(); ++j)
|
||||
res[j] = v[j] * s;
|
||||
print_vector(res);
|
||||
}
|
||||
|
||||
void matrix_scalar_product(const mat& m, const int& s) {
|
||||
mat res;
|
||||
res = m;
|
||||
for (unsigned long j = 0; j < m.size(); ++j) {
|
||||
for (unsigned long i = 0; i < m[0].size(); ++i)
|
||||
res[j][i] = m[j][i] * s;
|
||||
}
|
||||
print_matrix(res);
|
||||
}
|
||||
|
||||
void dot_product(const vec& v1, const vec& v2) {
|
||||
int res = 0;
|
||||
if (v1.size() == v2.size()) {
|
||||
for (unsigned long j = 0; j < v1.size(); ++j)
|
||||
res += (v1[j] * v2[j]);
|
||||
print_scalar(res);
|
||||
} else {
|
||||
std::cout << "error";
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_vector_product(const mat& m, const vec& v) {
|
||||
vec res;
|
||||
if (v.size() == m[0].size()) {
|
||||
for (unsigned long j = 0; j < m.size(); ++j) {
|
||||
int k = 0;
|
||||
for (unsigned long i = 0; i < m[0].size(); ++i) {
|
||||
k += m[j][i] * v[i];
|
||||
}
|
||||
res.push_back(k);
|
||||
}
|
||||
print_vector(res);
|
||||
} else {
|
||||
std::cout << "error";
|
||||
}
|
||||
}
|
||||
|
||||
void matrix_product(const mat& m1, const mat& m2) {
|
||||
mat res(m1.size(), vec(m2[0].size()));
|
||||
if (m1[0].size() == m2.size()) {
|
||||
for (unsigned long i = 0; i < m1.size(); ++i) {
|
||||
for (unsigned long j = 0; j < m2.size(); ++j) {
|
||||
for (unsigned long k = 0; k < m2[0].size(); ++k) {
|
||||
int l = m1[i][j] * m2[j][k];
|
||||
res[i][k] += l;
|
||||
}
|
||||
}
|
||||
}
|
||||
print_matrix(res);
|
||||
}
|
||||
else {
|
||||
std::cout << "error";
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "Left operand" << std::endl;
|
||||
char left_type;
|
||||
std::cin >> left_type;
|
||||
|
||||
if (left_type == 'v') {
|
||||
// left operand is vector
|
||||
|
||||
// get vector from input
|
||||
vec left_operand;
|
||||
read_vector(left_operand);
|
||||
|
||||
// get right operand type
|
||||
std::cout << "Right operand" << std::endl;
|
||||
char right_type;
|
||||
std::cin >> right_type;
|
||||
|
||||
if (right_type == 's') {
|
||||
// right operand is a scalar => compute scalar product
|
||||
|
||||
// get scalar from input
|
||||
int s;
|
||||
std::cin >> s;
|
||||
|
||||
// compute vector scalar product
|
||||
vector_scalar_product(left_operand, s);
|
||||
} else if (right_type == 'v') {
|
||||
// right operand is a vector => compute dot product
|
||||
|
||||
// get vector from input
|
||||
vec right_operand;
|
||||
read_vector(right_operand);
|
||||
|
||||
// compute dot product
|
||||
dot_product(left_operand, right_operand);
|
||||
} else {
|
||||
std::cout << "error";
|
||||
}
|
||||
|
||||
} else if (left_type == 'm') {
|
||||
// left operand is matrix
|
||||
|
||||
// get matrix from input
|
||||
mat left_operand;
|
||||
read_matrix(left_operand);
|
||||
|
||||
// get right operand type
|
||||
std::cout << "Right operand" << std::endl;
|
||||
char right_type;
|
||||
std::cin >> right_type;
|
||||
|
||||
if (right_type == 's') {
|
||||
// right operand is a scalar => compute scalar product
|
||||
|
||||
// get scalar from input
|
||||
int s;
|
||||
std::cin >> s;
|
||||
|
||||
// compute matrix scalar product
|
||||
matrix_scalar_product(left_operand, s);
|
||||
} else if (right_type == 'v') {
|
||||
// right operand is a vector => compute dot product
|
||||
|
||||
// get vector from input
|
||||
vec right_operand;
|
||||
read_vector(right_operand);
|
||||
|
||||
// compute matrix vector product
|
||||
matrix_vector_product(left_operand, right_operand);
|
||||
} else if (right_type == 'm') {
|
||||
// right operand is a matrix => compute matrix vector product
|
||||
|
||||
// get matrix from input
|
||||
mat right_operand;
|
||||
read_matrix(right_operand);
|
||||
|
||||
// compute matrix product
|
||||
matrix_product(left_operand, right_operand);
|
||||
} else {
|
||||
std::cout << "error";
|
||||
}
|
||||
} else {
|
||||
std::cout << "error";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
-----
|
||||
|
||||
Made by JirR02 in Switzerland 🇨🇭
|
Loading…
x
Reference in New Issue
Block a user