Question

This assignment is about array operations. You need to count a specific items in an array. Steps to follow are here: . Create
The array with 1000 random items is ready... Enter a number: -4 Frequency of -4: 54 Enter a number: 4 Frequency of 4: 15 Ente

write c++ program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program to generate random number array and find frequency of a specific number based on user input is given below. It has the following features as per given conditions in the question:

  • It creates and array, arr with 1000 items of random numbers between -100 and 100 (including the extremes, -100 and 100).
  • It then asks the user for an input number and stores it in variable K.
  • It then searches the earlier initialized array arr for the number K and evaluates its frequency and prints it.
  • It continues asking the user for a number and prints its frequency until user enters an out-of-range number (i.e. <-100 or >100), in which case it shows a message and exits the program.

C++ code for this program as well as its screenshot and sample output is copied below. Comments have been added in each code block for better comprehension.

#include <iostream>

using namespace std;

int main()
{
    //Declare variables
    int arr[1000];
    int min = -100;
    int max = 100;
    int K, freq;
    
    //Initialize our random number seed
    srand((unsigned) time(0));
    
    //Use for loop to initialize our array with 1000 random numbers
    for(int i=0; i<1000; i++)
        arr[i] = rand()%(max-min + 1) + min;
        
    //Display a message indicating our array is ready
    cout<<"The array with 1000 random items is ready..."<<endl;
    
    //Use an infinite while loop to take user input and show frequency
    while(1)
    {
        cout<<"Enter a number: ";
        cin>>K;
        if(K<-100 || K>100)
        {
            //If user inputs an out-of-range number, exit
            cout<<K<<" is out-of-range number, bye...";
            break;
        }
        else
        {
            //Set freq=0 for every loop
            freq = 0;
            //Use for loop to evaluate num frequency
            for(int i=0; i<1000; i++)
            {
                if(arr[i] == K)
                    freq++;
            }
            //Show freq value to user and continue with next input
            cout<<"Frequency of "<<K<<" is: "<<freq<<endl;
        }
    }
    
    //End of program
    return 0;
}

Code Screenshot:

main.cpp 1 #include <iostream> 2| 3 using namespace std; 4 5 int main() 6 { 7 //Declare variables 8 int arr[1000]; 9 int min

I/Use an infinite while loop to take user input and show frequency while(1) { cout<<Enter a number: cin>>K; if(K<-100 || K>1

Sample Run Output:

The array with 1000 random items is ready...
Enter a number: 3
Frequency of 3 is: 9
Enter a number: -12
Frequency of -12 is: 2
Enter a number: 19
Frequency of 19 is: 4
Enter a number: 197
197 is out-of-range number, bye...

input The array with 1000 random items is ready... Enter a number: 3 Frequency of 3 is: 9 Enter a number: -12 Frequency of -1

Hope this helps.

Add a comment
Know the answer?
Add Answer to:
write c++ program This assignment is about array operations. You need to count a specific items...
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
  • This assignment is about array operations. You need to count a specific items in an array....

    This assignment is about array operations. You need to count a specific items in an array. Steps to follow are here: • Create an array with 1000 items and fill it with random numbers from the range of -100, 100 (including - 100 and 100). . In your program asks user to enter a number, K. • Search this number and print the frequency (how many) of this K is found in the array. • Ask another number, and so...

  • code must be c++ thank you This assignment is about array operations. You need to count...

    code must be c++ thank you This assignment is about array operations. You need to count a specific items in an array. Steps to follow are here: • Create an array with 1000 items and fill it with random numbers from the range of -100, 100 (including - 100 and 100). . In your program asks user to enter a number, K. • Search this number and print the frequency (how many) of this K is found in the array...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Write in C# Programming Language.   - count =0; - Create a 2d array "items". string[,] items...

    Write in C# Programming Language.   - count =0; - Create a 2d array "items". string[,] items = new string[100, 4]; - Create a do while loop, when user enter "0", stop the loop. - User enter item name, price, quantity, save these info and subtotal to array. - increase "count++" -conver price(decimal, Convert.ToDecimal() ) and quantity(int) to do mulplication for subtotal - create a for loop (with count ) to cycle through the "items", display item name, price, quantity, and...

  • Write a C or C++ program a pseudo C program (based on chapter 2 in the...

    Write a C or C++ program a pseudo C program (based on chapter 2 in the book) and then use these programs to develop a MIPS program that gets an integer input from the user, and based on the input returns the minimal or maximal value stored in a static initialized array. The following are detailed instructions: 1) The program generates a static initialized array with 10 integers (using the .word directive). 2) The program prompts the user to enter...

  • in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array...

    in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array filled up with random positive integer number ranging from 15 to 20, and display it on the screen. After the creation and displaying of the array, the program displays the following: [P]osition [Reverse, [A]verage, (Search, [Q]uit Please select an option: Then, if the user selects: -P (lowercase or uppercase): the program displays the array elements position and value pairs for all member elements of...

  • (C programing, Not C++) Write a program in C that asks the user to input 10...

    (C programing, Not C++) Write a program in C that asks the user to input 10 numbers. Store these numbers in an array. Then ask the user to input a single number. Your program should execute a linear search on the array, trying to find that number. If the number exists in the array, have the program print out which position/element the number was found at. If the target number is not in the array, have the program print out...

  • Write a computer program that prompts the user for one number, n for the number of...

    Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers (Make sure...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

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