# Absolute value of each element in a given array

• 11-15-2007
DriftinSW20
Absolute value of each element in a given array
I'm trying to get the absolute value of each element of an array, but am running into problems. I need to write a function that takes in as its arguments an array of numbers and the number of elements and outputs an array that contaions the absolute value of each number in the input array.

Code:

```#include <stdio.h> #include <math.h> int abs(int *array, int N); int main() {         int array[16] = {0,1,2,3,-4,5,6,7,-8,9,-10,11,12,13,14,20};         int result;         int absolute;                 absolute = abs(array,16);         printf("abs is %i", absolute);         return 0; } int abs(int *array, int N) {         int i;         for(i=0; i<N, i++)                 if (array[i]<0)                         array[i]=array*(-1); return array; }```
• 11-15-2007
swoopy
> array[i]=array*(-1);
I believe you want:
Code:

`                        array[i] = array[i] * (-1);`
Or:
Code:

`                        array[i] *= -1;`
And there's no need for:
Code:

`return array;`
since the modifed array is an argument to the function, and the changes are seen by the calling function.
• 11-15-2007
DriftinSW20
thanks for the help! we also need to print out the array to verify that it works correctly, I have this code but its not printing out right.

Code:

```for ( i = 0; i < 16; i++ )                 absResult=absolute(array,16);                 printf("the absolute value is %d\n", absResult);         return 0;```
• 11-15-2007
Dino
Quote:

Originally Posted by DriftinSW20
Code:

```#include <stdio.h> #include <math.h> int abs(int *array, int N); int main() {         int array[16] = {0,1,2,3,-4,5,6,7,-8,9,-10,11,12,13,14,20};         int result;         int absolute;                 absolute = abs(array,16);         printf("abs is %i", absolute);         return 0; } int abs(int *array, int N) {         int i;         for(i=0; i<N, i++)                 if (array[i]<0)                         array[i]=array*(-1); return array; }```

There are a few errors.

The red disagree. In the prototype, you say abs() returns an int, but in the function, you return a pointer.

In green, you need to use an element of the array, not the pointer to the array.

In blue, even if you had the other things right, would only print one value. You might want to move the printf somewhere closer to your loop.

Todd
• 11-15-2007
DriftinSW20
this is the corrected code that i have, its just not printing out right:
Code:

```#include <stdio.h> #include <math.h> int absolute(int *array, int N); int main() {         int array[16] = {0,1,2,3,-4,5,6,7,-8,9,-10,11,12,13,14,20};         int ray[16];         int i;                 for ( i = 0; i < 16; i++ )                 ray[i]=absolute(array,16);                 printf("the absolute value is %d\n", ray[i]);         return 0; } int absolute(int *array, int N) {         int i;         for(i=0; i<N; i++)                 if (array[i]<0)                         array[i] = array[i] * (-1); }```
• 11-15-2007
Dino
Now you are calling your absolute() function 16 times, and each time it is called, it processes (the same) 16 integers.

Todd
• 11-15-2007
DriftinSW20
then how would you make it so that it prints out each individual number from the resulting array?
• 11-15-2007
swoopy
You could change the absolute function to simply find the absolute value of a number:
Code:

```int absolute(int number) {         if (number<0)                 return number * (-1); }```
Then you could use the for-loop contained in main() as is. It doesn't seem to match the original problem description though. You would have to change this line:
Code:

`                ray[i]=absolute(array,16);`
to:
Code:

`                ray[i]=absolute(array[i]);`
• 11-15-2007
Dino
You tell me! Do you want to print out the values in main() or in absolute()?
• 11-15-2007
DriftinSW20
nevermind, i got it! thanks for the help!