1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mixas84 [53]
3 years ago
14

Pointers with classes a) 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 timePtr. It should call the constructor with two parameters. Use values of 10 and 20 for hour and minute respectively. b) Write the definition for a function named randArray 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.) c) Now write C statements to call the randArray function to construct a 100 element array, then print the array values to the display (one per line) and delete the dynamically allocated array.
Computers and Technology
1 answer:
klemol [59]3 years ago
8 0

Answer:

Check the explanation

Explanation:

a) there is a need to create an allocated object of dynamic ability for the Timer class, and assign it to a pointer variable timePtr.

As we are going to create an object of timer class, so timePtr will be a pointer of type timer class. Hence, left side of the statement will be as follows.

Timer* timePtr

Now, to create an object in a dynamic way, we use new operator in C++. And at last, we need to call the constructor to create the object. So, the full statement will be as follows:

                        Timer* timePtr = new Timer (10, 20);

b)

//c++ code : a:

#include <iostream>

using namespace std;

class Timer

{

public:

int hr;

int minute;

Timer(int h,int min)

{

h=h+min/60;

min=min%60;

hr=h;

minute=min;

}

};

int main()

{

Timer *t=new Timer(10,20);

cout<<"Time : "<<t->hr<<" hr :"<<t->minute<<" min"<<endl;

return 0;

}

c)

//c++ code for b, c:

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

int* randfun_alloc(int n)

{

srand (time(NULL));

int *arr=new int[n];

for(int i=0;i<n;i++)

arr[i]=rand()%1000;

 

return arr;

}

int main()

{

int n;

cin>>n;

int *arr =randfun_alloc(n);

 

for(int i=0;i<n;i++)

cout<<arr[i]<<" ";

 

cout<<endl;

delete[] arr;//delete the allocated array

return 0;

}

Documents: bash Konsole File Edit View Bookmarks Settings Help raushan-HP-2000-Notebook-PC /Documents > g++ randarr.c

You might be interested in
Write a function to sum the following series:
Phoenix [80]

Answer:

<u>C program to find the sum of the series( 1/2 + 2/3 + ... + i/i+1)</u>

#include <stdio.h>

double m(int i);//function declaration  

//driver function

int main() {

int i;

printf("Enter number of item in the series-\n");//Taking input from user

scanf("%d",&i);

double a= m(i);//Calling function

printf("sum=%lf",a);

return 0;

}

double m(int i)//Defining function

{

double j,k;

double sum=0;

for(j=1;j<i+1;j++)//Loop for the sum

{

k=j+1;

sum=sum+(j/k);

}

return sum;

}

<u>Output:</u>

Enter number of item in the series-5

sum=3.550000

5 0
3 years ago
five pros and cons of online learning (this is actually for a paper for my computer class please don't report me)
Travka [436]

Answer:

pros:

tons of curriculum

more academic resources

work at your own pace

endless ideas for projects

delve deeper into topics you enjoy, instead of reading a boring textbook.

- you can do cons yourself, you can do it!

     

\

7 0
3 years ago
Function Name: d2x Parameters: int, int Returns: string Description: Write function, d2x, that takes as input a nonnegative inte
Marina CMI [18]

Answer:

I am writing a Python program. Let me know if you want the program in some other programming language.

#definition of function d2x which takes two parameters v: which is a non #negative integer and x which is an integer between 2 and 9. this method #returns string of digits that represents the base-x representation of v

def d2x(v,x):

   remainder = v%x  

   if v<=1:

       return str(v)

   else:

       return str(d2x(v//x,x)) + str(remainder)

 

v= int(input("Enter a non negative integer: "))

x= int(input("Enter an integer between 2 and 9: "))

print(d2x(v,x))

Explanation:

I will explain the code line by line.

def d2x(v,x) This is the definition of function d2x which takes two parameters v: which is a non negative integer and x which is an integer between 2 and 9. This method returns string of digits that represents the base-x representation of v.

Now lets take an example to understand the working of this function.

Suppose the value of v = 10 and x = 2.

remainder = v%x  takes the mod of v and x which returns the remainder of the division of v by x. So v%x = 10 % 2 = 0. So remainder = 0.

if v<=1 This if condition checks if the value of v is less than or equal to 1. This is false because v=10 and 10 is greater than 1. If v is 1 or less than return str(v)  will return the value of v as it is i.e. 10.

Since the IF condition is false so the else part will execute which has the following statement: return str(d2x(v//x,x)) + str(remainder)

This calls the d2x function recursively. Here v is divided by x successively until the quotient of v divided by x is 0. On each successive division, the remainder is the new most significant digit. In v//x, the double slash is called floor division or integer division which we use as both v and x are integers. So using recursion the above statement becomes:

str(d2x(v//x,x)) + str(remainder)

str(d2x(10//2,2) + str(0)

d2x(5,2) + (0)

Now d2x will again be called recursively to perform the division and this successive division continues until quotient of v divided by x is 0. Here str() method is used returns the result in the form of string.  So v%x is the number which is added to the end of resultant number. v//x is the recursive portion which keeps using the answer of the previous binary number. This means 10 in base 2 calls convert(5,2), then adds 0 at the end. So when v//x=5 and v%x=0 So the answer is 1010.

At the end v= int(input("Enter a non negative integer: "))

x= int(input("Enter an integer between 2 and 9: "))  these two statement wills take integer input from user and print(d2x(v,x)) calls d2x method and print the result.

6 0
3 years ago
An employment situation that pays bills but is largely unfilling is
Ilia_Sergeevich [38]

Answer:

Explanation:Any job that a person doesn't particularly like or enjoy.

7 0
3 years ago
Identify the tools on the boxes. write the answer
Gnom [1K]

Answer:

im going off of what i know =( 2 screw driver 3 zip ties 5 needle nose plyers 8 can air

Explanation:

5 0
3 years ago
Other questions:
  • What is an (CR) Optical character recognition?
    13·2 answers
  • A(n) ____ is a predefined procedure that you can call (or invoke) when needed.
    9·1 answer
  • Which plot element is typically the turning point in the most intense moment of a story
    10·2 answers
  • Which technology forms the foundation for cloud computing? forms the foundation for cloud computing.
    12·2 answers
  • Who invented the collodian process
    6·2 answers
  • What method of research deals with large amounts of information that is analyzed and presented statistically?
    9·1 answer
  • PLEASE HELP!!! WILLGIVE BRAINLIEST!!!
    11·2 answers
  • Um ok that makes sooo much sence
    11·2 answers
  • Ransomware as a service has allowed the increase of:.
    10·1 answer
  • Which keyboard shortcut would you press to copy cells that are selected in a spreadsheet?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!