Resources‎ > ‎Programming‎ > ‎C++‎ > ‎C++ Tutorial‎ > ‎

### Practice 0

Write a code which reads a real number (a number with decimal point) and display its absolute value. In this practice, you learn the difference between an integer number and a real number.

### Practice 0-1

Write a code which reads the length of the base and the hight of of a triangle and calculate triangle's area. In this practice, you learn more about float number.

### Practice 0-2

Write a code which reads the radius of a sphere and calculate its surface area and volume. In this practice, you learn the use of a constant.

### Practice 0-3

Write a code which generates following random numbers:

• a random number between 1 and 9,
• a random number between -9 and -1,
• a random number between 10 and 99.

In this practice, you learn how to use a function, which is defined and implemented by someone else.

You can use a function called rand(), which is defined in <cstdlib>. You can initalize this function using the current time like:

```#include <ctime>

...
srand(time(NULL));    // set a seed of the random number generator.
...```

### Practice 0-4

Write a code, which reads an integer value from a console, and examine its sign (plus/minus/0) and display it.

Sample output:

```Integer number: 22
It's a plus value.

Integer number: -33
It's a negative value.

Integer number: 0
It's 0.```

### Practice 0-5

Write a code, which reads an positive integer value from a console, and examine whether it can be divided by 7 or not. The following is a sample output:

```The integer value: 42
It can be divided by 7.

The integer value: 33
It cannot be divided by 7.

The integer value: -21
It is not a positive value.```

You should try to use nested if statements 1) for examining whether the number is positive or negative and 2) for examining whether the number can be divided by 7 or not.

### Practice 1

Try compile and execute the following code. If you spot any undesirable code, rewrite it so that it comply with C++ standard.

```#include <iostream>

using namespace std;

// macro to calc an average
#define average(x,y) ((x + y) / 2)

// macro to find the smaller value
#define smaller(x,y) (x < y ? x : y)

// macro to find the larger value
#define larger(x,y) (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;
}```

### Practice 2 (Class)

Define a People class, which has namegenderheight, and weight. Without using newexpression, create instances of People. Once you create the instances, substitute appropriate values to their members. Once all members are set, display them on the console.

#### Note

• make sure you understand the format of defining a class.
• make sure you understand how to access the members of an instance.

### Practice 3 (Class)

Use the above code and make height and weight members private. In order to substitute values those private members, define and implement member functions such as:

```void setHeight(float h) {}
float getHeight(){}
void setWeight(float w) {}
float getWeight(){}```

Without using new expression, create instances of People. Once you create the instances, substitute appropriate values to their members. Once all members are set, display them on the console.

#### Note

• make sure you understand the format of defining a class with member functions.
• make sure you understand how to implement member functions.
• make sure you understand how to use member functions.

### Practice 4

The following is a C++ code but contains lines which are not suited for C++. Discuss how the code should be changed.

```#include <iostream>
using namespace std;

struct node {
char name;
struct node *pnode;
};

int main(void) {
char name[] = {"Intel", "ATI", "NVIDIA", "MSI", "GIGABYTE"};
struct node narray;
struct 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;
narray[i].pnode = &narray;

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

return;
}```

### Practice 5

Why the following code produce lots of compile errors?

```#include <iostream>
#include <cmath>
using namespace std;

class Point {
int x, y;

int getx() {return x;}
int gety() {return y;}

void setx(int xval) {x = xval;}
void sety(int yval) {y = yval;}

Point tmp;
tmp.setx(x);
tmp.sety(y);
tmp.x += pt.getx();
tmp.y += pt.gety();
return tmp;
}

double distance(Point pt) {
return fabs(sqrt(pow(abs(x - pt.getx()), 2) + pow(abs(y - pt.gety()), 2)));
}
};

int main() {
Point pt1, pt2;

for (int i = 0; i < 10; i++) {
pt1.setx(i), pt1.sety(i);
pt2.setx(i + i * 2), pt2.sety(i + i * 2);
cout << "pt1("<< pt1.getx() << "," << pt1.gety() << ") ";
cout << "pt2("<< pt2.getx() << "," << pt2.gety() << ") " << endl;
cout << "distance between p1 and p2 is: " << pt1.distance(pt2) << endl;
}

return;
}```

Subpages (1):