Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1 KOM3550 Object Oriented Programming Assignment 3

Assignment 3 Due Date: Dec.15.2021

Name:
Number:

1) Given Array.h and type the corresponding implementation file Array.cpp

// Array Class Header file : Array.h Hint:


... Array::Array()
class Array {
{ Size=1;
public: data=new int[Size];
Array();//constructor for (int i=0; i<Size; i++)
// initialize the object to have one element with a value 0 data[i]=0;
// dynamic memory allocation is needed. }
Array(const int SIZE, const int val); //constructor
// initialize the object to have the size Size with value val // this is how you apply
// dynamic memory allocation is needed. // dynamic memory allocation
// for the first constructor
Array(const int SIZE, const int* arr); //constructor // function
// initialize the object to have the size Size
// with values of array’s elements
// dynamic memory allocation is needed.

Array(const Array& src_Arr);


// initialize the object to have the size of src_Arr
// with values of src_Arr’s elements
// dynamic memory allocation is needed.

void add(const Array& src_Arr);


void subtract(const Array& src_Arr);
void multiply (const Array& src_Arr);
// add, subctract, multiply (element-wise) two Array objects
// the caller object has to be updated after the operation.
// A1.add(A2) ; A1 is updated
// check if they are compatiple to apply these.

int product(const Array& src_Arr);


// inner product of two Array objects
// check if they are compatiple to apply this.

void copy(const Array& rhs_Arr);


// copy the elements of an Array object, update the size.
// dynamic memory allocation is needed.

int getSize();
// return the size of the array
int* getData();
// a pointer to the elements of the array is returned
void fill(const int value);
//fill the array with a constant value
void print();
//display the array information

private:
int* data;
int size;
};
----------------------------------------------------------
// test_Array.cpp
Array a1; // a1 ← {0}
Array a2(5, 0); // a2 ← {0, 0, 0, 0, 0}
int arr[]={3,3,3,3,3};
Array a3(5, arr); // a3 ← {3, 3, 3, 3, 3}
2 KOM3550 Object Oriented Programming Assignment 3

Array a4(a3); // a4 ← {3, 3, 3, 3, 3}

a2.add(a3); // a2 ← {3, 3, 3, 3, 3}
a2.subtract(a3); // a2 ← {0, 0, 0, 0, 0}
a2.fill(1); // a2 ← {1, 1, 1, 1, 1}
a2.multiply(a3); // a2 ← {3, 3, 3, 3, 3}
cout << a3.product(a4)<<endl; // 45

cout << a3.getSize()<<endl; //5


cout << a3.getData()[2]<<endl; //3

a2.copy(a3); // a2 ← {3, 3, 3, 3, 3}
a2.fill(1); // a2 ← {1, 1, 1, 1, 1}
a2.print(); // [1 1 1 1 1]

2) Given Pi.h and type the corresponding implementation file Pi.cpp


4 4 4 4
π=4− + − + …
3 5 7 9
// Array Class Header file : Pi.h
class Pi
{
public:
Pi (const int nTerms);
double apprErr();
//uses value() function’s return and value of M_PI, returns approximation error.
void print(); // //uses value() function’s return displays it
private:
int n;
double value(); // return value of pi with assigned number of terms
};

...

Pi pi2(2);
pi1.print();
cout << pi1.apprErr()

Pi with 2 terms: 2.66667


0.47619

 Submit your files for the codes, and screen images for your runs in one zipped folder.
 A suitable name for the folder can be YourNumber_YourName_KOM3550_Assignment3.{zip/rar}.

Dr. Muharrem Mercimek

a. The due date is firm. The files should be submitted by the end of the due date.
b. Submit your documents via e-mail to programming.kom@gmail.com

You might also like