reverse order

Hi anyone, I'm new to C++ programming. I got a task to reverse the numbers in a certain range. I don't have idea how to do it. Hoping for help from anyone. Thank you.

input file:
44 83 30 29 61 88 93 91 90 59 58 99 79 64 43
8 50 75 87 82 80 69 14 65 28 42 41 74 5 57
100 84 17 51 72 68 85 78 47 45 15 52 8 50 75
8 50 75 87 82 80 69 14 65 28 42 41 74 5 57
65 87 82 80 69 14 75 8 50 28 42 41 74 5 57
85 78 47 45 68 15 52 8 50 75 87 82 80 69 14
96 97 11 27 25 24 63 26 13 40 81 10 73 31 60
57 5 74 41 42 28 50 8 75 87 82 80 69 14 65
8 50 75 87 82 80 69 14 65 28 42 41 74 5 57
14 87 82 80 69 65 75 8 50 28 42 41 74 5 57

I need to reverse the numbers from (i1 + 2) to (i2 - 1) for each row.
i1 and i2 is the random value.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;

//declare functions
void input();		
void randomSwapReverse();

//declare variables & files
const int pop_size = 10;				
const int column_initialR = 15;		

vector< vector <int>> initial_route (pop_size, vector <int> (column_initialR,0));		

int i, j; 
 
void main() 
{
	input();
	randomSwapReverse();
	
}

void input()
{
	//open input files
	ifstream inpInitialRoute("Initial route 15.txt");	

	//assign initial routes into array                    
	for ( i = 0; i < initial_route.size(); i++ )
	{
		for ( j = 0; j < column_initialR; j++)
		{
			inpInitialRoute >> initial_route[i][j];
		}
	}
	
	//close input files
	inpInitialRoute.close();
}

void randomSwapReverse()
{
	srand(time(NULL));

	for (int i = 0 ; i < pop_size; i++)
    {
        for (int j = 0; j < column_initialR; j++)
            cout << setw(3) << initial_route[i][j] << ' ';
        cout << endl;
    }
    cout << endl;

    for (int i = 0; i < pop_size; i++)
    {

        // Generate two different random value from col to col + 14, inclusive.
        int i1, i2;
        do
        {
            i1 = rand() % 15;
            i2 = rand() % 15;
        }
        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 " << i << ", positions "
             << i1 << " and " << i2 << " were chosen.\n";

                //Swap next value at position i1 with value at position i2.
		swap(initial_route[i][i1 + 1], initial_route[i][i2]);

                //Reverse values from position (i1 + 2) to (i2 - 1).
		reverse(initial_route[i][i1 + 2], initial_route[i][i2 - 1]);

    }
	cout << endl;

    for (int i = 0 ; i < pop_size; i++)
    {
        for (int j = 0; j < column_initialR; j++)
            cout << setw(3) << initial_route[i][j] << ' ';
        cout << endl;
    }

    cout << endl;
	system("pause");
}


Thank for your help.
What are you having trouble doing?
> srand(time(NULL));
This should be at the start of main.
Mostly to prevent you from calling it more than once, which does nothing to increase the randomness of your data.
It makes no difference in this case, but half of writing good code is making the right choice out of habit.

Lines 54 to 60, and lines 94 to 100 are identical code.
Move them out into a separate 'print' function you can call.

> I need to reverse the numbers from (i1 + 2) to (i2 - 1) for each row.
...
> // Ensure i1 is less than i2; if not, swap them.
Your loop only ensures that i1 < i2.
Not that i1+2 <= i2-1.
You're going to be doing things with negative ranges.

> i1 = rand() % 15;
You should be using % column_initialR instead.
Magic numbers in code are just a nightmare if you ever need to change them.

> int i, j;
Remove these, and just use for ( int i notation in your loops in input(). You manage the syntax just fine elsewhere.


> void main()
main returns an int.
You only need change void to main. You don't even need to bother with a return 0; at the end, since C++ just assumes that's what you meant.







I dont have any idea on how to reverse the number from initial_route[i][i1 + 2] to initial_route[i][i2 - 1]. Hopefully u understand what i'm saying.

For example row 1
44 83 30 29 61 88 93 91 90 59 58 99 79 64 43

The random value is position is position 1 and 8.(done this part)
Swap next value at position i1 with value at position i2.(done this part)

Now, i need to do reverse order from [i1 + 2] and [i2 - 1]. (I dont have idea how to do it).

The answer should be
44 83 90 91 93 88 61 29 30 83 59 58 99 79 64 43

Hoping for someone help.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int temp;
const int size = 10;

cout << "Array elements before reversing:\n";
for(int i = 0; i < size; i++)
   cout << array[i] << ' ';

for(int i = 0; i < (int)size/2; i++) {
   temp = array[i];
   array[i] = array[3-i-1];
   array[3-i-1] = temp;
}

cout << "\n\nArray elements after reversing:\n";
for(int i = 0; i < size; i++)
   cout << array[i] << ' ';

Play around with the initializing and condition part of the for-loop to reverse parts of the array and I think you'll get the hint. Like for example, how would you modify the for-loop to reverse all elements but the first two?
Start by removing all the fluff from your code and just focus on the problem.
1
2
3
4
5
6
7
8
9
void reverse ( int arr[], int fromIndex, int toIndex ) {
  // code here
}
int main ( ) {
  int arr[] = { 44, 83, 30, 29, 61, 88, 93, 91, 90, 59, 58, 99, 79, 64, 43 };
  int fromIndex = 1;
  int toIndex = 8;
  reverse(arr,fromIndex,toIndex);
}


It's far easier to work on a 10-line program to explore just one specific part of the problem, than hack about with a 100-line program. You can edit/debug/test really small programs a lot faster.

I mean, once it works, you just copy/paste the reverse() function into your actual program, maybe change the name to something better, and make the first parameter a vector<int>& parameter.

> The answer should be
> 44 83 90 91 93 88 61 29 30 83 59 58 99 79 64 43
Really!?
Because that's now 16 elements long (it was 15), and you've repeated 83 twice.



Salem c, sorry i had made a mistakes.. u re right the i have repeat 83 twice.. the correct answer should be

44 83 90 91 93 88 61 29 30 59 58 99 79 64 43

Grime, thanks for the hint.. i will try to do it..
Topic archived. No new replies allowed.