Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:29:42+00:00 2026-06-08T04:29:42+00:00

There is an array with 9 spaces(elements), where those 9 numbers will be stored

  • 0

There is an array with 9 spaces(elements), where those 9 numbers will be stored which are entered by user.

How would you make sure that they enter only those 9 numbers and that they are not the same?
Finally cout(print) them from less to greater?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:29:43+00:00Added an answer on June 8, 2026 at 4:29 am

    Here’s a solution that ensures numbers in the range [0,9], and ignores duplicates:

    #include <algorithm> //if using copy printing
    #include <iostream> //for cin and cout
    #include <iterator> //if using copy printing
    #include <set> //for set
    
    CHOOSE A METHOD BELOW AND ADD THE CORRESPONDING INCLUDE
    
    int main() {
        std::set<int> nums; //here's the array
    
        std::cout << "Please enter nine different numbers." 
                     "Duplicates will be ignored.\n";
    
        //ADD THE NEXT PART OF THE METHOD, THE DECLARATION
    
        do {
            std::cout << "Enter the next number: ";
    
            //ADD THE FINAL PART, GETTING INPUT AND INSERTING IT INTO THE SET
        } while (nums.size() < 9); //do this until you have 9 numbers
    
        std::cout << "The numbers you entered, in order, are:\n";
    
        //C++11 printing
        for (const int i : nums)   
            std::cout << i << ' ';
    
        //C++03 printing using copy
        std::copy (nums.begin(), nums.end(), 
                   std::ostream_iterator<int> (std::cout, " "));
    
        //C++03 printing using an iterator loop
        for (std::set<int>::const_iterator it = nums.cbegin(); //this was to
                                           it != nums.cend();  //eliminate the
                                           ++it)               //scrollbar
            std::cout << *it << ' ';
    }
    

    First Method: (better for a wider range)

    #include <limits> //for numeric_limits
    ...
    int temp; //this holds the current entry
    ...
    //works better when you get out of the range 0-9
    while (!(std::cin >> temp) || temp < 0 || temp > 9) { 
    //body executes if input isn't an int between 0 and 9
    
        //clear bad input flag
        std::cin.clear(); 
    
        //discard bad input
        std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 
    
        //prompt for new number (could also add that you're ignoring to beginning)
        std::cout << "Invalid number. Please enter a new one: "; 
    }
    
    //insert the valid number, duplicates ignored, automatically sorted
    nums.insert (temp); 
    

    Second Method: (better for a 0-9 range)

    #include <cctype> //for isdigit
    ...
    char temp; //holds current entry
    ...
    //suitable for 0-9 range 
    do {
        std::cin >> temp; 
        if (!std::isdigit (temp))
            std::cout << "Invalid number. Please enter a new one: ";
    while (!std::isdigit (temp));
    
    nums.insert (temp - '0'); //add the integer value of the character processed
    

    The key here is std::set, which only allows unique entries, and automatically sorts the elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is an array of user states stored in the session. This works: <?php
I am making a page in which there will be array of images. I
Given an array with N elements. We know that one of those elements repeats
What is the appropriate function that shows how many are there in an array?
Is there any way to find the number of elements in an array in
Suppose you have an unsorted array, how will you find 2 equal elements such
Is there any speed/space/general performance gains in using array based implementation of a binary
Scenario: If there is an array of integers and I want to get array
I have a really tough situation in here. There is an array of these
I'm trying to write a snake game. It's pretty simple, there's an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.