Tutorial 3 C++

Exercise 1

int i = 16;

int j = 32;

int k = 8;

bool r;

1. r = (i == 16);   True

2. r = (k != 16);   True

3. r = (k < 10);   True

4. r = (j > 33);  False

5. r = (j == (k * 2));   False

6. r = (j == (i * 2));  True

7. r = ((k + i) <= (j – i));   False

8. r = ((i / k) == 2);   True

9. r = ((( i + j + k ) / k ) >= 7 );   False

Exercise 2

#include <iostream>

using std::cout;

using std::cin;

int hi_score = 500;

int main()

{

    int score1, score2;

    cout << “Player one please input a number: ” ;

    cin >> score1;

    cout << “Player two please input a number: ” ;

    cin >> score2;

    if (score1 >= hi_score )

        cout << “This score is the new high score !: ” << score1 << “\n”;

    if (score2 >= hi_score )

        cout << “This score is the new high score !: ” << score2;

    if (score1 > score2)

        cout << “Player 1’s score is higher! \n”;

    if (score2> score1 )

        cout << “Player 2’s score is higher! \n”;

    if (score1 == score2)

        cout << “Both Players had the same score! \n”;

    return 0;

}

Exercise 3

#include <iostream>

using std::cout;

using std::cin;

int hi_score = 500;

int main()

{

    int score1, score2;

    cout << “Player one please input a number: ” ;

    cin >> score1;

    cout << “Player two please input a number: ” ;

    cin >> score2;

    if (score1 >= hi_score )

        cout << “This score is the new high score !: ” << score1 << “\n”;

    if (score2 >= hi_score )

        cout << “This score is the new high score !: ” << score2;

    if (score1 > score2)

        cout << “Player 1’s score is higher! \n”;

    if (score2> score1 )

        cout << “Player 2’s score is higher! \n”;

    if (score1 == score2)

        cout << “Both Players had the same score! \n”;

    return 0;

}

In my code the number for my local variable is displayed over the global variable, this is because variables inside a function take priority.

#include <iostream>

int test_number = 100;

int main()

{

    int test_number = 200;

    std::cout << test_number << “\n”;

    std::cout << ::test_number;

    return 0;

}

With this code the value for the global integer is also displayed because I used the scope resolution operator ( :: ) .

Exercise 4

#include <iostream>

using std::cout;

int main()

{

    int i = 16, j = 32, k = 8;

    bool r;

    if (i == 16)

        cout << (i == 16) << “r = (i == 16)” << ” is true \n”;

    else

        cout << (i == 16) << “r = (i == 16)” << ” is false \n”;

    if (k != 16)

        cout << (k != 16) << “r = (k != 16) is true \n”;

    else

        cout << (k != 16) << “r = (k != 16) is false \n”;

    if (k < 20)

        cout << (k < 20) << “r = (k < 20) is true \n”;

    else

        cout << (k < 20) << “r = (k < 20) is false \n”;

    if (j > 33)

        cout << (j > 33) << “r = (j > 33) is true \n”;

    else

        cout << (j > 33) << “r = (j > 33) is false \n”;

    if (j == (k * 2))

        cout << (j == (k * 2)) << “r = (j == (k * 2)) is true \n”;

    else

        cout << (j == (k * 2)) << “r = (j == (k * 2)) is false \n”;

    if (j == (i * 2))

        cout << (j == (i * 2)) << “r = (j == (i * 2)) is true \n”;

    else

        cout << (j == (i * 2)) << “r = (j == (i * 2)) is false \n”;

    if ((k + i) <= (j – i))

        cout << ((k + i) <= (j – i)) << “r = ((k + i) <= (j – i)) is true \n”;

    else

        cout << ((k + i) <= (j – i)) << “r = ((k + i) <= (j – i)) is false \n”;

    if ((i / k) == 2)

        cout << ((i / k) == 2) << “r = ((i / k)==2) is true \n”;

    else

        cout << ((i / k) == 2) << “r = ((i / k)==2) is false \n”;

    if (((i + j + k) / k) >= 7)

        cout << (((i + j + k) / k) >= 7) << “r = (((i + j + k) / k) >= 7) is true \n”;

    else

        cout << (((i + j + k) / k) >= 7) << “r = (((i + j + k) / k) >= 7) is false \n”;

      return 0;

}

Exercise 5

If the code is run it will not display anything as there is a ; after the if statement that stops the code from running.

#include <iostream>

using namespace std;

int main()

{

    if (false)

                    ;   

    {

        cout << “This text wont be shown.”;

    }

    return 0;

}

Exercise 6

If you run the code it should display “x and y are equal” onto the screen. This would run anyway as the the if statement does not use the correct operator. Instead of using (x = y) it should use (x == y).

#include <iostream>

using namespace std;

int main()

{

    int x = 5;

    int y = 7;

    if (x = y)

    {

        cout << “x and y are equal.”;

    }

    return 0;

}

Design a site like this with WordPress.com
Get started