Hell that last one was complex.
Just sort the array with qsort (see link) and then take the [0] and [max] values.
How do i manipulate an array using visual c++ such that i can find the max and min values?
RTFM?
Please read the link
Reply:#include %26lt;iostream%26gt;
#include %26lt;algorithm%26gt;// for C++ sort function
using namespace std;
const int SIZE = 10000;
void GetInputArray (int%26amp; userSize, int originalArray []);
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray []);
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray []);
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray []);
int main ()
{
int originalArrayMain [SIZE];
int lowSortedArrayMain [SIZE];
int highSortedArrayMain [SIZE];
int lowSplitArrayMain [SIZE];
int highSplitArrayMain [SIZE];
int userSizeMain;
int minimum, maximum;
GetInputArray (userSizeMain, originalArrayMain);
if (userSizeMain %26gt; 0)
{
FindMinMax (userSizeMain, minimum, maximum, originalArrayMain);
Sort (userSizeMain, originalArrayMain, lowSortedArrayMain, highSortedArrayMain);
SplitArrayProcessing (userSizeMain, originalArrayMain, lowSplitArrayMain, highSplitArrayMain);
//outputs
cout %26lt;%26lt; "Min: " %26lt;%26lt; minimum %26lt;%26lt; endl;//minimum number in array
cout %26lt;%26lt; "Max: " %26lt;%26lt; maximum %26lt;%26lt; endl;//maximum number in array
cout %26lt;%26lt; "Low to High Sort: ";//low to high sorted array
for (int i = 0; i %26lt;= userSizeMain - 1; i++)
{
cout %26lt;%26lt; lowSortedArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl %26lt;%26lt; "High to Low Sort: ";//high to low sorted array
for (int i = 0; i %26lt;= userSizeMain - 1; i++)
{
cout %26lt;%26lt; highSortedArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
}
cout %26lt;%26lt; "First half split of array: ";//first half split of array
for (int i = 0; i %26lt;= ((userSizeMain/2) - 1); i++)
{
cout %26lt;%26lt; lowSplitArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl;
cout %26lt;%26lt; "Second half split of array: ";// second half split of array
for (int i = 0; i %26lt; ((userSizeMain+1)/2); i++)
{
cout %26lt;%26lt; highSplitArrayMain [i] %26lt;%26lt; " ";
}
cout %26lt;%26lt; endl %26lt;%26lt; endl;
cout %26lt;%26lt; endl;
system("pause");
return 0;
};
void GetInputArray (int%26amp; userSize, int originalArray [])//Gets input array from user
{
cout %26lt;%26lt; "How many integers do you want to put into the array?: " %26lt;%26lt; endl;
cin %26gt;%26gt; userSize;
cout %26lt;%26lt; endl;
if (userSize %26gt; 0)
{
cout %26lt;%26lt; "Enter " %26lt;%26lt; userSize %26lt;%26lt; " integer(s) into the array (e.g. 5 88 4 etc):" %26lt;%26lt; endl;
for (int i = 0; i %26lt;= userSize - 1; i++)
{
cin %26gt;%26gt; originalArray [i];
}
cout %26lt;%26lt; endl;
}
else
{
cout %26lt;%26lt; "Invalid Entry!" %26lt;%26lt; endl;
}
}
void FindMinMax (int userSize, int%26amp; min, int%26amp; max, int originalArray [])
{
//find the mimimum number in the array
min = originalArray [0];
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (originalArray [i] %26lt;= min)
{
min = originalArray [i];
}
}
//find the maximum number in the array
max = originalArray [0];
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (originalArray [i] %26gt;= max)
{
max = originalArray [i];
}
}
}
void Sort (int userSize, int originalArray [], int lowSortedArray [], int highSortedArray [])
{
int j = (userSize - 1);
//copy contents of originalArray into lowSortedArray
for (int i = 0; i %26lt;= userSize - 1; i++)
{
lowSortedArray [i] = originalArray [i];
}
//sorts the lowSortedArray from low to high
sort (lowSortedArray, lowSortedArray + userSize);
//reverse copies the lowSortedArray to the highSortedArray (high to low sorted array)
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (j %26gt;= 0)
{
highSortedArray [j] = lowSortedArray [i];
j--;
}
}
}
void SplitArrayProcessing (int userSize, int originalArray [], int lowSplitArray [], int highSplitArray [])
{
int j = 0;
//splits the array in half
for (int i = 0; i %26lt;= userSize - 1; i++)
{
if (i %26lt; (userSize/2))
{
lowSplitArray [i] = originalArray [i];
}
else
{
highSplitArray [j] = originalArray [i];
j++;
}
}
}
Reply:// this is a simple example, but not necessarily the most efficient
int myArray[5] = {6,1,3,8,2}; // an array of 5 random integers
int min = myArray[0]; // use the first value in the array
int max = myArray[0]; // use the first value in the array
// the loop below compares min and max to each value in the array
for(int i = 1; i %26lt; 5; i++) // 5 is the size of the array
{
if(min %26gt; myArray[i]) min = myArray[i];
if(max %26lt; myArray[i]) max = myArray[i];
}
cout %26lt;%26lt; "MIN = " %26lt;%26lt; min %26lt;%26lt; endl; // prints the lowest number
cout %26lt;%26lt; "MAX = " %26lt;%26lt; max %26lt;%26lt; endl; // prints the highest number
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment