+ 1

PLEASE CHECK IF I AM RIGHT Write a program to input 10 integer numbers into an array named fmax and determine the maximum value

/* Given an array arr[] and an integer k, we neex to pring k maximum element of givn array. */ #include <iostream> // #include <unistd.h> using namespace std; void printMax(int arr[], int k, int n) { for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } cout << "Sorted Array: "; for(int i=0; i<n; i++) { cout << arr[i] << " " ;// Sleep(1000); } cout << endl; // find k max elements in array cout << "First " << k << " max elements in array are: "; for(int i=n-1; i>=n-k; i--) { cout << arr[i] << " ";// Sleep(1000); } /* if array is sorted in descending order use this loop - for(int i=0; i<k; i++){ - cout << arr[i] << " "; Sleep(1000); - } */ } int main() { int arr[5] = {85,26,14,96,41}; int n = sizeof(arr) / sizeof(arr[0]); int k = 2; printMax(arr, k, n); return 0; }

14th Jun 2020, 9:28 AM
AmgKhvbbi Schweppes
AmgKhvbbi Schweppes - avatar
4 Antworten
+ 1
yes we are allowed
14th Jun 2020, 11:11 AM
AmgKhvbbi Schweppes
AmgKhvbbi Schweppes - avatar
+ 1
Then use the max_element() function in the algorithm header file
14th Jun 2020, 7:24 PM
Anthony Maina
Anthony Maina - avatar
+ 1
how
14th Jun 2020, 7:32 PM
AmgKhvbbi Schweppes
AmgKhvbbi Schweppes - avatar
0
Are you allowed to use built-in features like <algorithm> header? or should you write your own implementation for sorting?
14th Jun 2020, 10:58 AM
Ipang