Gyakorlatok nyomonkövetéssel

Mit írnak ki az alábbi programok?

a.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int f(int x, int y) {
    x++;
    return x+y;
}


int main()
{
    int a=10, b=20;
    cout << f(a,b) << endl;  // 31
    cout << a << ' ' << b << endl;  // 10 20
    return 0;
}

b.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int f(int x) {
    return 3*x + 2;
}


int main()
{
    int a = 10;
    cout << f(a) << endl; // 32
    cout << f(1+a) << endl; // 35
    cout << f(f(a)) << endl; // 98

    return 0;
}

c.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int g(int a, int &b) {
    a++;
    b++;
    return a+b;
}


int main()
{
    int a = 10, b = 20;
    cout << g(a, b) << endl; // 32
    cout << a << " " << b << endl; // 10 21

    return 0;
}

d.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

void f(int a[], int n) {

    for (int i = 0; i < n; i++)
        t[i] *= 2;

    for (int i = 0; i < n; i++)
        cout << t[i] << " ";
    cout << endl;
}


int main()
{
    int t[5] = {1,2,3,4,5};

    f(t, 5);

    for (int i = 0; i < n; i++)
        cout << t[i] << " ";
    cout << endl;

    return 0;
}

e.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int f(int x, int &y, int &z)
{
    x++;
    y++;
    z++;
    return x+y+z;
}

int main()
{
    int a = 1, b = 2, c = 3;
    cout << f(a,b,c) << endl; // 9
    cout << a << " " << b << " " << c << endl; // 1 3 4
    return 0;
}

f.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int f(int x, int &y, int &z)
{
    x++;
    y++;
    z++;
    return x+y+z;
}

int main()
{
    int a = 1;
    cout << f(a,a,a) << endl; // 8
    cout << a << endl; // 3
    return 0;
}

g.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void g(int &x, int &y) { x++; y++; }

void f(int a, int &b)
{
    int c = 10;
    g(a,b);
    g(c,a);
    g(b,c);
    cout << a << " " << b << " " << c << endl;
    return;
    cout << "teszt" << endl;
}

int main()
{
    int i = 5, j = 6;
    f(i, j);
    f(j, i);
    f(i, j);
    return 0;
}

Megoldás (g.):

KIMENET:
    7 8 12
    10 7 12
    9 10 12

main()
|        i: 5  6  7
|        j: 6  7  8 9 10
|
|    f(...)
|    |        a: 5  6  7
|    |        b: ref. j-re
|    |        c: 10  11  12
|    |
|    |    g(...)
|    |    |        x: ref. a-ra
|    |    |        y: ref. b-re
|    |    |
|    |    |    x++
|    |    |    y++
|    |    []
|    |    g(...)
|    |    |        x: ref. c-re
|    |    |        y: ref. a-ra
|    |    |    x++
|    |    |    y++
|    |    []
|    |    g(...)
|    |    |        x: ref. b-re
|    |    |        y: ref. c-re
|    |    |   x++
|    |    |   y++
|    |    []
|    |    cout ...
|    []
|    f(...)
|    |          a: 8 9 10
|    |          b: ref. i-re
|    |          c: 10  11 12
|    |    g(...)
|    |    |        x: ref. a-ra
|    |    |        y: ref. b-re
|    |    |
|    |    |    x++
|    |    |    y++
|    |    []
|    |    g()
|    |        ...
|    |    g()
|    |        ...
|    |    cout
|    []
|    f(...)
|    |      a: 7 8 9
|    |      b: ref j-re
|    |      c: 10 11 12
|    |  g()...
|    |  g()...
|    |  g()...
|    []
[]

h.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int g(int x)
{
    return 3*x + 1;
}

int f (int x)
{
    if (x % 2 == 0)
        return x/2;
    else
        return (x+1) / 2;
}

int h(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}

//   h(f(g(1)), f(h(g(5),13)))
//   h(f(4), f(h(16,13)))
//   h(2, f(16))
//   h(2, 8)
//   8

int main()
{
    cout << f(h(g(2), f(3))) << endl;
    cout << h(f(g(1)), f(h(g(5),13))) << endl;

    return 0;
}

i.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int f(int x, int &y)
{
    x++;
    y++;
    return x+y;
}

int g(int &x)
{
    x *= 2;
    return x+1;
}

int main()
{
    int a = 10, b = 20;

    cout << f(g(a), b) + a << endl;

    return 0;
}