Tutorial 2 C++

Tutorial 2

Exercise 1:

#include <iostream>

//Seb Crowhurst tutorial 2 exercise 1

int main()

{

    int num1, num2, num3;

    std::cout << “Please enter a number: “;

    std::cin >> num1;

    std::cout << “Please enter a second number: “;

    std::cin >> num2;

    std::cout << “Please enter a third number: “;

    std::cin >> num3;

    std::cout << “Your numbers were: ” << num3 << “,” << num2 << “,” << num1 << “.”;

    return 0;

}

Exercise 2:

#include <iostream>

using std::cout;

using std::cin;

int main()

{

    float num1, num2, num3, sum, average ;

    cout << “Please enter a number (can be decimal): ” ;

    cin >> num1;

    cout << “Please enter a second number: ” ;

    cin >> num2;

    cout << “Please enter a third number: ” ;

    cin >> num3;

    sum = num1 + num2 + num3 ;

    average = (num1+ num2 + num3) / 3 ;

    cout << “Thank you, the sum of your numbers is: ” << num1 << ” + ” << num2 << ” + ” << num3 << ” = ” << sum ;

    cout << “The average of your numbers is: ” << average;

    return 0;

}

Exercise 3:

#include <iostream>

using std::cout;

using std::cin;

int main()

{

    float radius, circumference, area ;

    cout << “Please enter a radius: “;

    cin >> radius;

    circumference = (radius * 2) * 3.14;

    area = 3.14 * (radius * radius);

    cout << “Here is the circumference: ” << circumference << ” Here is the area: ” << area;

    return 0;

}

In this code it would be a good idea to make pi a constant as the number will never need to change as it is the same in all calculations.

Exercise 4:

#include <iostream>

using std::cout;

using std::cin;

int main()

{

    // there is no float modulo as it only works with integers

    float float1, float2, floatadd, floatdivide, floattimes, floatminus, fiadd, fidivide, fitimes, fiminus ;

    int int1, int2, intadd, intdivide, inttimes, intminus, intmodulo ;

    cout << “Please enter one decimal number: ” ;

    cin >> float1 ;

    cout << “Please enter a second decimal number: ” ;

    cin >> float2 ;

    cout << “Please enter one non-decimal number: ” ;

    cin >> int1 ;

    cout << “Please enter one non-decimal number: ” ;

    cin >> int2 ;

    floatadd = float1 + float2, floatdivide = float1 / float2, floattimes = float1 * float2, floatminus + float1 – float2;

    intadd = int1 + int2, intdivide = int1 / int2, inttimes = int1 * int2, intminus = int1 – int2, intmodulo = int1 % int2;

    fiadd = float1 + int1, fidivide = float1 / int1, fitimes = float1 * int1 fiminus = float1 – int1;

    cout << “Here are the results for your floats: ” << ” +: ” << floatadd << ” ~ /: ” << floatdivide << ” ~ *: ” << floattimes << ” ~ -: ” << floatminus ;

    cout << ”    Here are the results for your integers: ” << ” +: ” << intadd << ” ~ /: ” << intdivide << ” ~ *: ” << inttimes << ” ~ -: ” << intminus << ” ~ %: ” << intmodulo;

    cout << ”    Here are the results for the first float and first integer: ” << ” +: ” << fiadd << ” ~ /: ” << fidivide << ” ~ *: ” << fitimes << ” ~ -: ” << fiminus;

    return 0;

}

Exercise 5:

#include <iostream>

using std::cout;

using std::cin;

int main()

{

    int num1;

    float num2;

    cout << “Please enter a number: “;

    cin >> num1

    cout << “Please enter a second number: “;

    cin >> num2

    num1++;

    num2++;

    ++num1;

    ++num2;

    cout << “Postfix: ” << num1++;

    cout << ”  Prefix: ” << ++num1;

    cout << ”  Number 2 postfix: ” << num2++;

    cout << ”  Number 2 prefix: ” << ++num2;

        num1 *= num2 + 3;

    return 0;

}

Exercise 6:

#include <iostream>

using std::cout;

using std::cin;

int main()

{

    int num1, num2, num3;

    cout << “Please enter a number: ” ;

    cin >> num1;

    cout << “Please enter a second number: “;

    cin >> num2;

    cout << “Please enter a third number: “;

    cin >> num3;

    cout << “The sum of your numbers is: ” << (num1 + num2) * num3;

    return 0;

}

Exercise 7

#include <iostream>

using std::cout;

using std::cin;

enum rocks{Garnet=75, Heulandite = 25, Hematite = 10, Halite = 15};

int main()

{

    const int constint = 125;

    const float constfl = 7.5f;

    rocks shiney = Garnet;

    rocks mineral = Heulandite;

    rocks oxide = Hematite;

    rocks sediment = Halite;

    cout << constint << “\n”;

    cout << constfl << “\n”;

    cout << “Garnet = ” << Garnet << “\nHeulandite = ” << Heulandite << “\nHematite = ” << Hematite << “\nHalite = ” << Halite;

    return 0;

}

Design a site like this with WordPress.com
Get started