Question
c++
2. Write a CH function called range that returns the difference between the largest and smallest elements in an array.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

/**
* Returns the difference between the largest and smallest element of the array
* This function takes array and length of array in input
*/
int range(int *array, int length) {
   int i;
   int smallest = INT_MAX;
   int largest = INT_MIN;
   for(i = 0; i < length; i++) {
       if(array[i] < smallest) {
           smallest = array[i];
       }
       if(array[i] > largest) {
           largest = array[i];
       }
   }
   return largest - smallest;
}

int main() {
   int array[8] = {-9, 10, 20, -30, 70, 18, -25, 40};
   cout << range(array, 8) << endl;
   return 0;
}

Screenshots --

Code

Pinclude <iostre using namespace std; • Returns the difference between the largest and smallest element of the array • This f

Output

+ - g++ main2.cpp ./a.out 100

Add a comment
Know the answer?
Add Answer to:
c++ 2. Write a CH function called range that returns the difference between the largest 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
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