Question

In: Computer Science

Im trying to create a function in C where it takes an array and size and...

Im trying to create a function in C where it takes an array and size and returns the largest absolute value in the array (either negative or positive) using only stdio.h. Any help would be greatly appreciated!

Ex: if the array is { -2.5, -10.1, 5.2, 7.0}, it should return -10.1

Ex: if the array is {5.1, 2.3, 4.9, 1.0}, it should return 5.1.

double getMaxAbsolute(double array[], int size)
{
    double max = array[0];
   double abs[size];
   for (int i = 0; i < size; i++) {
      
        if ( array[i] >= 0){
        abs[i] = array[i];
        }
        else if (abs[i] < 0){
           abs[i] = - array[i];
       }

       if (abs[i] > max)
       {
            max = array[i];
          
        }
   }

   return max;
}

Solutions

Expert Solution

#include <stdio.h>

double getMaxAbsolute(double array[], int size)
{
double maximum = array[0];
double actual_value = maximum;
if(maximum < 0)
maximum = maximum*-1;

int i;
for(i=1; i<size; i++)
{
if(array[i] < 0 && array[i]*-1 > maximum)
{
actual_value = array[i];
maximum = array[i]*-1;
}
else if(array[i] >= 0 && array[i] > maximum)
{
actual_value = array[i];
maximum = array[i];
}
}
return actual_value;
}


int main(void) {
double a[] = { -2.5, -10.1, 5.2, 7.0};
printf("%lf\n", getMaxAbsolute(a, 4));

double b[] = {5.1, 2.3, 4.9, 1.0};
printf("%lf\n", getMaxAbsolute(b, 4));
return 0;
}

/*OUTPUT

-10.100000
5.100000

*/

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

Create a function output() in C that takes the pointer to the array and its size...
Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (function prototype : void output(int *arrayPtr, int size);
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
In C++ Write a function that took in SIZE and array [size] and counted the number...
In C++ Write a function that took in SIZE and array [size] and counted the number of elements >100
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT