Skip to content

Instantly share code, notes, and snippets.

@gonaumov
Last active May 30, 2017 20:00
Show Gist options
  • Save gonaumov/b9c6bf623fecf8539a2c4ce4c84550f9 to your computer and use it in GitHub Desktop.
Save gonaumov/b9c6bf623fecf8539a2c4ce4c84550f9 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <limits>
using namespace std;
#define MINIMUM_SIZE 2
#define MAXIMUM_SIZE 125
void dumpArray(double input[], int inputSize)
{
for (int i = 0; i < inputSize; i++)
{
cout << "position " << i << ": " << input[i] << endl;
}
}
int main()
{
int arraySize,
currentPosition = 0,
i;
double *arrayFromDoubles,
*maximum,
*minimum,
tempSwap;
cout << "Please enter array size between "
<< MINIMUM_SIZE
<< " and "
<< MAXIMUM_SIZE
<< endl;
while (!(cin >> arraySize) || arraySize < MINIMUM_SIZE || arraySize > MAXIMUM_SIZE)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Please input a proper integer number between "
<< MINIMUM_SIZE
<< " and "
<< MAXIMUM_SIZE
<< ", thank you!: "
<< endl;
}
arrayFromDoubles = new double[arraySize];
while (currentPosition < arraySize)
{
cout << "Please enter element on position ";
cout << currentPosition
<< endl;
while(!(cin >> arrayFromDoubles[currentPosition]))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Please input a proper real number, thank you!: "
<< endl;
}
currentPosition++;
}
minimum = maximum = arrayFromDoubles;
for (i = 1; i < arraySize; i++)
{
if (arrayFromDoubles[i] > *maximum)
{
maximum = &arrayFromDoubles[i];
}
else if(arrayFromDoubles[i] < *minimum)
{
minimum = &arrayFromDoubles[i];
}
}
cout << "The inputed array:"
<< endl;
dumpArray(arrayFromDoubles, arraySize);
tempSwap = *maximum;
*maximum = *minimum;
*minimum = tempSwap;
cout << "The inputed array after swapping:"
<< endl;
dumpArray(arrayFromDoubles, arraySize);
delete[] arrayFromDoubles;
arrayFromDoubles = 0;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment