Quote
And this teaches the programmer what? That there is a canned function for damn near everything?
yes
Posted 07 October 2011  07:26 AM
const int x = 10; int arr[x] = {5, 22, 4, 63, 112, 49, 27, 32, 9, 87}; // 4th lowest is 22 int nth = 4; int curLow = 0; int z = 0; for(int i = 0; i < x; i++) { curLow = 0; for(z = 0; z < x; z++) { if(arr[z] < arr[i]) { curLow++; } } if(curLow == nth1) { cout << arr[i] << endl; //This is your solution //break; } }
This post has been edited by Wuzseen: 07 October 2011  07:31 AM
Posted 28 December 2012  12:42 PM
baavgai, on 21 August 2011  01:03 PM, said:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int findNSmallest(const int *a, const int size, const int n) { // find the first smallest int smallest = a[0]; for(int i=1; i<size; i++) { if (a[i]<smallest) { smallest = a[i]; } } for(int i=0; i<n1; i++) { // find the next smallest int found = 1; for(int i=0; i<size; i++) { if (a[i]>smallest && (found==1  a[i]<a[found])) { found = i; } } if (found==1) { cout << endl << "invalid request" << endl; break; } smallest = a[found]; } return smallest; } // because I'm lazy void test(const int size, const int min, const int max, const int n) { cout << "size=" << size << " n=" << n << " range=" << "(" << min <<""<<max<<")"<<endl; int range = (max  min) + 1; int *data = new int[size]; for(int i=0; i<size; i++) { data[i] = (rand() % range) + min; } for(int i=0; i<size; i++) { cout << data[i] << ' '; } cout << endl << n << "th smallest = " << findNSmallest(data, size, n) << endl << endl; delete [] data; } int main() { srand ( time(NULL) ); test(20, 1, 100, 3); test(20, 1, 100, 7); // this will often fail because of lack of result domain test(10, 1, 10, 6); test(10, 1, 10, 6); return 0; }
Posted 28 December 2012  01:30 PM
Posted 28 December 2012  01:34 PM
Posted 29 December 2012  08:06 PM
#include <iostream> #include <algorithm> #include <limits> int findNthSmallest(int *arr, int size, int nth) { int index = 0; while(nth) { index = std::distance(arr, std::min_element(arr, arr + size)); if (nth) arr[index] = std::numeric_limits<int>::max(); } return arr[index]; }
Query failed: unknown local index 'forums_search_posts_main' in search request.
