Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to add a new element and move some others in my vector in this program \/, but vector elements do not want to change at all.

C++
#include <iostream>
#include <deque>

using namespace std;

int main()
{
	int n;
	cin >> n;
    vector <int> v;
    for (auto i = 0, j = 0; i < n; i++)
    {
    	cin>>j;
        v.push_back(j);
    }
    //
    int max = 0, ind = 0;
    for (auto i:v)
    	if (max <= i)
        {     	
        	max = i;
        	ind = i;
        }
    //
    v.push_back(0);
    for (auto i = n, j =0; i > ind+1; i--)
    {    	j = v[i]; -----this code does not work
          v[i] = v[i-1];-------this code does not work
          v[i-1] = j;----------this code does not work
    }
    v[ind+1] = v[ind];--------- this code does not work
    //
   for (auto i:v)
    	      cout << i << ' ';
  	cout << endl;
	return 0;
}

I know it may seem hilarious, but it does!
Example of input:
3
3 1 1
Expected output:
3 3 1 1
Real output:
3 1 1 0

What I have tried:

read several articles about vector type on the Net;
tried to use swap() function, but it does not work either;
tried to use deque or queue instead of vector - same result;
i think that that code must work and do not know why it doesn`t.
Posted
Updated 17-Nov-17 16:29pm
v4
Comments
Richard MacCutchan 17-Nov-17 13:43pm    
Step through the code with the debugger and see why.

1 solution

Quote:
i think that that code must work and do not know why it doesn`t.

Launch the debugger, on every step, check that everything is what it should.
C++
for (auto i = n, j =0; i > ind+1; i--)

Are you using the maximum value in vector as the ending position in loop ?
Unless this is carefully planed, you are in for great troubles.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Comments
Member 13527256 18-Nov-17 7:20am    
Thank you - I was working late at night yesterday and forgot about debugger) Actually, the problem was here:
for (auto i:v)
if (max <= i)
{
max = i;
ind = i;
}
ind should be increased by 1 on each iteration here.
Patrice T 18-Nov-17 7:33am    
nice to hear it.
If problem is solved, accept my solution or post your own solution ans accept it.
it will close the question, so everyone will know it is solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900