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 7636647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:44:08+00:00 2026-05-31T07:44:08+00:00

this is my code about bubble sort. #include stdafx.h #include <iostream> #include <cstdlib> using

  • 0

this is my code about bubble sort.

#include "stdafx.h"

#include <iostream>
#include <cstdlib>

using namespace std;

void swap(int &a, int &b) {
    int * x = &a;
    int * y = &b;
    int tmp = * x;
    * x = * y;
    * y = tmp;   
}


int main()
{
    // INPUT
    int size;
    int i=0;
    int A[80];
    cout << "How many number in your list A ? ";
    cin >> size;
    for(i=0;i<size;i++) {           
        cout << " A[" << i << "] = " ;
        cin >> A[i];
    }

    // PRINT LIST
    cout << "This is your list number: ";
    cout << endl;
    for(int i=0; i<=size -1;i++) {
        cout << A[i] << " ";
    }


    // WHILE LOOP , continue if swapped;
    bool swapped = true;
    int pass=0;
    while(swapped == true) {
        swapped = false;
        // Increase Pass
        pass++;
        cout << endl << endl << "Pass " << pass << ":";
        // Loop size - Pass;
        for( i=1; i<=size - pass;i++) {
            // check if X > Y
            if(A[i-1] > A[i]) {
                // true, doing swap
                swap(A[i-1], A[i]);
                // set swapped to continue next loop
                swapped = true;
            }       
            // Print list again after sort;
            cout << endl;
            for(int i=0; i<=size -1;i++) {
                cout << A[i] << " ";
            }


        }               
    }

    // PRINT after sort;
    cout << endl << endl << "Your list after sort: ";       
    for(int i=0; i<=size -1;i++) {
        cout << A[i] << " ";
    }
    cout << endl;

    system("pause");
    return 0;
}

On this code,i must enter number of amount (size), and then enter each of A[i].
But I want to improve this code, can i don’t need to enter amount (size), and just cin the the whole A?

Like:

Please enter your list number: 5 1 4 2 8 [enter]

And I get the whole A[];

Just a idea after see first answer. I see, Vector can automatic resize, but if I change into vector, will have any way to enter 1 line ? I just got an idea, I enter a string of number: 1 2 3 4 5, then I enter. do C++ have any function to split by space, and then return back to an array or a vector ? in PHP, I just use $array = explode(” “,$string); >_<

Thanks your help, tried to read many article >_<

  • 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-05-31T07:44:09+00:00Added an answer on May 31, 2026 at 7:44 am

    What you should use instead of an array, is a vector. Arrays require you to know in advance how many elements will be stored, and if this number is unknown, you have to employ some rather involved memory copying once you need to exceed their predetermined capacity. Vectors do this for you under the hood.

    Here’s an example that basically performs what you ask, using a vector:

    std::vector<int> intList;
    std::string inputStr;
    std::cin >> inputStr;
    std::string subStr;
    for ( std::string::iterator _it = inputStr.begin(); _it != inputStr.end(); ++_it )
    {
        if ( *_it == ',' )
        {
            intList.push_back( atoi( subStr.c_str() ) );
            subStr.clear();
        }
        else
            subStr.push_back( *_it );
    }
    if ( subStr.size() > 0 )
        intList.push_back( atoi( subStr.c_str() ) );
    

    Now intList is populated with the integers you have entered, so long as each is separated by a comma.

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

Sidebar

Related Questions

The compiler complains about this code: HashMap<String,int> userName2ind = new HashMap<String,int>(); for (int i=0;
There are a lot of posts on here about this, I'm using this code
GCC complains about this code even though I compile with -std=c++11 flag, and my
Just wondering about this code below... when I turn off my internet connection and
There's something very unsatisfactory about this code: /* Given a command string in which
I am not very clear about this code outer is a class and the
I'm VERY confused as to why this code Html.ActionLink(About, About, Home, new { hidefocus
I would like to ask you about some advices about this code. It works,
according to this code i dont understand about NSClassFromString how did i know viewControllerName
When I run this code, About half-way through the concatenation loop, $xml becomes null

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.