Question

Write a C++ application to find the smallest number in the list which has the maximum...

Write a C++ application to find the smallest number in the list which has the maximum number of repetitions.

Ex.

8,11,2,2,3,4,6,3,3,9,2,3,3,15

The smallest number in the list is 3 with maximum repetitions of 4.

Show the output with the number and its repetition count.

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

using namespace std;

int maxRepetitions(int *arr, int size, int &maxCount) {
    int num = arr[0];
    maxCount = 0;
    for (int i = 0; i < size; ++i) {
        int count = 0;
        for (int j = 0; j < size; ++j) {
            if (arr[i] == arr[j])
                count++;
        }
        if (count > maxCount || (count == maxCount && arr[i] < num)) {
            num = arr[i];
            maxCount = count;
        }
    }
    return num;
}

int main() {
    int num, maxCount;
    int arr[] = {8, 11, 2, 2, 3, 4, 6, 3, 3, 9, 2, 3, 3, 15};
    num = maxRepetitions(arr, sizeof(arr) / sizeof(int), maxCount);
    cout << "The smallest number in the list is " << num << " with maximum repetitions of " << maxCount << "." << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a C++ application to find the smallest number in the list which has the maximum...
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
  • Write a function to find the smallest odd number in a list. The function takes a...

    Write a function to find the smallest odd number in a list. The function takes a list of numbers (each number is greater than or equal to 0) as an input. The function outputs a tuple, where the first number in the tuple is the smallest odd number and the second number in the tuple is the number of occurrences of that number. For example, if the input is [1, 4, 7, 3, 5, 2, 1, 3, 6] then the...

  • in C language Write a program to find the element of an array, which occurs maximum...

    in C language Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3

  • (Find the smallest element) use pointers to write a function that finds the smallest element in...

    (Find the smallest element) use pointers to write a function that finds the smallest element in an array of integers. Use {1,2,4,5,10,100,2,-22} to test the function. using the following header. int minindex(double* list, int size) example output: array size: 8 number 1: 1 number 2: 2 number 3: 4 number 4: 5 number 5: 10 number 6: 100 number 7: 2 number 8: -22 smallest element is: -22 C++ only.

  • Write a program called SmallestLargest.java which outputs the biggest and smallest numbers in a list of...

    Write a program called SmallestLargest.java which outputs the biggest and smallest numbers in a list of numbers entered by the user. Ask the user for a terminating value which should be entered again when they are done inputting the list of numbers. First output the biggest number and then the smallest number. There must be at least 1 number in the list. YOU MUST USE THE IO MODULE FOR INPUT/OUTPUT. Report bad input via IO.reportBadInput() and exit on error. Example:...

  • 4.12 LAB: Smallest number Write a program whose inputs are three integers, and whose output is...

    4.12 LAB: Smallest number Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 I have to write this in python, having a hard time deciding how to approach this. Thank you!

  • In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a...

    In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...

  • In C++: Write a program that reads a list of integers, and outputs the two smallest...

    In C++: Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The list is preceded by an integer indicating how many numbers are in the list. If the input is: 5 10 5 3 21 2, the output is: 2 3 To achieve the above, first read the integers into a vector.

  • Find Maximum Experiment with the Find Maximum problem here 1. Describe the strategy for finding the...

    Find Maximum Experiment with the Find Maximum problem here 1. Describe the strategy for finding the maximum number in a list of 8 items 2. Count the number of comparisons to find the maximum element (number) in a list of 15 items 3.Count the number of comparisons to find the maximum element (number) in a list of 25 items 4. Determine a formula for the number of comparisons to find the maximum number in a list of any size Use...

  • Please write in C++ A list of 6 integers is input into vector listints. Complete the...

    Please write in C++ A list of 6 integers is input into vector listints. Complete the program to copy only the negative integers to a new vector listNeglnts. Output the number of negative elements, and the negatives list. Ex: For input 5-209-66-4, output is: 3 -2 -66 -4

  • Write a program that can find the smallest element in a vector. Given a vector such...

    Write a program that can find the smallest element in a vector. Given a vector such as [1 2 -2 7 -8 9 3 1], you program has to find its smallest element and output it in the command window. In the vector above, the smallest element would be the -8. Your program should work for any vector with any size. the answer should be answered by matlab.

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