# Reversing the order of a Vector

Page 1 of 1

## 7 Replies - 4012 Views - Last Post: 24 November 2009 - 06:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=140469&amp;s=415eebca1ab3919c7a606c3d32245898&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dr0pkick

Reputation: 0
• Posts: 38
• Joined: 22-September 09

# Reversing the order of a Vector

Posted 20 November 2009 - 12:48 PM

Our assignment requires us to wrie one of two functions to reverse the order of a vector of a non-specified size. I have written and re-written the code, and cannot for the life of me figure out what I am doing wrong.

```#include "G:/Computer Science/Chapter 8/Chapter08/std_lib_facilities.h"

void print(const string& label, const vector <int>& v)
{
cout << label << "(";
for (int i = 0; i<v.size(); ++i) cout << v[i] << ' ';
cout << ")\n";
}

void rev1(vector<int>& v)
{

vector<int> w;
{
for (int i = v.size()-1; 0<=i; --i)
w.push_back(v[i]);
v = w;
}

int main()
try
{

vector<int> num;
cout << "Please enter a series of numbers, and end your series with ';'";
int input;
while (cin>>input)
num.push_back(input);
print("\nInput:\n",val);
rev1 (num);

keep_window_open();
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Reversing the order of a Vector

### #2 EdwinNameless

Reputation: 123
• Posts: 720
• Joined: 15-October 09

## Re: Reversing the order of a Vector

Posted 20 November 2009 - 01:07 PM

Well... You don't print after reversing, so how do you know it's not working?

### #3 dr0pkick

Reputation: 0
• Posts: 38
• Joined: 22-September 09

## Re: Reversing the order of a Vector

Posted 20 November 2009 - 02:46 PM

I keep receiving the following errors from the compiler:

Error 2 error C2601: 'main' : local function definitions are illegal g:\computer science\chapter 5\homework8_5\homework8_5\homework8_5.cpp 29
Error 3 fatal error C1075: end of file found before the left brace '{' at 'g:\computer science\chapter 5\homework8_5\homework8_5\homework8_5.cpp(18)' was matched g:\computer science\chapter 5\homework8_5\homework8_5\homework8_5.cpp 43

### #4 KYA

• g++ jameson.cpp -o beverage

Reputation: 3153
• Posts: 19,194
• Joined: 14-September 07

## Re: Reversing the order of a Vector

Posted 20 November 2009 - 02:50 PM

try catch goes inside main() not the whole thing.

### #5 dr0pkick

Reputation: 0
• Posts: 38
• Joined: 22-September 09

## Re: Reversing the order of a Vector

Posted 21 November 2009 - 08:07 AM

Ok, I made a few adjustments, and still have the same errors...any thoughs?

```#include "G:/Computer Science/Chapter 8/Chapter8Stuff/std_lib_facilities.h"

void print(const string& label, const vector <int>& v)
{
cout << label << "(";
for (int i = 0; i<v.size(); ++i) cout << v[i] << ' ';
cout << ")\n";
}

void rev1(vector<int>& v)
{

vector<int> w;
{
for (int i = v.size()-1; 0<=i; --i)
w.push_back(v[i]);
v = w;
}

int main()
{

vector<int> num;
cout << "Please enter a series of numbers, and end your series with ';'";
int input;
while (cin>>input)
num.push_back(input);
print("\nInput:\n",val);
rev1 (num);

keep_window_open();
return 0;
}

```

### #6 dr0pkick

Reputation: 0
• Posts: 38
• Joined: 22-September 09

## Re: Reversing the order of a Vector

Posted 23 November 2009 - 09:08 AM

ok, I have re-written the code...again...so, now it is only printing out what the total number of the vector is, and not the reverse order...help is greatly appreciated as I am not sure what I am doing wrong.

```#include "G:/Computer Science/Chapter 8/Chapter8Stuff/std_lib_facilities.h"

void print (const string& label, const vector<int> &v)
{
cout << label << "(";
for (int i = 0; i<v.size(); ++i)
cout << v[i];
cout << ")\n";
}
void reverse (vector<int>& v)
{
vector<int> w;
for (int i = v.size()-1; 0<=i; --i)
w.push_back(v[i]);
v = w;
}

int main()
try
{
cout << "Please eneter a series of integers, followed by a character.\n";
vector<int> num;
int i;
while (cin >> i);
num.push_back(i);
print ("\nInput: \n", num);
reverse (num);
print ("Reversed: \n", num);

keep_window_open("~");

}
catch (runtime_error e)
{
cout << e.what() << '\n';
keep_window_open("~");
}
catch (...)
{
cout << "Terminating Program\n";
keep_window_open("~");
}

```

### #7 KYA

• g++ jameson.cpp -o beverage

Reputation: 3153
• Posts: 19,194
• Joined: 14-September 07

## Re: Reversing the order of a Vector

Posted 23 November 2009 - 09:20 AM

You don't even need a function:

```//example

int main(){
vector<int> nums;
int temp;
for(int i = 1; i <= 5; i++){
nums.push_back(i);
}

//intial output:
for(int i = 0; i < nums.size(); i++){
cout << nums.at(i) << " ";
}

cout << endl << endl;

//reversal:
for(int i = 0, j = nums.size()-1; i < nums.size()/2; i++, j--){
temp = nums.at(i);
nums.at(i) = nums.at(j);
nums.at(j) = temp;
}

//after output:
for(int i = 0; i < nums.size(); i++){
cout << nums.at(i) << " ";
}

cout << endl << endl;

return 0;
}
```

### #8 dr0pkick

Reputation: 0
• Posts: 38
• Joined: 22-September 09

## Re: Reversing the order of a Vector

Posted 24 November 2009 - 06:54 AM

Thanks for the assist, KYA...I managed to figure out where I was going wrong with your help. You had mentioned that there was no need for a function...I neglected to mention that the assignment required us to have function headers...oops, but needless to say, it is all figured out now:)

Again, thank you for the help!