# Thread: absolute value in an expression?

1. ## 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. I guess abs() function will do it. Have you tried it?

3. 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. 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.

5. 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.

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. 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.

8. Originally Posted by Arex Bawrin
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. 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. 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. Originally Posted by Sebastiani
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. 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.

13. Originally Posted by Arex Bawrin
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. I just rebuilt it and it works great now. Thank you very much

15. When i ran the code above it gave me 2293608 minutes... Whats the problem.