Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎Lesson 2‎ > ‎

Sample Solutions

Prac 0

// A program to display the absolute value.
#include <iostream>
using namespace std;

int main() {
    float x;     // variable to store read integer number

    cout << "Type a real number: ";  // ask a user to type a real number
    cin >> x;                        // read the typed number and store it in the variable x.
    float abs = x;
    if (abs < 0) {
        abs = -x;
    }
    
    cout << "The absolute value of the number you typed is : " << abs << endl;
}

Prac 0-1

#include <iostream>

using namespace std;

float calculateArea(float base, float height) {
    return (base * height / 2.0);
}

int main() {
    float base;
    float height;

    cout << "Base : "; cin >> base;      // read the length of the base
    cout << "Height : "; cin >> height;  // read the height of the triangle
    cout << "The area is " << calculateArea(base, height) << "." << endl;

    return 0;
}

Prac 0-2

#include <iostream>

using namespace std;

namespace {
    const float PI = 3.14159;
}

float calcArea(float r) {
    return (PI * r * r);
}

float calcVolume(float r) {
    return (4 * PI * r * r * r / 3);
}

int main() {
    float r;

    cout << "Radius?: "; cin >> r;    // read the radius
    cout << "The surface area is: " << calcArea(r) << endl;
    cout << "The volume is : " << calcVolume(r) << endl;

    return 0;
}

Prac 0-3

#include <ctime>    // for time();
#include <cstdlib>  // for rand();
#include <iostream>

using namespace std;

int main() {
    srand(time(NULL));    // set a seed for the random function.

    cout << "the random number (1-9) is: " << 1 + rand() % 9 << "." << endl;
    cout << "the random number (-1-9) is: " << -1 - rand() % 9 << "." << endl;
    cout << "the random number (10-99) is: " << 10 + rand() % 90 << "." << endl;

    return 0;
}

Prac 0-4

#include <iostream>

using namespace std;

int main() {
    int n;
    cout << "Integer number: "; cin >> n;
    if (n > 0)
        cout << "It's a positive value" << endl;
    else if (n < 0)
        cout << "It's a negative value" << endl;
    else
        cout << "It's 0" << endl;
   
    return 0;
}

Prac 0-5

#include <iostream>

using namespace std;

int main() {
    int n;

    cout << "The integer value: ";
    cin >> n;
    if (n > 0)
        if (!(n % 7))
            cout << "It can be divided by 7." << endl;
        else
            cout << "It cannot be divided by 7." << endl;
    else
        cout << "It is not a positive number." << endl;

    return 0;
}

Prac 1

#include <iostream>

using namespace std;

namespace {
    // macro to calc an average
    inline int average(int x, int y) { return (x + y) / 2; }

    // macro to find the smaller value
    inline int smaller(int x, int y) { return (x < y ? x : y); }

    // macro to find the larger value
    inline int larger(int x, int y) { return (x > y ? x : y); }
}

int main() {
    // output an average
    cout << "Averages: ";
    cout << average(5, 8) << ' ';
    cout << average(4, 6) << endl;

    // find the smaller value
    cout << "Smaller: ";
    cout << smaller(3, 9) << ' ';
    cout << smaller(7, 2) << endl;

    // find the larger value
    cout << "Larger: ";
    cout << larger(3, 5) << ' ';
    cout << larger(6, 1) << endl;

    return 0;
}

When you use #define directive to define a macro with arguments, the strict type check won't be applied to those arguments. This is because the preprocessor is not a part of C++ language system. Hence, you should use an "inline function" instead of #define.

How would you describe such anonymous namespace in OODesign (UML)?

Prac 3

Person.h

#include <string>
#include <iostream>

using namespace std;

class Person{
private:
   string name, gender;
   float weight, height;

public:
   Person(string n, string g, float w, float h);
   void print();
   void setWeight(float w);
   float getWeight();
   void setHeight(float h);
   float getHeight();
};

Person.cpp

#include "Person.h"
Person::Person(string n, string g, float w, float h){
   name = n;
   gender = g;
   weight = w;
   height = h;
}

void Person::print(){
   cout << "Name: " << name << "\nGender: " << gender << "\nWeight: " << weight << "\nHeight: " << height << endl;
}

void Person::setWeight(float w){
   weight = w;
}

float Person::getWeight(){
   return weight;
}

void Person::setHeight(float h){
   height = h;
}

float Person::getHeight(){
   return height;
}

main.cpp

#include <iostream>
#include <string>
#include "Person.h"

using namespace std;

int main(){
   float w, h;
   string name, gender;
   cin >> w;
   cin >> h;
   cin >> name;
   cin >> gender;

   Person p(name, gender, w, h);
   p.print();

   cin >> w;
   p.setWeight(w);
   p.print();

   return 0;
}

Prac 4

In C++, struct is just like class and it represents a data type. Hence, you do not needstruct keyword whey you declare a struct variable.

#include <iostream>
using namespace std;

struct node {
    char name[10];
    node *pnode;
};

int main(void) {
    char name[][10] = {"Intel", "ATI", "NVIDIA", "MSI", "GIGABYTE"};
    node narray[5];
    node *pNode = NULL;

    int i; 
    for (i = 0; i < 5; i++)
        strcpy(narray[i].name, name[i]);

    for (i = 0; i < 4; i++)
        narray[i].pnode = &narray[i+1];
    narray[i].pnode = &narray[0];

    pNode = narray;
    for (i = 0; i < 10; i ++) {
        cout << pNode->name << endl;
        pNode = pNode->pnode;
    }

    return;
}
Comments