reversing number

Hi everyone. I got a task to reverse number from index 2 to 5 for each row. I had try with the code but the solution wont appear. I dont know either i do right or wrong. Hope someone can help me. Thank you.

Input file:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
3 4 5 6 7 8 9 10 1 2
6 5 4 3 2 1 7 8 9 10
5 7 9 1 3 4 6 10 2 8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 #include "stdafx.h"
#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std; 

const int n = 10;
const int m = 5;
vector< vector <int>> route (m, vector <int> (n,0));
  
int main() 
{ 
    ifstream inpData("myData.txt");

	//assign route into array                    
	for (int i = 0; i < m; i++ )
	{
		for (int j = 0; j < n; j++)
		{
			inpData >> route[i][j];
			//cout << "route [" << i << "][" << j << "] = " << route[i][j] << endl;
			cout << setw(3) << route[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	//close input files
	inpData.close();

	for (int row = 0; row < m; row++)
	{
		cout << "Reverse only from index 2 to 5 for each row:\n"; 
		// Reversing elements from index 2 to index 5 
		reverse(route.begin() + route[row][2], route.begin() + route[row][5]); 
	}
	cout << endl;

	// Displaying solution
	for (int i = 0; i < m; i++ )
	{
		for (int j = 0; j < n; j++)
		{
			inpData >> route[i][j];
			cout << setw(3) << route[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	system("pause");
 
}


The output should be:
1 2 6 5 4 3 7 8 9 10
2 3 7 6 5 4 8 9 10 1
3 4 8 7 6 5 9 10 1 2
6 5 1 2 3 4 7 8 9 10
5 7 4 3 1 9 6 10 2 8
Replace line 40 by:
reverse(route[row].begin() + 2, route[row].begin() + 5);
Don't hesitate to ask if its meaning is unclear to you.
Hi nuderobmonkey, i got it. Thanks for your help :)
Oh, I see that I made a mistake. Line 40 should rather be:
reverse(route[row].begin() + 2, route[row].begin() + 6);
Because the last iterator needs to be one beyond the last element to reverse.
Thanks nuderobmonkey. If you don't mind. I would like to ask another question also regards to reverse. I had try with the code but its weird. sometimes i could get the answer but when i run for another time error occurred. If not be mistaken, I think it related to
srand(time(null))
. Because when I disable
srand(time(null))
. I could get the answer but when I include those, error occurred. I don't know how to solved it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"
#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std; 

const int n = 10;
const int m = 5;
vector< vector <int>> route (m, vector <int> (n,0));
  
int main() 
{ 
	//srand(time(null));
	//srand((unsigned int)time(NULL));

    ifstream inpData("myData.txt");

	//assign route into array                    
	for (int i = 0; i < m; i++ )
	{
		for (int j = 0; j < n; j++)
		{
			inpData >> route[i][j];
			//cout << "route [" << i << "][" << j << "] = " << route[i][j] << endl;
			cout << setw(3) << route[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	//close input files
	inpData.close();

	for (int row = 0; row < m; row++)
	{

		// Generate two different random value from col to col + 14, inclusive.
        int i1, i2;
        do
        {
            i1 = rand() % 10;
            i2 = rand() % 10;
        }
        while (i1 == i2);

        // Ensure i1 is less than i2; if not, swap them.
        if (i1 > i2)
        {
            int temp = i1;
            i1 = i2;
            i2 = temp;
        }
        
        cout << "For row " << row << ", positions " << i1 << " and " << i2 << " were chosen.\n";

		// ============= Reverse & Swap ============"; 

		// Reversing elements from index (i1 + 2) to index (i2 - 1) 
		reverse(route[row].begin() + (i1 + 2), route[row].begin() + i2); 

		// Swap next value at position i1 with value at position i2.
		swap(route[row][i1 + 1], route[row][i2]);
	}
	cout << endl;

	// Displaying solution
	for (int i = 0; i < m; i++ )
	{
		for (int j = 0; j < n; j++)
		{
			inpData >> route[i][j];
			cout << setw(3) << route[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	system("pause");
 
}
Last edited on
Could you describe precisely what error you get?

What are the values of i1 and i2, and what output do you get vs. what do you expect?
Could you describe precisely what error you get?

Debug assertion failed.
Expression: invalid iterator range.

What are the values of i1 and i2

i1 and i2 are two different random value positions from col 0 until col 9 for each row.

After i got two different random value, I do reverse number from index (i1 + 2) to index (i2 - 1) for each row.

After i reverse, I do swap next value at position i1 which is (i1 + 1) with value at position i2.

what output do you get

If I included srand(time(null)), the solution won't appear.
If I disable srand(time(null)), the solution appear but the random value is same for each run.

what do you expect?

Input:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
3 4 5 6 7 8 9 10 1 2
6 5 4 3 2 1 7 8 9 10
5 7 9 1 3 4 6 10 2 8

For example, for row 0, position 1 and 8 were chosen.
i1 = position 1
i2 = position 8

1 2 3 4 5 6 7 8 9 10

The output should be
1 2 9 8 7 6 5 4 3 10

Hope you understand what i'm trying to explain.


Last edited on
Oh, that's weird. When I compile and run your program at my, I get the correct output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//#include "stdafx.h"
#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std; 

const int n = 10;
const int m = 1;
vector< vector <int>> route (m, vector <int> (n,0));
  
int main() 
{ 
	srand(time(nullptr));
	//srand((unsigned int)time(NULL));

    ifstream inpData("myData.txt");

	//assign route into array                    
	for (int i = 0; i < m; i++ )
	{
		for (int j = 0; j < n; j++)
		{
			inpData >> route[i][j];
			//cout << "route [" << i << "][" << j << "] = " << route[i][j] << endl;
			cout << setw(3) << route[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	//close input files
	inpData.close();

	for (int row = 0; row < m; row++)
	{

		// Generate two different random value from col to col + 14, inclusive.
        int i1, i2;
        do
        {
            i1 = 1; //rand() % 10;
            i2 = 8; //rand() % 10;
        }
        while (i1 == i2);

        // Ensure i1 is less than i2; if not, swap them.
        if (i1 > i2)
        {
            int temp = i1;
            i1 = i2;
            i2 = temp;
        }
        
        cout << "For row " << row << ", positions " << i1 << " and " << i2 << " were chosen.\n";

		// ============= Reverse & Swap ============"; 

		// Reversing elements from index (i1 + 2) to index (i2 - 1) 
		reverse(route[row].begin() + (i1 + 2), route[row].begin() + i2); 

		// Swap next value at position i1 with value at position i2.
		swap(route[row][i1 + 1], route[row][i2]);
	}
	cout << endl;

	// Displaying solution
	for (int i = 0; i < m; i++ )
	{
		for (int j = 0; j < n; j++)
		{
			inpData >> route[i][j];
			cout << setw(3) << route[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl;

	//system("pause");
 
}

I made some changes because I need it to run on an unixoid os.
I had copy your changes and I try run. One error occurred.

error C2065: 'nullptr' : undeclared identifier

Actually, what is srand(time(nullptr)) ?

It is same with srand(time(null))
What compiler (and version) do you use?

nullptr is a void pointer to address 0 and is builtin keyword since C++11. You could replace this by NULL or 0.

I suggest reposting your program in the 'Windows Program' forum if the error still will resist.
What compiler (and version) do you use?

I'm using Visual Studio 2008.

I had replace with NULL but error still occurred as previous.
and
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

I suggest reposting your program in the 'Windows Program' forum if the error still will resist.


Okay i will post. Btw, thanks for your help. Really appreciated :)
null is defined in cstdlib among other places. check your includes, here is a link that may help..
It is often mistaken as a keyword, but its not really, its just a #define in that era of C++ at least...

https://en.cppreference.com/w/cpp/types/NULL

nullptr actually IS a keyword, but you don't have it, seeing as how your compiler is almost old enough to attend middle school...

I mean I see that you have cstdlib, and it should be in there... zero will work, if you can't find the issue, but a new compiler would do you a lot better.
Last edited on
Hi Jonnin, I had try the code on a new compiler which is Visual Studio 2017.

but the error still occurred.
Debug assertion failed.
Expression: Vector iterator range transposed

But when I change
reverse(route[row].begin() + (i1 + 2), route[row].begin() + i2);

to
reverse(route[row].begin() + i1, route[row].begin() + i2);
the code run well.

also if i change to
reverse(route[row].begin() + i1 + 1, route[row].begin() + i2)
the code also run well.
Topic archived. No new replies allowed.