Question

In: Computer Science

In C++ Declare an integer array of size 20 and assign the array with 20 randomly...

In C++

  1. Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points)

#include<iostream>

using namespace std;

const int NUM = 20;

int main() {

     

      // write code to Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100].

      // Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program.

      // Write code to print out the shuffled array.

      return 0;

}

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int NUM = 20;
int main()
{
        int arr[NUM];
        srand(time(0));
        for (int i = 0; i < NUM; i++)
        {
                arr[i] = (rand() % (100 - 1 + 1)) + 1;
        }
        for (int i = 0; i < NUM; i++)
                cout << arr[i] << " ";
        for (int i = 0; i < NUM; i++)
        {
                int temp;
                if (arr[i] >= 50)
                {
                        for (int j = i + 1; j < NUM; j++)
                        {
                                if (arr[j] < 50)
                                {
                                        temp = arr[i];
                                        arr[i] = arr[j];
                                        arr[j] = temp;
                                        break;
                                }
                        }
                }
        }
        cout << endl;
        for (int i = 0; i < NUM; i++)
        {
                cout << arr[i] << " ";
        }
}

Related Solutions

Declare an integer array of size 20 and assign the array with 20 randomly generated integers...
Declare an integer array of size 20 and assign the array with 20 randomly generated integers in the range [1, 100]. Shuffle the data in the array so that all numbers smaller than 50 are put before the numbers greater or equal to 50. Note you are not allowed to create any other arrays in the program. Finally, print out the shuffled array. (25 points) #include<iostream> using namespace std; const int NUM = 20; int main() {             //...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
C++ Write a program to declare an array of double of size 25, ask the user...
C++ Write a program to declare an array of double of size 25, ask the user to input all array elements from the keyboard, then write a function named out_of_order that will test this array for the condition a[0] >= a[1] >= a[2] >= ... > a[24] The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order. Explain what you do to avoid...
In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a...
In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a for loop to ask the user for numbers and fill up the array using those numbers. Write a program to determine the 2 largest integers in the array, and print those numbers. Hint: You can declare integer variables “largest” and “secondLargest” to keep track as you make comparisons using the if...elseif statement. The following is an example of how your program should look when...
Unix Create a script that will declare an array and assign four values from the command...
Unix Create a script that will declare an array and assign four values from the command line. 1. Use these four values - Paul, Ringo, George, John, - in that order 2. Display the content of the array, displaying the values in this format and this order The first array value is "John" The second array value is "Paul" The third array value is "George" The fourth array value is "Ringo"
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write...
C++ please 1. Randomly assign integers in the range of 1-100 to a two-dimensional array. Write a program that finds the average value of the rows and the average value of the columns. Display the averages. 2. Create an array of randomly generated numbers in any range. Write a function that takes the array as an argument and returns an array that consists of only the even numbers in the original array. Use the function in a program. 3. Create...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT