Practice 0Write 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-1Write 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-2Write 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-3Write a code which generates following random numbers:
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-4Write 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-5Write 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 1Try 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 name, gender, height, 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
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
Practice 4The 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[10]; struct node *pnode; }; int main(void) { char name[][10] = {"Intel", "ATI", "NVIDIA", "MSI", "GIGABYTE"}; struct node narray[5]; 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[0]; narray[i].pnode = &narray[0]; pNode = narray; for (i = 0; i < 10; i ++) { cout << pNode->name << endl; pNode = pNode->pnode; } return; } Practice 5Why 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 add(Point pt) { 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; } Sample Solutions |
Lesson 2
Subpages (1):
Sample Solutions