create this program with everything just tell me what numbers i will have to use
ID: 3593425 • Letter: C
Question
create this program with everything just tell me what numbers i will have to use for the txt file becuase im assuming you cant attatch a text file
In this assignment you are expected to use the sorting and searching algorithms that we learned about.
You are also expected to be comfortable with both reading from and writing to a text file sequentially.
1. Prepare a file called unsortedNumbers.txt containing 10 numbers.
2. Your program should read the 10 numbers from the file unsortedNumbers.txt into an
array.
3. Sort the array using either bubble sort or selection sort.
4. Save the sorted array in a file called sortedNumbers.txt
5. Display on the screen the values of the minimum and maximum found in the file.
Since the array has already been sorted, you can select the minimum and maximum values
without going through the entire array.
6. Ask the user to enter a number. Use binary search, extended to the left and right, to find the
number of times that number appears in the sorted array.
When you think you are done, test your code with several different arrays and several different values to
search for. Search for values that appear in different parts of the array, as well as values that cannot be
found in the array at all. Search for values that appear only once in the array and search for values that
occur only once.
Explanation / Answer
The desired program is given below:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class readFile //created class for readFile
{
public:
void loadFile(); //method for loading a file having strings and save its contents to array
void bubbleSort(int* a, int n);
};
//defination of bubble sort method
void readFile::bubbleSort(int* a, int n)
{
int tempVal, i;
for( i=1;i<n;++i)
{
for(int j=0;j<(n-i);++j)
{
if(a[j]>a[j+1])
{
tempVal=a[j];
a[j]=a[j+1];
a[j+1]=tempVal;
}
}
}
cout<<"The minimum value is: "<<a[0]<<endl;
cout<<"The maximum value is: "<<a[9]<<endl;
ofstream fout;
fout.open("sortedNumbers.txt");
for(int k=0;k<10;k++)
fout<<a[k]<<endl;
fout.close();
}
void readFile::loadFile()
{
int fileArray[10];
int pos = 0;
ifstream fin("unsortedNumbers.txt");
if(fin.is_open())
{
while(!fin.eof() && pos < 10)
{
fin >> fileArray[pos];
pos++;
}
bubbleSort(fileArray, 10); //calling the bubbel sort method for sorting
}
else
{
cout<<"Failed not opne file!!"<<endl;
}
fin.close();
}
int main()
{
readFile f1;
f1.loadFile();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.