Question

Problem: Write a program that generates random numbers between 1 and 10 and fill an array...

Problem: Write a program that generates random numbers between 1 and 10 and fill an array of size 20 with them. Have your program sort the array then output in order the number of occurrences of each random number generated. Use the STL sort function to sort your array.

How to use the STL sort:

#include <algorithm>

...

int a[20]

...

sort(a,a+20);

Example Output:

This program generates random numbers and tabulates the results

Original List Sorted:

a[ 0] 1

a[ 1] 1

a[ 2] 1

a[ 3] 2

a[ 4] 2

a[ 5] 3

a[ 6] 6

a[ 7] 6

a[ 8] 6

a[ 9] 7

a[10] 8

a[11] 8

a[12] 8

a[13] 9

a[14] 9

a[15] 9

a[16] 9

a[17] 10

a[18] 10

a[19] 10

N Count

1: 3

2: 2

3: 1

4: 0

5: 0

6: 3

7: 1

8: 3

9: 4

10: 3

Try Again? (1 = yes, 0 = exit)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
int a[20];
int i, j, k=0, cnt=0;
int freq[10] = {0};

//Initializing random seed
srand (time(NULL));

//Generating random numbers
for(i=0; i<20; i++)
{
a[i] = (rand() % 10) + 1;
}

//Sorting array
sort(a, a+20);

//Printing sorted array
for(i=0; i<20; i++)
{
cout << "a[" << i << "] " << a[i] << endl;
}

//Number of occurrence of each number
for(i=1; i<=10; i++)
{
//Iterating over array
for(j=0; j<20; j++)
{
//Updating frequency
if(a[j]==i)
{
freq[i-1] += 1;
}
}
}

//Printing results
for(i=1; i<=10; i++)
{
cout << endl << i << ": " << freq[i-1];
}
cout << endl;

return 0;
}
______________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Problem: Write a program that generates random numbers between 1 and 10 and fill an array...
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
  • In JAVA, write a program that generates 10 random numbers between 0 and 9. Use count...

    In JAVA, write a program that generates 10 random numbers between 0 and 9. Use count arrays for each number. A sample output would be: The 10 random numbers are: 5 7 1 9 0 0 1 2 3 4 0 occurs 2 times 1 occurs 2 times 2 occurs 1 time 3 occurs 1 time 4 occurs 1 time 5 occurs 1 time 6 occurs 0 times 7 occurs 1 time 8 occurs 0 times 9 occurs 1 time

  • Description: Write a complete C++ program that will do the following: 1. Declare an array of...

    Description: Write a complete C++ program that will do the following: 1. Declare an array of 30 elements 2. Set the array to random numbers between 0 to 100 inclusive. Don't seed the random number generator. 3. Output the array. 4. Sort the array in ascending order using the selection sort. 5. Output the sorted array. Required IO: Array by F. Last Original: 1 5 ... 2 10 + Sorted: 1 2 ... 5 10 ( +

  • C++ programming please Write a program that will display random numbers in order from low to...

    C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...

  • Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the...

    Create a CodeBlocks project "HW 9" Write the code to ask the user to enter the size of an array. Then create an integer array of that exact size. Ask the user to enter a maximum value and then write a loop to fill the array with random numbers with value in the range of 1 to the maximum value. For example, if the maximum value is 100, random numbers must have value 1 to 100 inclusive. Input size of...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • Create a program that creates an array of 500 random numbers between -10 and 10. Write...

    Create a program that creates an array of 500 random numbers between -10 and 10. Write several functions: Function 1 - prints the array with a certain number of elements per line. Prototype: void printArray(int array[], int numElements, int numPerLine, ostream &os) Function 2 prints only the array elements that are evenly divisible by an input number. Prototype: void printDivBy(int array[], int numElements, int numPerLine, int divBy, ostream &os) Function 3 takes the input array and returns the maximum, the...

  • C++ language Write a program that 1. generates a random integer between 100 and 1000, call...

    C++ language Write a program that 1. generates a random integer between 100 and 1000, call the number N 2. reads a file name from the keyboard 3. opens a file for output using the file name from step 2 4. generates N random numbers between 1 and 100 and writes them to the file from step 3 5. closes the file 6. opens the file from steps 3, and 4 for input 7. reads the numbers from the file...

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • C++ Write a program that generates and prints 24 random values using an array and the...

    C++ Write a program that generates and prints 24 random values using an array and the rand () function. Implement an algorithm to find the maximum and a minimum number of random numbers generated. Use the header "#include <ctime>", "#include <cstdlib>" in addition to the usual header: "#include <iostream>"

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