Question

In: Computer Science

Programming lang C++ Given a file of 100,000 unsorted integers, write a program that reads those...

Programming lang C++

Given a file of 100,000 unsorted integers, write a program that reads those integers into an array and sorts the integers in the array. The program should then prompt the user for an index, and then display the integer at that index.

For example, if the array contains ten integers and the contents are

79 4 42 51 12 22 33 17 91 10

after sorting, the integer at index 6 is 42

You are encouraged to use this data to test your program.

Solutions

Expert Solution

Following is the solution to above problem

In this i have used merge sort and fstream

Code-

--------------------------------------------------------------------------------------------------------

#include<stdlib.h>
#include<stdio.h>
#include <fstream>
#include<iostream>

using namespace std;

//merging two sub arrays
void merge(int s[], int l, int m, int r)
{
   int i, j, k;
   int n1 = m - l + 1;
   int n2 = r - m;

   int L[n1], R[n2];

   for (i = 0; i < n1; i++)
       L[i] = s[l + i];
   for (j = 0; j < n2; j++)
       R[j] = s[m + 1+ j];

   i = 0;
   j = 0;
   k = l;
   while (i < n1 && j < n2)
   {
       if (L[i] <= R[j])
       {
           s[k] = L[i];
           i++;
       }
       else
       {
           s[k] = R[j];
           j++;
       }
       k++;
   }

   //remaining elements of first temp array
   while (i < n1)
   {
       s[k] = L[i];
       i++;
       k++;
   }

   //remaining elements of second temp array
   while (j < n2)
   {
       s[k] = R[j];
       j++;
       k++;
   }
}


void mergeSort(int s[], int a, int b)
{
   if (a < b)
   {

       int m = (a+b)/2;

       mergeSort(s, a, m); //first half
       mergeSort(s, m+1, b); //second half

       merge(s, a, m, b); // send to combine array
   }
}


int main()
{
   int sum = 0,i=0,size;
   cout<<"Enter no of intergers that file contains :";
   cin>>size;
int m,ui[size];
int index;
ifstream file;

file.open("a.txt");
//check if file is opened correctly
if (!file) {
cout << "Unable to open file";
exit(1);
}

while (file >> m) {
ui[i]=m;
i++;
}

file.close();

mergeSort(ui, 0, size - 1);

cout<<"\nSorted Array is : "<<"\n";

for(i=0;i<10;i++)
cout<<ui[i]<<" ";

cout<<"\nEnter index to display sorted integer - ";
cin>>index;
cout<<"\nSorted integer is :"<<ui[index];

return 0;
}
-------------------------------------------------------------------


Related Solutions

Programming lang C++ Write a program that reads 10,000 words into an array of strings. The...
Programming lang C++ Write a program that reads 10,000 words into an array of strings. The program will then read a second file that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list.
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
In C++, write a program that reads data from a text file. Include in this program...
In C++, write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global variables are the actual data points, the mean, the standard deviation, and the number of data entered. All other variables must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function... ALL LINES...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv),...
In this programming assignment, you will write a program that reads in the CSV file (passenger-data-short.csv), which contains passenger counts for February 2019 on 200 international flights. The data set (attached below) is a modified CSV file on all International flight departing from US Airports between January and June 2019 reported by the US Department of Transportation. You write a program that give some summary statistics on this data set. Create a header file named flights.h. In this file, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT