Question

Write code that scans an array of random integers, whose value is between 0 and 1000,...

  1. Write code that scans an array of random integers, whose value is between 0 and 1000, and prints out the most common integer in the array. (HINT: how can we store a count of each integer that we may encounter when we scan our array? Provide a main method to make sure that the code works properly.

Please keep it as simple as possible, there is no need for a random number generator and we aren't supposed to use functions. The language is C++, thank you in advance.

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

using namespace std;

int main() {
    int arr[1000];
    cout << "How many numbers do you want to enter? ";
    int size;
    cin >> size;
    cout << "Enter " << size << " integers(0-1000): ";
    for (int i = 0; i < size; ++i) {
        cin >> arr[i];
    }
    int counts[1001] = {0};
    for (int i = 0; i < size; ++i) {
        counts[arr[i]]++;
    }
    int mostCommon = 0;
    for (int i = 0; i < 1001; ++i) {
        if (counts[i] > counts[mostCommon])
            mostCommon = i;
    }
    cout << "Most common number is " << mostCommon << " and it appears " << counts[mostCommon] << " times." << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write code that scans an array of random integers, whose value is between 0 and 1000,...
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
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