Lab #9.1. Function parameters. Independence of parameter names and argumant names

Assignment. Given uncompleted program lab9.1. cpp.

#include <iostream>

using namespace std;

 

void add (int a, int &b, int c)

{

    // ADD CODE HERE

};

 

int main ()

{

    int a, b, c;

    cout << "Input two integer values:" << endl;

    cin >> a >> b;

    add (a, c, b);

    cout << a << "+" << b << "=" << c << endl;

    return 0;

}

Complete the program through implementing function add.

Specification. The program takes two integer values and prints out sum of these two.

A correct solution: lab9.1a. cpp

 

 

 

Lab #9.2. Default paramters

Assignment. Given uncompleted program lab9.2. cpp.

#include <iostream>

using namespace std;

 

int add ( // ADD CODE HERE

};

 

int main ()

{

    cout << add () << endl;

    cout << add (1) << endl;

    cout << add (1,2) << endl;

    cout << add (1,2,3) << endl;

    cout << add (1,2,3,4) << endl;

    return 0;

}

Complete the program through implementing exactly one function add.

Specification. Function add computes sum of several integer numbers (up to 4).

A correct solution: lab9.2a. cpp

 

 

 

Lab #9.3. Function overloading

Uzdevums. Given C++ program lab9.3. cpp.

#include <iostream>

using namespace std;

 

float add (float a, float b)

{

    return a + b;

};

 

double add (double a, double b)

{

    return a + b;

};

 

int main ()

{

    int x, y;

    cin >> x >> y;

    cout << add (x, y) << endl;

    return 0;

}

Do actions according to the scenario.

Specification. Compute sum of two numbers.

Scenario.

1.      Compile program.

2.      Observe and analyze the error message (call of overloaded  function is ambiguous) –the compiler can’t select the right function to execute.

3.      Change data types of x and y from int to float.

4.      Compile (should compile successfully).

5.      Run program.

6.      Change parameter types of function double add (double a, double b) from double to float. Now header of the function looks the following way: double add (float a, float b).

7.      Compile.

8.      Observe and analyze the error message (new declaration `double add(float, float)' ambiguates old declaration `float add(float, float)') – prototypes of two different functions are not allowed to differ only by return type.

9.      Cgange paramater types and the return type of function double add (float a, float b) to int. Now header of the function looks the following way: int add (int a, int b).

10.  Compile (should compile successfully).

11.  Run program.

12.  Add “ / 5” to the printed information (in function main), the new version looks as follows: cout << add (x, y) / 5 << endl;

13.  Compile and run program with numbers 6 and 6.

14.  Determine which function has been called.

15.  Change data types of x and y from float to int.

16.  Compile and run program with numbers 6 and 6.

17.  Determine which function has been called.

 

A correct solution: lab9.3a. cpp

 

 

Lab #9.4. Exercises (Similar to #4.5, but with functions)

 

Exercise 1. (lab9.4a.cpp)

Given 2 integers a, b.

If both numbers are less than 0, then print out product of a and b,

otherwise print out sum of these numbers.

To solve the task, you should use function with two int paramteres (a, b) which returns int.

Example #1. a=-7, b=-5; output 35.

Example #2. a=7, b=-5; output 2.

 

Exercise 2. (lab9.4b.cpp)

Given 3 integers a, b, c.

Find the middle number by value and find out – is it closer to the smallest number or the biggest number.

To solve the task, you should use function with three int paramteres (a, b) which returns int.

Example #1. a=1, b=2, c=3; output: 2, same distance.

Example #2. a=-7, b=2, c=-5; output: -5, closer to the smallest.

Example #3. a=-7, b=2, c=5; output: 2, closer to the biggest.

 

Vingrinājums 3. (lab9.4c.cpp)

(functionally the same as for exercise #1, but with different communication with function)

Given 2 integers a, b.

If both numbers are less than 0, then print out product of a and b,

otherwise print out sum of these numbers.

To solve the task, you should use function with three int paramters: first two for input, the third one for output, nothing is returned.

Example #1. a=-7, b=-5; output 35.

Example #2. a=7, b=-5; output 2.