Skip to content

Instantly share code, notes, and snippets.

@avipars
Last active January 16, 2022 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avipars/0a09071e0fb65ca55c0665b09b9b1afb to your computer and use it in GitHub Desktop.
Save avipars/0a09071e0fb65ca55c0665b09b9b1afb to your computer and use it in GitHub Desktop.
using namespace std;
#include <iostream>
#include <cmath>
//Question 11 (7 points) moed b 5778
//Consider an array of unique integers terminated by the value 0 (0 is not considered part of the array). Write a function that receives a pointer to the array (it does not receive its size) and returns the distance between the position of the largest element in the array and the smallest one.
int pointGap(int *arr)
{
int distance = 0;
int smallest = *arr;
int largest = *arr;
int smallindex;
int largeindex;
int curr;
for(int i = 0; *(arr + i) != 0; i++)
{
curr = *(arr + i);
if(curr >= largest)
{
largest = curr;
largeindex = i;
}
if(curr <= smallest)
{
smallest = curr;
smallindex = i;
}
}
(largeindex > smallindex) ? distance = largeindex - smallindex : distance = smallindex - largeindex;
return distance;
}
int main()
{
int arr0[] = {5, 6, -3, 2, -1, 52, 7, 9, -14, 65, 10,0};
int arr[] = {5,6,31,12,7,1,9,0};
int arr2[] = {15, 4, 13, 12, 8, 7, 19, 0};
cout << pointGap(arr0) << endl;
cout << pointGap(arr) << endl;
cout << pointGap(arr2) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment