Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

if the text file cant be created just tell me the numbers and i will create the

ID: 3594624 • Letter: I

Question

if the text file cant be created just tell me the numbers and i will create the text file myself

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.

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
int index = 0,i, temp, j;
int a[10];
ifstream inFile;
  
// file opening
ofstream myfile ("sortedNumbers.txt");
inFile.open("unsortedNumbers.txt");
  
// TASK 2: reading file and storing values in array
while (inFile >> a[index++]);
  
// TASK 3: BUBBLE SORT
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
if (a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
  
// TASK 4: WRITING to file
for(i=0;i<10;i++)
{
myfile << a[i] << " ";
}
  
myfile.close();
inFile.close();
return 0;
}

Task 1: unsortedNumbers.txt

2 3 4 5 6 2 3 4 5 1

sortedNumbers.txt

1 2 2 3 3 4 4 5 5 6

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
int index = 0,i, temp, j;
int a[10];
ifstream inFile;
  
// file opening
ofstream myfile ("sortedNumbers.txt");
inFile.open("unsortedNumbers.txt");
  
// TASK 2: reading file and storing values in array
while (inFile >> a[index++]);
  
// TASK 3: BUBBLE SORT
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
if (a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
  
// TASK 4: WRITING to file
for(i=0;i<10;i++)
{
myfile << a[i] << " ";
}
  
myfile.close();
inFile.close();
return 0;
}

Task 1: unsortedNumbers.txt

2 3 4 5 6 2 3 4 5 1

sortedNumbers.txt

1 2 2 3 3 4 4 5 5 6