-1
Input: 30 30 30 40 40 50 50 4 4 4 4
output: 3 30 2 40 2 50 4 4

I have to accept these integer numbers and display the count of each unique number with the number beside the count.

I honestly don't know what condition to give to accept only till the last integer 4 of the input and not accept infinitely or some garbage values. Because all test cases do not have the same number of values as input. Please explain what function/logic I need to use to just accept the finite numbers of integers.

I used a for loop that accepted and stored values in a vector for 99 times, because the question said that the maximum number of integers that I can accept is 99. But for the above test case, 11 required integers are accepted and the rest 87 are garbage/last integer values...

It accepted 30 30 30 40 40 50 50 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 .........(till the 99th loop)

(I would've attached a screenshot of what I did if I could. I didn't do that because, 1) I was asked to code this in a contest so I don't remember the exact question as we were not allowed to bring our rough sheets out with us. 2) I couldn't make a decent attempt to solve it as I didn't even know how to accept the integers without accepting garbage values. Sorry for the uncertainty!)

Astera
  • 23
  • 3
  • 1
    Generally input is "signaled". If this is for a programming challenge then the stream will go into a "end of input" state that will stop an input loop for you. Have a look at this Q&A pair for the technique and what happens: https://stackoverflow.com/questions/50608176/read-unknown-number-of-inputs-in-c – NathanOliver Aug 05 '19 at 18:03
  • 2
    There are numerous ways to validate input in c++. At least show some effort of what you tried already and why it fails. – πάντα ῥεῖ Aug 05 '19 at 18:04
  • Define ***random values*** as you think it applies tor your application. – R Sahu Aug 05 '19 at 18:04
  • How do you determine if a value is random or not? – Thomas Matthews Aug 05 '19 at 18:05
  • ***Because all test cases do not have the same number of values as input.*** That is where you will need a `std::vector` instead of a fixed sized array. – drescherjm Aug 05 '19 at 18:16
  • Define "random values". –  Aug 05 '19 at 18:26
  • Please provide the code you have developed for solving this. We can help you improving and fixing the code but we are not gonna write the whole code for you. – Veikko Aug 05 '19 at 18:27
  • https://stackoverflow.com/questions/57356714/how-to-detect-different-letters-and-numbers-in-a-string –  Aug 05 '19 at 18:53

1 Answers1

1

You can read the numbers in a string and then extract each number from the string using the standard string stream std::istringstream declared in header <sstream>.

Here is a demonstrative program.

#include <iostream>
#include <string>
#include <map>
#include <sstream>

int main()
{
    std::string input;

    std::cout << "input: ";
    std::getline( std::cin, input, '\n' );

    std::map<int, size_t> m;

    int value;

    for ( std::istringstream is( input ); is >> value; )
    {
        ++m[value];
    }

    std::cout << "output: ";
    for ( const auto &p : m ) std::cout << p.first << ' ' << p.second << ' ';
    std::cout << '\n';
}

Its output is

input: 30 30 30 40 40 50 50 4 4 4 4
output: 4 4 30 3 40 2 50 2
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Yes! This is exactly what I needed! I didn't know about std string stream header and it's function. Thank you very much @Vlad ! But can you explain what 'const auto' does here?? I didn't exactly understand that.. – Astera Aug 07 '19 at 18:11