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

  • Home
  • SEARCH
  • 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 8881063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:16:21+00:00 2026-06-14T20:16:21+00:00

I am trying to make a simple program which basically reverse the sequence of

  • 0

I am trying to make a simple program which basically reverse the sequence of the vector. In other words after the program is ran, x[0] should be equal to 5. The output which I am currently getting is 5234. The 1 goes missing somewhere. I did include the <vector> header. Thanks in advance.

int main()
{
    vector<int> x(5);
    x[0] = 1;
    x[1] = 2;
    x[2] = 3;
    x[3] = 4;
    x[4] = 5;
    for(int z = 0; z < x.size()-1; z++)
    {
        int temp = x[x.size() - (1+z)];
        x[x.size() - (1+z)] = x[z];
        x[z] = temp;
    }
    for(int s = 0; s < x.size() - 1; s++)
    {
        cout << x[s] << endl;
    }
    return 0;
}
  • 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-14T20:16:22+00:00Added an answer on June 14, 2026 at 8:16 pm

    Take a step back a minute. What do you think this will do if I just put in swap (pseudo code)

    for( elt1 = begin, elt2 = end-1; elt1 != end; ++elt1, --elt2 )
    {
        iter_swap( elt1, elt2 );
    }
    

    This will not reverse your collection. Or rather it will, but will then reverse it back again leaving you where you started. Say your collection has 4 elements:

    swap( 0, 3 )
    swap( 1, 2 )
    swap( 2, 1 )
    swap( 3, 0 )
    

    So your terminating condition is wrong, you should only swap halfway…

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

Sidebar

Related Questions

I am trying to make a simple program which involves a tableView. When I
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
I am trying to make a simple program to put data from a database
I am trying to make a simple program with SFML. So far I have
I am trying to make a simple program that could open a new activity
I've been trying to make a simple program that takes the user input, until
I'm new to iPhone programming, and I'm trying to make a simple program without
I am trying to make a jar of a simple inventory program using ant
i'm trying to make a simple Java program to open an existing word-document, change

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.