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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:24:30+00:00 2026-05-20T07:24:30+00:00

I have an assignment to merge two sorted vectors into a third sorted vector.

  • 0

I have an assignment to merge two sorted vectors into a third sorted vector. I’m sure the solution to this problem is pretty simple, but my brain is fried at the moment and could use your help.

Basically vectors A and B have a size of 3. They will hold integers such as 1, 2, 3 and 4, 5, 6 respectively. I can’t seem to get the syntax of my while loop correctly. I’ve tried making it a do/while loop, putting parentheses around the cases, and a few other things. It just doesn’t seem to be reading the part after the &&.

The first for loop just makes the R vector have the right size. And the second for loop just displays the values of the R vector. The result should print out from 1-6, but I’m only seeing 1-3.

Any help would be appreciated!

void::combine(vector<int> A, vector<int> B, vector<int> R) {
int ia = 0, ib = 0, ir = 0;

for (int i = 0; i < A.size() + B.size(); i++) {
    R.push_back(0);
}

while (ia != A.size() && ib != B.size()) {
        if (A[ia] < B[ib]) {
             R[ir] = A[ia];
             ia += 1;
        }
        else {
            R[ir] = B[ib];
            ib += 1;
        }
        ir += 1;
}

for (int i = 0; i < R.size(); i++) {
    cout << "L3[" << i << "] = " << R[i] << endl;
}

}
  • 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-20T07:24:30+00:00Added an answer on May 20, 2026 at 7:24 am

    First of all, this smells like a classic merge sort or a piece of it:
    http://en.wikipedia.org/wiki/Merge_sort

    The objective is to examine elements of the first vector, A, to elements of vector B, and append the elements to a resulting vector R, in order.

    Thus if A[i] < B[j], append A[i] to R. With std::vector, there is no need to load the result vector with zeros before starting. See std::vector::push_back().

    The following is untested code:

    void Sort_Vector(const std::vector& A, const std::vector& B, std::vector& result)
    {
      unsigned int index_a = 0;  // Point to first element in first vector.
      unsigned int index_b = 0;  // Point to first element in second vector.
      result.clear();            // Clear all elements, so size == 0.
    
      while ((index_a < A.size()) && (index_b < B.size())
      {
         if (A[index_a] < B[index_b])
         {
             result.push_back(A[index_a]);
             ++index_a;
         }
         else // B[index_b] <= A[index_a]
         {
             result.push_back(B[index_b]);
             ++index;
         }
      }
    
      // Append any remaining elements to the result vector.
      // Only one of the two loops should be executed,
      //    the other will fail the comparison expression
      //    and not execute.
      for (; index_a < A.size(); ++index_a)
      {
         result.push_back(A[index_a]);
      }
      for (; index_b < B.size(); ++index_b)
      {
         result.push_back(B[index_b]);
      }
      return;
    }
    

    Notice the difference between my function’s signature (parameters) and yours. I’m passing the source vectors as constant data and the result vector is passed by reference which enables my function to modify the caller’s result vector.

    Also, I’m lazy and using the std::vector::push_back() method. I believe this is more readable than assigning to an element in the result vector. The push_back method implies that I am appending to the vector. The assignment route may catch readers off-guard if they forget that a vector will expand in order resolve the assignment.

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

Sidebar

Related Questions

No related questions found

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.