Question

In: Computer Science

Declare an integer array of size 20 and assign the array with 20 randomly generated integers...

  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;

}

Please Use C++ to solve

Solutions

Expert Solution

#include<iostream>
using namespace std;
const int NUM = 20;

void swap(int *a, int *b)
{
        int temp = *a;
        *a = *b;
        *b = temp;
}

int main()
{
        int arr[NUM], i, j;

        //assigning the array with 20 randomly generated integers in the range [1, 100].
        for(i = 0; i < NUM; i++)
                arr[i] = rand() % 100 + 1;

        cout<<"Array before shuffling\n";
        for(i = 0; i < NUM; i++)
                cout<<arr[i]<<" ";
        cout<<"\n";

        j = -1;
        i = 0;

        //shuffling the array
        while(i < NUM)
        {
                if(arr[i] < 50)
                {
                        j++;
                        swap(&arr[i], &arr[j]);
                }
                i++;
        }

        //printing the shuffled array
        cout<<"Array after shuffling\n";
        for(i = 0; i < NUM; i++)
                cout<<arr[i]<<" ";
        cout<<"\n";
        return 0;
}

IF YOU LIKED THE ANSWER, PLEASE UPVOTE!!!


Related Solutions

In C++ Declare an integer array of size 20 and assign the array with 20 randomly...
In C++ 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])...
Design a program that uses an array to store 10 randomly generated integer numbers in the...
Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: 1. Display 10 random numbers stored in the array 2. Compute and display the largest number in the array 3. Compute and display the average value of all numbers 4. Exit The options 2...
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.
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...
Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new...
Using c++, 1: Create a vector of 20 randomly generated integers, then 2. Create a new vector that will only store the even numbers from the original vector. 3. Display the original vector and the vector of even integers to the console.
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"
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT