Thread: write a program that replaces values in a vector with their absolute values.

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    write a program that replaces values in a vector with their absolute values.

    This is my code:
    [tag]
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <vector>
     
    using namespace std;
     
     
     
    void v_abs( vector <int>  &x, double p[] ) 
    { 
    int i=0; 
     
     
    while( p[i] < x.size () ) 
    { 
    if ( p[i] < 0 )
    { 
    p[i] *= -1 ; 
    i ++; 
    } 
    } 
     
     
    }
     
    int main(){
           cout<<v_abs<<endl;
           system("pause");
           return 0;
     
    }
    [/tag] 

    idk, I can't execute correctly.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Why does your v_abs function take both a vector and an array of doubles?
    2. Why does your function not use the vector?
    3. Why does your function not use abs?
    4. Why do you not pass anything to your function?

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Quote Originally Posted by tabstop View Post
    1. Why does your v_abs function take both a vector and an array of doubles?
    2. Why does your function not use the vector?
    3. Why does your function not use abs?
    4. Why do you not pass anything to your function?

    It sure not take both a vector and an array of doubles?

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by ams210 View Post
    It sure not take both a vector and an array of doubles?
    What is the array for? Make the function iterate over the vector, read in each value, calculate the absolute value, write the result to the vector, rinse and repeat. No need for an array.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    cout<<v_abs<<endl;

    What does that even do? I can't even follow this code. std::cout a void return?

    I think you may have cut a little too much code out of what you are doing here.

    Code:
    #include <vector>
    #include <iostream>
    
    
    int main()
    {
        std::vector <double> numbers;
        numbers.push_back(-1.0);
        numbers.push_back(3.0);
        numbers.push_back(-5.0);
        numbers.push_back(7.0);
        numbers.push_back(-9.0);
        numbers.push_back(11.0);
        
        for(unsigned int i = 0; i < numbers.size(); i++)
        {
            if(numbers[i] < 0)numbers[i] *= -1; //make positive.    _OR_   use numbers[i] = abs(numbers[i]);
            std::cout<<numbers[i]<<std::endl;
        }
        //vector is now absolute valued.
        
        return 0;
    }
    NOTE: if you use abs include stdlib or it won't work
    Last edited by ~Kyo~; 12-10-2013 at 02:34 AM. Reason: added Note

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 07-27-2013, 10:20 AM
  2. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  3. vector values not updating
    By cowboyjo in forum C++ Programming
    Replies: 11
    Last Post: 08-07-2009, 01:02 PM
  4. vector : accessing values
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 09:26 PM
  5. Read and Write, not getting same values.
    By Wraith-Lunati in forum C++ Programming
    Replies: 9
    Last Post: 09-13-2006, 10:41 AM