Write a C++ program to find the kth largest number among N numbers. You may assume 0 < k <= N. The numbers are in an input file. Input value of k from the user. Read the first k numbers into an array and sort them into non-increasing order. Next, read the remaining numbers one by one (until end-of-file). As a new number is read, if it is smaller than the number in position k-1 in the array, it is ignored. Otherwise, it is placed in its correct position in the array, bumping out the number in position k-1. When all numbers are read, the number in position k-1 is the answer.
Explanation::
CODE in C++::
#include <iostream>
#include <fstream>
using namespace std;
void sort(int arr[],int k);
int main() {
/**
* We will scan numbers from a file named input.txt
* In input.txt file, numbers are stored as
* one number per line
*/
ifstream infile("input.txt");
int n,k,i=0;
cout<<"Enter the value of k : ";
cin>>k;
/**
* Creating array of size k
*/
int arr[k];
while(infile>>n){
if(i<k){
arr[i]=n;
i++;
}else{
/**
* Sorting in non-increasing order
*/
sort(arr,k);
if(n>=arr[k-1]){
arr[k-1]=n;
}
}
}
sort(arr,k);
cout<<"The kth Largest number is "<<arr[k-1]<<endl;
}
void sort(int arr[],int k){
for(int i = 0; i < k - 1; i++)
{
for(int j = 0; j < k - 1 - i; j++)
{
if(arr[j] < arr[j + 1])
{
int num = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = num;
}
}
}
}
input.txt ::
12
4
5
6
7
3
4
5
6
67
78
OUTPUT:
Enter the value of k : 5
The kth Largest number is 6
Please provide the feedback!!
Thank You!!
Write a C++ program to find the kth largest number among N numbers. You may assume...
Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...
Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...
Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 different arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times. In the body of the loop, Create an array of n random integers (Make sure...
c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...
Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...
This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...
In C++ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding...
Please code in C thank you.
1. Create an array with 20 random numbers between 1 and 100. Incorporate the following specifications. • Send the base address of the array and an integer to a function called sort(). • The integer is a user input which is either 0 or 1. If the user does not enter either 0 or 1, keeping asking the user until you get the right input. • If the user input is 0, sort the...