# absolute value in an expression?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-06-2009
Arex Bawrin
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.
• 09-06-2009
BEN10
I guess abs() function will do it. Have you tried it?
• 09-06-2009
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; }```
• 09-06-2009
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.
• 09-06-2009
Arex Bawrin
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.
• 09-06-2009
Arex Bawrin
Quote:

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
• 09-06-2009
cpjust
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.
• 09-06-2009
Sebastiani
Quote:

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).
• 09-06-2009
Arex Bawrin
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; }```
• 09-06-2009
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.
• 09-06-2009
Arex Bawrin
Quote:

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.
• 09-06-2009
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. :)
• 09-06-2009
Sebastiani
Quote:

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?
• 09-06-2009
Arex Bawrin
I just rebuilt it and it works great now. Thank you very much:)
• 09-08-2009