Reversing the order of a Vector

Reverse vector input order without using 'swap' function

Page 1 of 1

7 Replies - 2370 Views - Last Post: 24 November 2009 - 06:54 AM Rate Topic: -----

#1 dr0pkick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Your assistance is greatly appreciated!

#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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 dr0pkick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 dr0pkick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#6 dr0pkick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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("~");
}


Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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;
}

Was This Post Helpful? 1
  • +
  • -

#8 dr0pkick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1