Thread: Manhattan Distance between two vectors

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Manhattan Distance between two vectors

    Hi

    I can't figure out the error in this code; it compiles but returns rubbish. Can you help? Thank a lot!

    serge

    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <algorithm>

    Code:
    int main(int argc, char* argv[]) {
    	std::vector<double> VEC1;
    	std::vector<double> VEC2;
    	
    	VEC1.push_back(1.2);
    	VEC1.push_back(3.4);
    	VEC1.push_back(5.1);
    	VEC2.push_back(4.5);
    	VEC2.push_back(1.0);
    	VEC2.push_back(1.9);
    	for ( std::vector<double>::iterator it1 = VEC1.begin(); it1 != VEC1.end(); ++it1 ) std::cout << " " << *it1;
    	std::cout << std::endl;	
    	for ( std::vector<double>::iterator it1 = VEC2.begin(); it1 != VEC2.end(); ++it1 ) std::cout << " " << *it1;
    	std::cout << std::endl;
    	double DIST = 0.0;
    	
    	for ( std::vector<double>::iterator it1 = VEC1.begin(); it1 != VEC1.end(); ++it1 ) {
    		for ( std::vector<double>::iterator it2 = VEC2.begin(); it2 != VEC2.end(); ++it2 ) {
    			DIST += abs( (*it1) - (*it2) );
    		}
    	}
    	
    	std::cout << DIST << std::endl;
    
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? Like, what is your expected output and actual output? How did you arrive at your expected output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by laserlight View Post
    How does it not work? Like, what is your expected output and actual output? How did you arrive at your expected output?
    The result should be:
    abs(1.2-4.5)+abs(3.4-1.0)+abs(5.1-1.9)=3.3+2.4+3.2=8.9
    whereas the program returns -14

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by serge
    The result should be:
    abs(1.2-4.5)+abs(3.4-1.0)+abs(5.1-1.9)=3.3+2.4+3.2=8.9
    whereas the program returns -14
    Right. Now look at your loops:
    Code:
        for ( std::vector<double>::iterator it1 = VEC1.begin(); it1 != VEC1.end(); ++it1 ) {
            for ( std::vector<double>::iterator it2 = VEC2.begin(); it2 != VEC2.end(); ++it2 ) {
                DIST += abs( (*it1) - (*it2) );
            }
        }
    VEC1 has 3 elements. VEC2 has 3 elements. Therefore, the inner loop runs 3 * 3 = 9 times, i.e., DIST is updated 9 times. According to you, however, DIST should only be updated 3 times, i.e., once for each of the three terms in your manual computation. Clearly, the use of nested loops here is a logic error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    115
    Ok, a stupid error. But then one cannot use one iterator on two arrays. So, what is the best way of doing it

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use two iterators, but compare them with their respective end iterators and increment them in the same loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Or just use the [] operator, maybe?
    I don't think being verbose with iterators has any advantage here.
    Code:
    for(int i=0;i<std::min(vec1.size(),vec2.size());++i)
        dist+=std::fabs(vec1[i]-vec2[i]);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>for(int i=0;i<std::min(vec1.size(),vec2.size());++i)
    You should put an assert or a check before. If the vectors are not of the same size, it is an error (you cannot subtract two mathematical vectors that are not of equal length). You are just ignoring this error.
    Furthermore, .size() does not return an int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to do euclidian distance?
    By Jeffery1 in forum C Programming
    Replies: 4
    Last Post: 05-23-2013, 08:51 AM
  2. distance formula help
    By Arex Bawrin in forum C Programming
    Replies: 15
    Last Post: 02-25-2011, 03:38 PM
  3. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM
  4. distance algorythm help
    By MicroFiend in forum Game Programming
    Replies: 9
    Last Post: 06-12-2004, 09:28 PM
  5. Hex distance
    By waterst in forum C Programming
    Replies: 7
    Last Post: 11-02-2002, 01:59 PM