#include
using std::cin;
using std::cout;
using std::endl;
double minimum(double myArray[], const intarraySize);
double maximum(double myArray[], const intarraySize);
int main()
{
const int arraySize = 10;
double myArray[arraySize] = {0.0};
cout << "Enter array elements" << endl;
for (int index = 0; index < arraySize; index++)
{
cout << "Enter " << index << " element: ";
cin >> myArray[index];
}
cout << "Minimum is " << minimum(myArray, arraySize) << endl;
cout << "Maximum is " << maximum(myArray, arraySize) << endl;
system("PAUSE");
return 0;
}
double minimum(double myArray[], const int arraySize)
{
double min = myArray[0];
for (int index = 1; index < arraySize; index++)
{
if (min > myArray[index])
{
min = myArray[index];
}
}
return min;
}
double maximum(double myArray[], const intarraySize)
{
double max = myArray[0];
for (int index = 1; index < arraySize; index++)
{
if (max < myArray[index])
{
max = myArray[index];
}
}
return max;
}
*The program was compiled in VS2010.
1010
To find the kth smallest number in an unsorted array, you can use a sorting algorithm like quicksort or heapsort to arrange the array in ascending order. Then, you can simply access the kth element in the sorted array to find the kth smallest number. This process ensures that the kth smallest number is easily identified and retrieved from the array.
by using index position we can find the particular element in array.
Your best bet would probably be to iterate through the array using a for loop and compare each value to the current low and high values (which you would store in a local variable) for example: for each element in array { if current is less than lowest_value lowest_value = current else if current is greater than highest_value highest_value = current }
(array.length - 1) will find the index of the last element in an array (or -1 if the array is empty).
Basically, &array[i]; That is, the memory location for an array object with index i. Or, you can do: (array + i);
To search, you would start with the first element of the array and compare it with the target value. If the first element matches the target, you found it. If not, you would move to the next element in the array and repeat the process until either you find the target or exhaust all elements in the array.
The smallest is 0o0'0'' and the largest is 180o0'0''
In a binary search algorithm, typically log(n) comparisons are required to find a specific element in a sorted array, where n is the number of elements in the array.
/* to find smallest income*/ double smalincome=income[0]; for(i=1;i<=n;i++) { if(income[i]<smalincome) { smalincome=income[i]; } } System.out.println("The smallest income is" +smalincome);
The operation you describe is not a recursive one, it is iterative (linear in nature). It's also better to return the index of the smallest value rather than just the value itself. In this way we know the position of the smallest value as well as the value itself. To find the smallest element in an array, we start with the first element. Being the only value encountered so far, it must be the smallest, so we store its index. We then compare the value at the stored index to that of each of the remaining elements, one by one. When we find a smaller value, we update the stored index, because that value is the smallest encountered so far. When we reach the end of the array, the stored index will tell us which element was the smallest, so we simply return that index. The algorithm can (and should) be generalised to cater for arrays of any length. // Returns the index of the smallest value in array a of length n int smallest (const int* const a, int n) { if (n<1) return -1; // sanity check: the array must have 1 or more elements to be valid int i, j; i = 0; // assume first element holds smallest value until proven otherwise for (j=1; j<n; ++j) if (a[j] < a[i]) i = j; // update i each time a smaller value is found at index j return i; // a[i] holds the smallest value in the array } int main (void) { const int max = 10; int a[max] = {5, 4, 6, 3, 0, 7, 2, 8, 1, 9}; int i; i = smallest (a, max); assert (i==4); // a[4] holds the smallest value return 0; }
The maximum number of comparisons required in a binary search algorithm to find a specific element in a sorted array is log(n), where n is the number of elements in the array.