A user-defined class named Timer has a constructor that takes two integer parame
ID: 3940885 • Letter: A
Question
A user-defined class named Timer has a constructor that takes two integer parameters to initialize hour and minute data members. Write a single C++ statement to create an object of the Timer class using dynamic memory allocation and assign it to a pointer variable named timePt r. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. Write the definition for a function named rand Ar ray that takes a single integer argument, n, and returns a dynamically allocated array of n pseudo-random integers. You may assume the pseudo-random number generator has been previously declared and seeded, (i.e. you do not need srand(time(0)) or an include.) Now write C++ statements to call the randAr ray function to construct a 100 element array, then print the array values to the display (one per line) and delete the dynamically allocated array.Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
#include <time.h>
//using namespace std;
int * randArray(int);
int * array;
class Timer
{
int hour,minute;
public:
Timer(int x,int y)
{
hour=x;
minute=y;
}
public :
int * randArray(int n)
{
srand((unsigned)time(0));
for(int i=0; i<n; i++)
{
array[i] = (rand()%100)+1;
cout <<array[i]<<" ";
}
return array;
}
public :~Timer()
{
delete array;
}
};
int main() {
Timer * timePtr;
int * arr;
clrscr();
timePtr= new Timer(10,20);
arr=timePtr->randArray(100);
// The Below Code is for checking that we got same return array
//it's upto you if you need below code please uncoment it otherwise keep coment
/* cout<<"Inside The Main Method"<<endl;
for(int i=0;i<100;i++)
{
cout <<arr[i]<<" ";
} */
delete arr;
cout<<"After deleting The memmory"<<endl;
cout<<"!bye bye";
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.