Question

C++ LanguageGiven an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Examples

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

Function:

int largeDiff(const int a[], int n) //function
{
int mini=a[0], maxi=a[0]; //initializing minimum and maximum are of first element.
int diff;
for(int i=1; i<n; i++) // reading second element to last element in array.
{
if(a[i] > maxi) //if any number is more than maximum then updating the maximum.
maxi = a[i];

if(a[i] < mini) //if any number is less than minimum then updating the minimum.
mini = a[i];
}
diff = maxi-mini; //calculating difference between maximum and minimum.
return diff; //returning difference.
}

int largeDiff (const int a[], int n) //function int mini-a [0], maxi=a[0]; //initializing minimum and maximum are of first el

Program:

#include<iostream>
using namespace std;
int largeDiff(const int a[], int n) //function
{
int mini=a[0], maxi=a[0]; //initializing minimum and maximum are of first element.
int diff;
for(int i=1; i<n; i++) // reading second element to last element in array.
{
if(a[i] > maxi) //if any number is more than maximum then updating the maximum.
maxi = a[i];

if(a[i] < mini) //if any number is less than minimum then updating the minimum.
mini = a[i];
}
diff = maxi-mini; //calculating difference between maximum and minimum.
return diff; //returning difference.
}
int main()
{ //declaring arrays and calling the function and printing the result.
int a[] = {10, 3, 5, 6};
cout << largeDiff(a, 4) << endl;

int b[] = {7, 2, 10, 9};
cout << largeDiff(b, 4) << endl;

int c[] = {2, 10, 7, 2};
cout << largeDiff(c, 4) << endl;

int d[] = {2};
cout << largeDiff(d, 1) << endl;
return 0;
}

1 2 3 #include<iostream> using namespace std; int largeDiff (const int all, int n) //function 4 5 int mini=a[0], maxi=a[0]; /

output:

7 8 8 CO Process returned @ (exe) execution time : 0.021 s Press any key to continue.

Note: my friend if you have any questions or queries comment below. I am happy to answer your all questions. I will sort out your queries. Thank you my friend.

Add a comment
Know the answer?
Add Answer to:
C++ Language Given an array length 1 or more of ints, return the difference between the...
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
  • /** Given an int array, return an int array with duplicate ints removed if the array...

    /** Given an int array, return an int array with duplicate ints removed if the array contains duplicate values. <br> <br> removeDuplicateInts({3}) -> {3} <br> removeDuplicateInts({1, 2}) -> {1, 2} <br> removeDuplicateInts({7, 7}) -> {7} <br> removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7} <br> removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5}) <br> removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5} <br> @param numbers int[] an array of integers. @return...

  • Given an array of ints length 3, return a new array with the elements in reverse...

    Given an array of ints length 3, return a new array with the elements in reverse order, so (1, 2, 3) becomes (3, 2, 1). reverse3 ([1, 2, 3]) [3, 2, 1] reverse3(15, 11, 9])9, 11, reverse3(17, e, 0]) [e, e, 7] 5]

  • please write this code in c++ // a: an array of ints. size is how many...

    please write this code in c++ // a: an array of ints. size is how many ints in array 7/ Return the largest value in the list. 7/ This value may be unique, or may occur more than once // You may assume size >= 1 int largestValue(int *a, int size) { assert(size >= 1); return -42; // STUB !!! Remove and replace with correct code 71 a: an array of ints. size is how many ints in array 7/...

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • I have to write a loop that moves the content of the dynamic array to the...

    I have to write a loop that moves the content of the dynamic array to the static array but I can't seem to figure it out. Exit Full Screen code.cpp New 1 #include <iostream> 3 using namespace std; 4 5 int main) 6 int *ptr 7 ptr -new int[5]; 8 int arr[5]; 9int x; 10 cin >> x; 11 for( int í = 0; 1 < 5; 1++) // initialization 12 13 ptr[i] x; = 14 for O//your code goes...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

  • C programming Write a function that gets a 2-d array of given dimensions of ints. It...

    C programming Write a function that gets a 2-d array of given dimensions of ints. It returns true if the array contains two rows with exactly the same values in the same order, and returns false otherwise, bool contains_equal_rows(int height, int width, const int ar[height] [width]) Examples: On input {{1,2,3,4}, {2,3,4,1}, {1,2,3,4}} it returns true. On input {{1,2,3,4}, {2,3,4,5}, {3,4,5,6}} it returns false. On input {{1,1,1,1}, {2,2,2,2}, {1,1,1,6}} it returns false.

  • Language: c++ Write the templated function T kMin(const T* A, const size_t n, const size_t k)...

    Language: c++ Write the templated function T kMin(const T* A, const size_t n, const size_t k) that takes as input an array A of values of type T with length n>= 0 and an integer k and returns the k-th smallest value in the array. If k >= n, throw a invalid_argument exception Example: A = [8,6,7,5,3,0,9] (array of ints), n = 7, k = 2, expected value = 5

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