a simple vector as an argument of a function

This is a discussion on a simple vector as an argument of a function within the C++ Programming forums, part of the General Programming Boards category; Dear All, I wrote two simple functions one of which works (sum) and the other (deduct) gives same values for ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    a simple vector as an argument of a function

    Dear All,
    I wrote two simple functions one of which works (sum) and the other (deduct) gives same values for all vector elements. Where did I go wrong???
    Thank you


    Code:
    vector <double> deduct (const vector<double>& v, double k)
    
          { 
               vector<double> d(v.size());
               
                  for(int i=0; i < v.size();++i)
    	
                        d[i]=v[i]-k;
                        
                        return d;
          }
    
    double sum (const vector<double>& v)
       {
           double c=0.000000000;
           
            for(int i=0; i < v.size();++i)
    	
                   c +=v[i];
    	
                  return c;
         }
    
    #include <vector>
    
    main()
    
    {
        vector <double> vect(8);
        double fidy []={112.999,112.994,112.994,-0.005,-0.007,-13.004, -113.003,-113.005};
         vector<double> yfid(fidy,fidy+imarknum);
         double k=sum (vect);
         v=deduct (vect, k);
    }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Mistake

    Sorry, I made a mistake. Last 2 lines should be:
    Code:
    double k=sum (yfid);
     v=deduct (yfid, k);

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't you think it would be a good idea to #include<vector> before you start using pieces of said vector? Also, what exactly is v, since you haven't actually declared it any place.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    It's OK

    I am sorry, I wrote this thread in haste, functions are coming after
    #include <vector>

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    v

    v is an argument of function

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by quzah
    Also, what exactly is v, since you haven't actually declared it any place.
    Quote Originally Posted by strickey
    v is an argument of function
    He means this one in main:

    Code:
    v=deduct (vect, k);
    It isn't declared anywhere before its use.

    You also need to consider that vector containers are a part of the std namespace. So either:

    1. Put std:: before any vector declaration, i.e.:
    Code:
    std::vector<double> vect(8);
    2. Put this after your includes:
    Code:
    using std::vector;
    3. Lastly you could do this after your includes:
    Code:
    using namespace std;
    You should prefer the first option over the others if possible.

    [edit]And while I'm at it, you should put an explicit int in front of main and you also don't seem to have defined the imarknum variable anywhere.[/edit]
    Last edited by hk_mp5kpdw; 01-14-2005 at 07:23 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    Corrections are being made

    Code:
    using namespace std;
    .
    .
    .
    main()
    .
    .
    imarknum=8;
    vector <double> v(8);
    but result is still the same.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I'm not sure what your problem is. After outputting both the sum and the deduct return values, it looks right to me:
    Code:
    Output:
    value of double k: 99.963
    value of vector<double> v: 13.036 13.031 13.031 -99.968 -99.97 -112.967 -212.966 -212.968
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Let's get this out at the start, you allude to output in your first post but your example does not show any attempts at output, you haven't even included the <iostream> header. If you post code, lets see the whole thing since this appears to be a relatively small program and we shouldn't have to guess at what important/relevant things you might be leaving out..

    Code:
    imarknum=8;  // int ??
    double fidy []={112.999,112.994,112.994,-0.005,-0.007,-13.004, -113.003,-113.005};
    vector<double> yfid(fidy,fidy+imarknum);
    I would prefer to use sizeof to get the number of elements in the array (get rid of imarknum):

    Code:
    double fidy []={112.999,112.994,112.994,-0.005,-0.007,-13.004, -113.003,-113.005};
    vector<double> vfid(fidy,fidy+(sizeof(fidy)/sizeof(fidy[0])));
    This way if you were to ever change the number of elements in the fidy array, you wouldn't need to change imarknum.

    Also, just so you are aware/exposed to these things, there are template functions available that can help you do much of what you are trying to accomplish with the sum and deduct functions, i.e. the following:
    Code:
    double sum (const vector<double>& v)
    {
        double c=0.000000000;
           
        for(int i=0; i < v.size();++i)
            c +=v[ i ];
    	
        return c;
    }
    
    ...
    
    double k=sum (vect);
    Can be replaced with a simple call to the accumulate function:
    Code:
    #include <numeric>
    
    ...
    
    double k = std::accumulate(vect.begin(),vect.end(),0.0);
    The deduct function can likewise be replaced with some code to copy the passed in vector into a new vector (using the vector's copy constructor) followed by a call to the transform function on the newly copied vector and using the minus function object along with the bind2nd function adapter. Although admittedly in this case, you aren't going to be saving much in the way of typing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21