Thread: Why range error & how to fix it in this program?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    Why range error & how to fix it in this program?

    Hi,

    I can't figure out why it gives the warning "range error". The program doesn't run... Could you please give a hint for fixing it? Thanks a lot...

    Code:
    // Exercise 8.5
    #include "std_lib_facilities.h"
    /*
    write a function to reverse the order of elements in a vector
    for example
    	1 2 3 4 5 6
    becomes
    	6 5 4 3 2 1
    
    */
    
    void v_rev1(vector<double>&);
    void print(vector<double>&);
    
    int main()
    try {
    	cout << "Enter some numbers: ";
    	vector<double> v;
    	double x;
    	while (cin >> x)
    		v.push_back(x);
    	
    	cout << "In reversed sequence: " << '\n';
    	cout << "{";
    	v_rev1(v);
    	print(v);
    	cout << "}" << '\n';
    	
    }
    
    catch (runtime_error e) {
    	cout << e.what();
    }
    
    
    void v_rev1(vector<double>& v1) 
    {
    	vector<double> v2;
    	for (unsigned int i = v1.size() - 1; i >= 0; --i) {
    		v2.push_back(v1[i]);
    	}
    	v1 = v2;	
    	
    }
    
    void print(vector<double>& v)
    {
    	for (unsigned int j = 0; j < v.size(); ++j) {
    		cout << v[j];
    		if (j != v.size() - 1)
    			cout << ", ";
    	}
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I don't see any "error range" in the code you posted. Perhaps you forgot to post the error message?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by Dino View Post
    I don't see any "error range" in the code you posted. Perhaps you forgot to post the error message?
    Hi,

    The warning I received is as follows:

    Code:
    terminate called after throwing an instance of 'Range_error'
      what():  Range error: -1
    {Abort trap

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Quote Originally Posted by pantera View Post
    Hi,

    I can't figure out why it gives the warning "range error". The program doesn't run... Could you please give a hint for fixing it? Thanks a lot...

    Code:
    void v_rev1(vector<double>& v1) 
    {
    	vector<double> v2;
    	for (unsigned int i = v1.size() - 1; i >= 0; --i) {
    		v2.push_back(v1[i]);
    	}
    	v1 = v2;	
    	
    }
    This function, you have i as unsigned, when you reach zero, then decrement it, you're not going to be below zero as you seem to expect(Assuming unsigned int is 32 bit, you end up with the value 2^32-1 ). To solve this you could either:

    A: Change the type of i to signed int.
    B: Change the method to work with unsigned int.

    For example you could accomplish B by doing:
    Code:
    for (unsigned int i = 0; i < v1.size(); ++i) {
    	v2.push_back(v1[v1.size()-1-i]);
    }

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by JacobN View Post
    This function, you have i as unsigned, when you reach zero, then decrement it, you're not going to be below zero as you seem to expect(Assuming unsigned int is 32 bit, you end up with the value 2^32-1 ). To solve this you could either:

    A: Change the type of i to signed int.
    B: Change the method to work with unsigned int.

    For example you could accomplish B by doing:
    Code:
    for (unsigned int i = 0; i < v1.size(); ++i) {
    	v2.push_back(v1[v1.size()-1-i]);
    }
    Hi JacobN, thanks so much for helping. It runs properly now...

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Since all v_rev1() is trying to do is reverse the order of elements in the vector, why not use std::reverse() from the <algorithm> standard header?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM