Question

Write a function that takes as an input parameter an integer that will represent the length...

Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49.

The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number in the array with the second parameter +1 (so in our above example, it would be 51. ) Continue to do this until every number in the original array is printed out in order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void print_sorted(int n, int max) {
   int *arr = new int[n];
   for(int i =0 ; i < n; ++i) {
      arr[i] = rand() % max;
   }
   for(int i = 0; i < n; ++i) {
      int minInd = i;
      for(int j = i; j < n; ++j) {
         if(arr[j] < arr[minInd]) {
            minInd = j;
         }
      }
      if(i != minInd) {
         int temp = arr[minInd];
         arr[minInd] = arr[i];
         arr[i] = temp;
      }
      cout << arr[i] << " ";
   }
   cout << endl;
}

int main() {
   srand(time(NULL));
   print_sorted(20, 50);
   return 0;
}

1 2 6 11 14 15 15 16 23 29 30 31 31 32 36 39 40 46 47 48 Process exited after 0. Press any key to continue - - - 1285 seconds

Add a comment
Know the answer?
Add Answer to:
Write a function that takes as an input parameter an integer that will represent the length...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all...

    C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all the numbers less that the parameter that are divisors of the parameter (i.e. divides it without a remainder) including 1. So printdivisors(6) will print 1,2,3. Note you may use a wrapper function or default parameters. (b) Write a function, sumdixisors, that takes a single integer parameter and returns the sum of all the divisors of the parameter (including 1). So sumdivisors(6) will return 6...

  • programming language Write a program that will generate 5 random numbers between 20 and 80 and...

    programming language Write a program that will generate 5 random numbers between 20 and 80 and assign each to a variable of datatype double. You should use a random number generator seeded with time Using a series of if statements, determine which of the 5 variables contains the smallest value generated Print all five random numbers to the screen and print out the value that you determined was the smallest. All values should be printed with zero decimal places. 2.

  • Write a user-defined MATLAB function that gives a random integer number within a range between two...

    Write a user-defined MATLAB function that gives a random integer number within a range between two numbers. For the function name and arguments, use n = randint(a,b) where the two input arguments a and b are the two numbers and the output argument n is the random number. Use the function in the command window for the following: (a) Generate a random number between 1 and 49. (b) Generate a random number between -35 and -2

  • In C: Write a function "MinMax" that takes as an argument an integer array, an integer...

    In C: Write a function "MinMax" that takes as an argument an integer array, an integer for the size of the array, and two integer pointers. The function should print nothing and return nothing but change the value of the first pointer to the minimum value in the array and change the value in the second pointer to the max value in the array.

  • Mapping Keys to Integer Values: Write a function mapKey() that can take a parameter of a....

    Mapping Keys to Integer Values: Write a function mapKey() that can take a parameter of a. One String. For example: "Mary" or "lamb" or "a" or "" (null string) b. A list of Strings example: [ "Mary", "had", "a", "little", "lamb"] c. A Number d. A list of numbers And transforms the key to an integer numeric value by adding the numbers up in the list. You can use ord() to convert character to numberic or use a table lookup...

  • Write a function that takes an integer input n as a parameter and after it calculates...

    Write a function that takes an integer input n as a parameter and after it calculates the sum of 1+2+3+...+n, it returns the value to the main program. For example, if n=4, the output of the sum will be 10.

  • C++ Write a function that takes as input parameters (using call by pointer) 3 integers. It...

    C++ Write a function that takes as input parameters (using call by pointer) 3 integers. It generates a random number between 25 and 50 (not including 50). It then creates an array on the memory heap of that length. It generates a random high number (mine was between 5 and 10) and a random low number (between -5 and -10) and fills in the array iteratively with random numbers between the high and the low numbers*, and it returns that...

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • use javascript please Complete the function below using recursion. Assume the parameter is a positive integer....

    use javascript please Complete the function below using recursion. Assume the parameter is a positive integer. The function will return an array that contains all of the numbers from 1 up to the parameter in consecutive order. For example, if the parameter is the value 5 then the return value should be the array [1, 2, 3,4 function makeSequence(value) t\ please use recursion thank von

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT