help needed

This is a discussion on help needed within the C++ Programming forums, part of the General Programming Boards category; insert Code: #include <iostream> using namespace std; int computeAbsoluteValue(int number) { // Write your code here. } int main() { ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    19

    help needed

    insert
    Code:
    #include <iostream>
    using namespace std;
    
    int computeAbsoluteValue(int number)
    {
    	// Write your code here.
    }
    
    int main()
    {
    	int input;
    	
    	cout << "Enter a number: ";
    	cin >> input;
    	
    	cout << "| " << input << " | = "
    	     << computeAbsoluteValue(input)
    	     << endl;
    	
    	return 0;
    }
    
    Fill up the computeAbsoluteValue function so that it will return the absolute value of a number. The absolute value of a number is its numerical value without regard to its sign (eg. 8 is the absolute value of both 8 and -8). The output of the program should look like these:
    
    Sample Output 1:
    Enter a number: -12
    |-12| = 12
    
    Sample Output 2:
    Enter a number: 5
    |5| = 5
    
    
    
    
    * can anyone help me with this??

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    look at the operator ?:
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    huh??

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It's a pretty simple algorithm. If the number is greater than or equal to zero, return that number. If the number is less than zero, multiply the number by negative one and return the result.

    This can be done in one line of code using the ?: operator like vart suggested, but I'd imagine it would be a lot easier for you to just use if/else statements instead.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    i think i get the idea.. thx.. ^^

  6. #6
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    Only one more thing, please but some more suggestive titles, like: Help in Insert, Insert Problems...
    And but the code and the result in separated code tags please!

    Thanks!
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: eeepc904@gmail.com
    ICQ: 424738586
    AIM: nathanjava

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Help Please
    By Moose112 in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 06:16 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. Using pointers - asterisks needed ?
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-28-2002, 05:56 PM

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