Question

Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array...

Write a function that passes an array argument, getRandomNumbers, to get a pointer to an array of random numbers in the array. The function dynamically allocates an array, uses the system clock to seed the random number generator, populates the array with random values, and then returns a pointer to the array.

Function getRandomNumbers to generate a random array and return a pointer. int* getRandomNumbers(int num);

// The parameter indicates the number of numbers requested. The algorithm can be described as follows: Accept an array size as argument Dynamically allocate a new array that is the same size as the argument. White a loop to generate random numbers to the new array Return result as a pointer.

Here's what I have so far, but can't get it to work. please label what you did so I may disect it and learn from it

#include
#include
using namespace std;
int* getRandomNumbers(int *num,int & size);
int main()
{
   int* num;
   int size;
   cout << "How big should the array be?" ;
   cin >> size;
   num = new int[size];
   cout << num;
  
   return 0;
}

int* getRandomNumbers(int *num,int &size)
{
   srand(time(0));
   for (int i = 0;i < size;i++)
   {
       num[i] = rand() % 50 + 1;

   }
   return num;
}

Homework Answers

Answer #1

So, here's what that went wrong:
-> Header files iostream, stdlib.h and time.h were not included.
-> After reading the input 'size', there is no call for the function.
-> Also, I see that you're printing the address of num but not the values in the array.
I've made the necessary changes for the program to work.

Code:

#include<iostream>//including required headers
#include<stdlib.h>
#include<time.h>
using namespace std;
int* getRandomNumbers(int *num,int & size);
int main()
{
int* num;
int size;
cout << "How big should the array be?" ;
cin >> size;
num = new int[size];
num = getRandomNumbers(num, size);//calling function
for(int i = 0; i < size ; i++)//printing values
cout << num[i] << " ";
  
return 0;
}

int* getRandomNumbers(int *num,int &size)
{
srand(time(0));
for (int i = 0;i < size;i++)
{
num[i] = rand() % 50 + 1;

}
return num;
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Write a C++ program to run a menu-driven program with the following choices: Compute the factorial...
Write a C++ program to run a menu-driven program with the following choices: Compute the factorial of a number Compute the alternating factorial of a number Quit #include <iostream> using namespace std; void getValidUserInputPosNumGT0 (int *a) { int num; cout << "Enter in a positive number greater than 0... "; cin >> *a; } long double factorial (int num) { int fact = 1; while (num > 1) { fact *= num; num--; } return fact; } long double AlternatingFactorial...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse (long       num, long   equation,long reverse = 0); int       main() {               long       num, reverse = 0;        cout << "Enter       the       num:       ";        cin >> num;        cout << "Reverse       num       is       =       "               << reverse << endl;        return       0; } long reverse(long       num, long equation, long reverse = 0) {        while (num)        {               equation...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • 7. How have angiosperms coevolve with pollinators to enhance the fitness of both partners? Consider factors...
    asked 10 minutes ago
  • If the effect size for a four-group study was .5, the sum of square between was...
    asked 23 minutes ago
  • What technology out there do you find the most exciting and interesting currently? Why?  What interests you...
    asked 29 minutes ago
  • 5. Classify each description as observational or experimental (a)Participants in a study to determine the effects...
    asked 32 minutes ago
  • Python Write function stringCount() that takes two string inputs—a file name and a target string— and...
    asked 32 minutes ago
  • If you pay more in tuition to go to a top business​school, will it necessarily result...
    asked 38 minutes ago
  • Choosing manual vs automated testing comes down to benefits gained on your particular project. In this...
    asked 40 minutes ago
  • In banker's algorithm, which process will be handle first? process   need (A, B) p1            1, 4...
    asked 50 minutes ago
  • Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...
    asked 52 minutes ago
  • Justify answer. Is the Jaccard coefficient for two binary strings (i.e., string of 0s and 1s)...
    asked 54 minutes ago
  • Solve the following system of equations and determine if there is a unique solution, an infinite...
    asked 1 hour ago
  • In C++ ------------------------------------------ All functions take no parameters as input and return nothing. Create a function...
    asked 1 hour ago