You are on page 1of 4

Practice excercices

1.)
Create a calculator that takes a number, a basic math operator (+,-,*,/,^), and
a second number all from user input, and have it print the result of the mathema
tical operation. The mathematical operations should be wrapped inside of functio
ns.
2) Write a program that asks the user for an integer number and find the sum of
all natural numbers up to that number.
3) Write a program that checks if the angles given can make a triangle or not (t
heir sum must be 180 degrees).
4) Write a program that prints on the screen all the even numbers up to 10.
5) Write a program that tells you if a number is even or odd.
6) Write a class called rectangle with two private variables: double width, doub
le height. Write member functions for computing the area and the perimeter.
Write a default constructor, a regular constructor and accessors for width and h
eight. Demonstrate the use of this class in a main routine.
7) Write a function that takes as an argument an integer n. The function should
output the pattern:
a)
*
**
***
****
Where the triangle has n rows.
Repeat this exercise for a different pattern:
*****
****
***
**
*
and for
*****
****
***
**
*
and for
*
***
*****

*******
Note that in the above you always need an odd number of stars each row. Figure
out a reasonable way to do this.
8)
Passing by value vs. passing by reference
Write the output of the following program:
int fun (int a, int &b)
{
int c=3;
cout << a << " " << b << " " << c << "\n";
a+=b;
b++;
cout << a << " " << b << " " << c << "\n";
return b;
}
int main()
{
int a=10, b=20, c=30;
cout << a << " " << b << " " << c << "\n";
c = fun(b,a);
cout << a << " " << b << " " << c << "\n";
}
9)
You may assume that the graphics project has been set up for you and you only ne
ed to write a snippet of code for the ccc_win_main routine. In this problem we
use the default coordinate system for our graphics window. That is, the top lef
t point is (-10,10) and the bottom right is (10,-10). (You may assume this is a
lready set up for you.)
Write code that creates a circle of radius 1 at all integer coordinate points.
That is, draw a circles centered at points (-10,10), (-9,10), ... ,(9,10),(10,10
), (-10,9),
(-9,9), ... ,(9,9),(10,9) ...

10)
A class called Fraction consists of private variables int numerator and int den
ominator. The Fraction class has a member function called get_value() that tak
es no arguments and returns a double. It has regular and default constructors.
It also has an operator member function + that takes as an argument another Frac
tion object and returns a Fraction object.
Here is an example of how the class might be used in a program:
Fraction A(5,3); // numerator set to 5 denominator set to 3
Fraction B(1,7); // numerator set to 1 denominator set to 7

Fraction C; // default constructor used, numerator set to 0


// denominator set to 1
int value = A.get_value(); // value is 5/3
C = A + B; // C becomes the sum of fractions A and B
// This means that the numerator of C is 5*7+3*1
// and the denominator of C is 3*7
a) Write the class declaration for the Fraction class.
b) Write the definitions of the regular constructor and the default constructor.
Refer to the example above to see how these work.
c) Write the definition for the member function get_value(). This function retu
rns the decimal value of the Fraction object (the numerator divided by the denom
inator).
d) Write the definition of the operator +. Write this function so that the expr
ession
C = A + B;
in the example above makes sense. The numerator of C should be given by the for
mula:
numerator_c = denominator_b * numerator_a + denominator_a * numerator_b
The denominator is given by:
numerator_c = denominator_a * denominator_b

You might also like