Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cmath>

using namespace std;

double getMaxAbsolute(double array[], int size)
{
double max = array[0];
for (int i = 1; i < size; i++)
{
if(fabs(array[i])>fabs(max))
{
max=array[i];
}
}
return max;
}

int main()
{
double array[4]= {-55.1, 2.3, 4.9, -110.0};
cout<<getMaxAbsolute(array,4);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Im trying to create a function in C where it takes an array and size and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ ProgrammingWrite a function that takes an integer array, the array size and a number....

    C++ ProgrammingWrite a function that takes an integer array, the array size and a number. And it determines how many times that number appears in the array. Just copy the following code and paste it to your answer. And fill the corresponding part. #include < iostream >using namespace atd; int howmany (int array lint , int number){ //Your code comes here} int main() {const int N =10; int array[N] = [11,3,2,1,3,4,6,9,1.3); int count = howlany(array,N,3);cout << 3 < "appesa" << cout << "times!"; return 0;

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a 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. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • USING C++: Write a function that given a 2D dynamic array of integers, will return the...

    USING C++: Write a function that given a 2D dynamic array of integers, will return the number of elements whose absolute value is smaller than a given value x. The function should take as arguments (in this order): a pointer to the array (i.e., 2D dynamic array) the number of rows the number of columns the given value The function should return the number of elements smaller in absolute value than epsilon E.g. if A={{0.2, -0.1, 3.4},{4.4, 0, -2}} and...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • Rewrite the C++ code below so the the following conditions are met: You may create any...

    Rewrite the C++ code below so the the following conditions are met: You may create any number of classes. You may edit the main() function. Remove the variable string type completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Get rid of every if and if else statement completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Hint: Use polymorphism. #include <iostream> #include <vector> #include <algorithm>...

  • C++ Need the step count for this function. int binarySearch(const int array[], int size, int value)...

    C++ Need the step count for this function. int binarySearch(const int array[], int size, int value) { int first = 0, last = size − 1, middle, position = −1; bool found = false; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle − 1; else first = middle + 1; } return position; }

  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT