Absolute value of each element in a given array

This is a discussion on Absolute value of each element in a given array within the C Programming forums, part of the General Programming Boards category; I'm trying to get the absolute value of each element of an array, but am running into problems. I need ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    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;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > 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.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    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;

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by DriftinSW20 View Post
    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

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    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);
    
    }

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Now you are calling your absolute() function 16 times, and each time it is called, it processes (the same) 16 integers.

    Todd

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    then how would you make it so that it prints out each individual number from the resulting array?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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]);

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You tell me! Do you want to print out the values in main() or in absolute()?

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    nevermind, i got it! thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 10:42 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 09:58 AM
  3. Replies: 19
    Last Post: 07-20-2007, 02:46 AM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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