Thread: absolute value in an expression?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    absolute value in an expression?

    This may be a silly question but I am pretty new to programming. Our teacher gave us an assignment where we are suppose to utilize the expression:

    Code:
    totalTime = (12 – | 12 – t |)*g/a;
    where t, g, and a are all integers. I was wondering how can I get the absolute value of 12-t? I've searched the reference but it's only telling me how to get the absolute value of a single number, and not really an expression.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I guess abs() function will do it. Have you tried it?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    I'm getting the following errors:

    Code:
    ucfParking.c: In function ‘main’:
    ucfParking.c:22: error: stray ‘\342’ in program
    ucfParking.c:22: error: stray ‘\200’ in program
    ucfParking.c:22: error: stray ‘\223’ in program
    ucfParking.c:22: error: expected ‘)’ before ‘absExp’
    ucfParking.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    here's my code:

    Code:
    ucfParking.c: In function ‘main’:
    #include <stdio.h>
    
    int main(void) 
    {
    
    	int timeOfDay, availableSpots, permitsGiven, totalTime, absExp;
    
    	printf("What hour are you looking for parking?\n");
    	scanf("%d", &timeOfDay);
    
    	printf("How many spots are available this semester?\n");
    	scanf("%d", &availableSpots);
    
    	printf("How many parking permits were given this semester?\n");
    	scanf("%d", &permitsGiven);
    
    	absExp = abs(12-timeOfDay); 
    	totalTime =  (12 – absExp)*permitsGiven/availableSpots;	
    
    	printf("You will have to wait %d minutes to find parking.\n", &totalTime);
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Absolute value is just:

    Code:
    if( variable < 0)
       variable *= -1;
    If that makes it any easier for you.

    Looks like your errors are in the ucfParking.c code.
    Last edited by Adak; 09-06-2009 at 09:22 PM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Well we havent got to if statements yet so I don't think he wants us to go that route just yet. He also said we only need the stdio.h library for this program to run properly. I just don't know what I'm doing wrong here.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by Adak View Post
    Absolute value is just:

    Code:
    if( variable < 0)
       variable *= -1;
    If that makes it any easier for you.

    Looks like your errors are in the ucfParking.c code
    .
    Sorry I forgot to mention that code is ucfParking.c

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You need to include the right header for abs() which is math.h
    Also, looks like you might have some strange (invisible) characters on line 22. Try deleting it and typing it in again.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Arex Bawrin View Post
    I'm getting the following errors:

    Code:
    ucfParking.c: In function ‘main’:
    ucfParking.c:22: error: stray ‘\342’ in program
    ucfParking.c:22: error: stray ‘\200’ in program
    ucfParking.c:22: error: stray ‘\223’ in program
    ucfParking.c:22: error: expected ‘)’ before ‘absExp’
    ucfParking.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    here's my code:

    Code:
    ucfParking.c: In function ‘main’:
    #include <stdio.h>
    
    int main(void) 
    {
    
        int timeOfDay, availableSpots, permitsGiven, totalTime, absExp;
    
        printf("What hour are you looking for parking?\n");
        scanf("%d", &timeOfDay);
    
        printf("How many spots are available this semester?\n");
        scanf("%d", &availableSpots);
    
        printf("How many parking permits were given this semester?\n");
        scanf("%d", &permitsGiven);
    
        absExp = abs(12-timeOfDay); 
        totalTime =  (12 – absExp)*permitsGiven/availableSpots;    
    
        printf("You will have to wait %d minutes to find parking.\n", &totalTime);
    
        return 0;
    }
    Weird, I'm not sure how it got there, but the '–' between the expression '(12 – absExp)' is not a minus character (which would be 45 in decimal). It's not even an ASCII character (having a value of 150 in decimal).

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Ok, so I fixed the weird minus character that was giving me all of those errors, but I still have a few left:

    Updated error:

    Code:
    ucfParking.c: In function ‘main’:
    ucfParking.c:22: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    code:
    Code:
    /*
    Alex Martin
    */
    
    #include <stdio.h>
    
    int main(void) 
    {
    	int timeOfDay, availableSpots, permitsGiven, totalTime, absExp;
    
    	printf("What hour are you looking for parking?\n");
    	scanf("%d", &timeOfDay);
    
    	printf("How many spots are available this semester?\n");
    	scanf("%d", &availableSpots);
    
    	printf("How many parking permits were given this semester?\n");
    	scanf("%d", &permitsGiven);
    
    	absExp = abs(12-timeOfDay);
    	totalTime = (12-absExp)*permitsGiven/availableSpots;	
    	printf("You will have to wait %d minutes to find parking.\n", &totalTime);
    
    	return 0;
    }

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Ah, I missed that. Well, printf doesn't need the address of a variable to print it (unless you're printing the value of a pointer, of course) - you just need to pass the variable itself. The reason scanf needs it is so it can modify the variable.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by Sebastiani View Post
    Ah, I missed that. Well, printf doesn't need the address of a variable to print it (unless you're printing the value of a pointer, of course) - you just need to pass the variable itself. The reason scanf needs it is so it can modify the variable.
    I'm sorry I just removed the ampersand in front of totalTime on line 22, but I am still getting the same error.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My editor didn't like a char in this line:

    Code:
    totalTime =  (12 – absExp)*permitsGiven/availableSpots;
    What input would be right for your program's equations?

    Military time like ( 0800 hours), is not an integer, so that fails. I tried ( 800 ) for 8 am, instead, and that failed because the abs of 12 - 800 seems off the mark.

    Tried 8 instead for the hour, 40 spots and 38 permits, and that seemed reasonable. Note that the results are irrational but seem OK for a beginning program. Example: 3 hour, 40 spots, 39 permits and you wait LESS time, than with 8 40 spots, and 38 permits.

    Should have the greater ratio of parking spots to permits equals less time waiting.
    Last edited by Adak; 09-06-2009 at 10:09 PM.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Arex Bawrin View Post
    I'm sorry I just removed the ampersand in front of totalTime on line 22, but I am still getting the same error.
    Rebuild the project. What compiler/IDE are you using?

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    I just rebuilt it and it works great now. Thank you very much

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    When i ran the code above it gave me 2293608 minutes... Whats the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 10:34 AM