Download Lab Week 6

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Recurrence relation wikipedia , lookup

Real number wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

System of polynomial equations wikipedia , lookup

Fundamental theorem of algebra wikipedia , lookup

Transcript
Object-Oriented Programming
Object-Oriented Programming (COM326)
Lab Week 6
Class and objects
Exercise 1: Pointers (Exercise from Week 5)
Objective here is to learn the technique to search an element in an array using pointer
technique. The elements of an array x={4,2,6,7,5,1,3} is stored in memory locations as
follows. ptr is a pointer pointing to the first element of the array. Find the smallest
element using pointer.
4
 ptr
2
6
7
5
void main()
{
int small, i;
int *ptr;
int x[5];
cout<< “Enter elements of array ::\n”;
for(t=0;t<5;t++)
cin>>x[t];
ptr= x;
/* ptr =&x[0] */
small =*ptr;
ptr++;
/*The loop can be done in two ways:
for(i=0;i<5;i++) or
ptr=&x[0];
for( ;ptr>=&x[4];ptr++)
{……..} */
for(i=1;i<6;i++)
{
if(small>*ptr)
small= *ptr;
ptr++;
}
cout<< “The smallest number is ” << small;
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
getch();
}
Test your program for both types of loops.
Exercise 2: Pointer in functions as arguments
Calculate the value of the function dis=(b*b-4*a*c) using pointer techniques.
The following points should be noted
 Formal parameters should be declared as pointer.
 When function is called, the addresses are passed as actual parameters.
double disc(int *, int *, int *);
void main()
{
int p=2, q=2, r=2;
double D;
D=dis(_p,_q,_r); //use appropriate parameters here
cout<<"discreminant " << d <<"\n\n";
getche();
}
double dis(int *a, int *b, int *c)
{
double *s;
//Do you need the pointer *s above?
*s=(*b)*(*b)-4*(*a)*(*c);
return *s;
}
Exercise 3: Class and objects
class solution
{
int a; int b; int c;
public:
void indata(void) //inside declaration
{
cout << "Enter a: ";
cin >> a;
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
cout << "Enter b: ";
cin >> b;
cout << "Enter c: ";
cin >> c;
}
//Define the method outside the class
void outdata(void)
{cout<<a<<b<<c<<endl;}
//Define the method outside the class
//Define a method called dis() inside or outside the class
// Use the function defined in Exercise 1
};
void main()
{
solution x; //object created
x.indata();
x.outdata();
x.dis();
getche();
//Create another object y, get the values of a,c, and c and display them
}
Exercise 4: This exercise is for super-mouse team.
Write a further three methods (function) for the class solution in Exercise 2,
which will check the term (b 2  4ac)  0 , (b 2  4ac)  0 and (b 2  4ac)  0 .
Write parametric methods (i.e. with three parameters) and return value that will
calculate the term (b 2  4ac). Call the methods in the main function in three
ways. Depending on the value of the term (b 2  4ac) , it will display two
messages as follows:
If (b 2  4ac)  0 , it will display the message as follows
-----------------------------------------------------------------Discriminant (b^2 – 4*a*c) =
There are no real solutions
------------------------------------------------------------------
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
If (b 2  4ac)  0 , it will display two solutions in the following form:
------------------------------------------------------------------Discriminant (b^2 – 4*a*c) =
There are two real solutions:
X1= (-b+sqrt(b^2 – 4*a* c)/2*a =
X2= (-b-sqrt(b^2 – 4*a* c)/2*a =
------------------------------------------------------------------Please note this is an exam type question. Try to understand all aspects of
functions and calls. Also do it with pointers and without pointers.
Write the programs in Exercise 2 and 3, compile, debug and run the program.
Once the program is running, show any two of them to module
coordinator/demonstrator for records. Save the program in folder on your U
drive. You will need the programs for the lab submission in week 11.
Imaginary Numbers
What is the square root of -9 ?
The answer is
(9  1) =
9  1 = 3   1
Setting i   1 , we get 3  i = 3i , where i is called an imaginary number.
An imaginary number is a complex number that can be written as a real number
multiplied by the imaginary unit i, which is defined by its property i 2  1 . The
square of an imaginary number (bi) 2 is −b2. For example, 5i is an imaginary
number, and its square is −25. Except for 0 (which is both real and imaginary),
imaginary numbers produce negative real numbers when squared.
An imaginary number bi can be added to a real number a to form a complex
number of the form (a  bi) , where the real numbers a and b are called,
respectively, the real part and the imaginary part of the complex number.
Imaginary numbers can therefore be thought of as complex numbers whose real
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
part is zero. The name "imaginary number" was coined in the 17th century as a
derogatory term, as such numbers were regarded by some as fictitious or useless.
The term "imaginary number" now means simply a complex number with a real
part equal to 0, that is, a number of the form bi .
Greek mathematician and engineer Heron of Alexandria is noted as the first to
have conceived these numbers. Rafael Bombelli first set down the rules for
multiplication of complex numbers in 1572.
The use of imaginary numbers was not widely accepted until the work of
Leonhard Euler (1707–1783) and Carl Friedrich Gauss (1777–1855). The
geometric significance of complex numbers as points in a plane was first
described by Caspar Wessel (1745–1818).
Figure 1: Complex number on a plane
In 1843 a mathematical physicist, William Rowan Hamilton, extended the idea
of an axis of imaginary numbers in the plane to a three-dimensional space of
quaternion imaginaries.
Example:
Figure 2: Spectrum Analyzer
Those cool displays you see when music is playing? Complex Numbers are used
to calculate them using methods called "Fourier Transforms". In fact many
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
clever things can be done with sound using Complex Numbers, like filtering out
sounds, hearing whispers in a crowd and so on.
A quadratic equation is a second-order polynomial equation in a single variable
x
ax 2  bx  c  0
With a  0 . Because it is a second-order polynomial equation, the fundamental
theorem of algebra guarantees that it has two solutions. These solutions may be
both real and both complex.
Example:
Figure 3: Solution to quadratic equation
The roots x can be found by completing the square
x2 
b
c
x
a
a
2
b 
c
b2
b 2  4ac

x 
   2 
2a 
a 4a
4a 2

b   b 2  4ac

x 

2a 
2a

Solving for x gives
x
 b  b 2  4ac
2a
That is, the solution to quadratic equation is given by the formula:
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
x
 b  b 2  4ac
2a
The plus-minus sign states that we have two solutions
x1 
 b  b 2  4ac
2a
x2 
 b  b 2  4ac
2a
The term b 2  4ac is very important and is called discriminant. Depending on the
value of the term b 2  4ac , there can be three possible situations:
(i) b 2  4ac  0
(ii) b 2  4ac  0
(iii) b 2  4ac  0
(i)If b 2  4ac  0 , we have two real solutions to the quadratic equation as
follows:
Solution 1: x1 
 b  b 2  4ac
2a
Solution 2: x 2 
 b  b 2  4ac
2a
(ii) If b 2  4ac  0 , we have two identical solutions to the quadratic equation as
follows:
Solution 1: x1 
b0 b

2a
2a
Solution 2: x 2 
b0 b

2a
2a
http://www.scis.ulster.ac.uk/~siddique
Object-Oriented Programming
(iii)If b 2  4ac  0 , we have two complex solutions to the quadratic equation as
follows:
Solution 1: x1 
b d bi d

2a
2a
Solution 2: x 2 
b d bi d

2a
2a
Where d is the discriminant.
http://www.scis.ulster.ac.uk/~siddique