Polinom
Sem I 2014-2015
Program Studi Teknik Informatika ITB
Tujuan
Mahasiswa memahami salah satu pemanfaatan struktur
data array untuk mengelola polinom
10/28/2014
Deskripsi Persoalan
Sebuah polinom berderajat n didefinisikan sebagai fungsi P(x) sebagai berikut :
P(x) = an x n + an-1 x n-1 +an-2 x n-2 + .............+a2 x 2 +a1 x 1 + a0
Definisi polinom di matematika
P(x) = a0 + a1 x 1 +a2 x 2 + ............. + an-2 x n-2 + an-1 x n-1 + an x n
Contoh polinom :
P1(x) = 4 x5 + 2 x4 +7x2 + 10
P2(x) = 23 x100 +9 x9 +2 x7 + 4 x5 +9 x9 + 2 x4 +3 x2 + 1
P3(x) = 10
P4(x) = 3 x2 + 2 x +8
P5(x) = x1000
BY/IF2031
10/28/2014
Operasi Polinom
Contoh polinom :
A(x) = 4 x5 + 2 x4 +7x2 + 10
B(x) = 9 x9 +2 x7 + 4 x5 +2 x4 +3 x2 + 1
Berapa A(x) + B(x)
A(x) =
4 x5 + 2x4 + 7x2 + 10
B(x) = 9 x9 +2 x7 + 4 x5 + 2x4 + 3x2 + 1
A(x)+B(x) = 9 x9 +2 x7 + 8 x5 + 4x4 + 10x2 + 11
Berapa A(x) B(x) ?
A(x)-B(x) =
Berapa A(X) =
4
10/28/2014
10 0
A(x) = 4 x7 - 2 x + 10
0
10 -2 0
5
4 0
BY/IF2031
10/28/2014
return 0;
}
BY/IF2031
10/28/2014
*/
/* P1(x) = 4 x^7 - 2 x + 10 */
/* isi suku polinom secara manual */
P1[0] = 10;
P1[1] = -2;
P1[7] = 4;
...
return 0;
}
BY/IF2031
10/28/2014
Format pencetakan memerlukan hasil yang lebih rapi, misalnya menyisipkan tanda +
BY/IF2031
10/28/2014
BY/IF2031
10/28/2014
Kenapa -1 ?
/* P1(x) = 4 x7 - 2 x + 10 */
/* isi suku polinom secara manual */
P1[0] = 10;
P1[1] = -2;
P1[7] = 4;
Degree1 = 7;
...
return 0;
}
10
BY/IF2031
10/28/2014
Pencetakan
void Cetak( int *P, int maxDegree)
{
int i;
for (i = 0; i <= maxDegree; i++) {
if (i<maxDegree) {cout << P[i] << x^ << i << + ;}
else {cout << P[i] << x^ << i;}
}
}
int main()
{
......
......
Cetak(P1, Degree1);
}
11
BY/IF2031
10/28/2014
typedef struct {
int P[10];
int Degree;
} Polinom;
12
BY/IF2031
10/28/2014
Mengisi Polinom
for (i = 0; i < 10; i++)
P1.P[i] = 0;
P1.Degree = -1;
/* Isi polinom */
/* P1(x) = 4 x7 - 2 x + 10 */
P1.P[0] = 10;
P1.P[1] = -2;
P1.P[7] = 4;
P1.Degree = 7;
13
BY/IF2031
10/28/2014
14
BY/IF2031
10/28/2014
Cetak Polinom
void Cetak(Polinom X)
{
int i;
for (i = 0; i <= X.Degree; i++)
if (i<X.Degree) {
cout << P[i] << x^ << i << + ;}
else {
cout << P[i] << x^ << i;}
}
/* Bagaimana mencetak Polinom kosong? */
15
BY/IF2031
10/28/2014
16
BY/IF2031
10/28/2014
Contoh:
17
P = 4 x5 + 2x4 + 7x2 + 10
P1 = 20 x4 + 8x3 + 14x
BY/IF2031
10/28/2014
Latihan
1.
2.
18
Bagaimana jika pemakai memasukkan data lebih dari satu suku berderajat
sama ???
BY/IF2031
10/28/2014
Latihan
19
BY/IF2031
10/28/2014
Latihan
20
BY/IF2031
10/28/2014