I was able to write up this code, but now I need to modify it to read the array
ID: 3530512 • Letter: I
Question
I was able to write up this code, but now I need to modify it to read the array from a file named "10.txt"
I don't know how to do this, so any help is great, thanks.
#include <iostream>
#include <fstream>
using namespace std;
//Function Prototypes
void BubbleSort(int arr[],int);
void SelectionSort(int arr[],int);
int main()
{
int array1[8]={8, 1, 4, 6, 3, 2, 5, 7};
int array2[8]={8, 1, 4, 6, 3, 2, 5, 7};
cout<<"Cntents of First Array: "<<endl;
BubbleSort(array1, 8);
cout<<"Contents of Second Array: "<<endl;
for(int i=0;i<8;i++)
cout<<" "<<array2[i]<<endl;
SelectionSort(array2,8);
return 0;
}
//Function Definitions
//***********************
// Bubble Sort Function *
//***********************
void BubbleSort(int array[], int size)
{
bool swap;
int temp;
cout<<"Bubble Sort"<<endl;
do
{
swap=false;
for (int count=0;count<(size-1);count++)
{
if (array[count]>array[count+1])
{
temp=array[count];
array[count]=array[count+1];
array[count+1]=temp;
swap = true;
}
}
for(int i=0;i<size;i++)
cout<<array[i]<<" "<<endl;
} while (swap);
}
//**************************
// Selection Sort Function *
//**************************
void SelectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
cout<<"Selection Sort: "<<endl;
for (startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=array[startScan];
for(int index=startScan+1;index<size;index++)
{
if(array[index]<minValue)
{
minValue=array[index];
minIndex=index;
}
}
array[minIndex]=array[startScan];
array[startScan]=minValue;
for(int i=0;i<size;i++)
cout<<array[i]<<" "<<endl;
}
}
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
//Function Prototypes
void BubbleSort(int arr[],int);
void SelectionSort(int arr[],int);
int main()
{
int SIZE = 8;
int array1[8];
int array2[8];
int count = SIZE;
int index = 0;
char *inname = "10.txt";
ifstream infile(inname);
int i;
if (!infile) {
cout << "There was a problem opening file "
<< inname
<< " for reading."
<< endl;
return 0;
}
cout << "Opened " << inname << " for reading." << endl;
while (infile >> i &&count>0) {
array1[index] = i;
index++;
count--;
}
count = SIZE;
index = 0;
while (infile >> i &&count>0) {
array2[index] = i;
index++;
count--;
}
cout<<"Cntents of First Array: "<<endl;
BubbleSort(array1, 8);
cout<<"Contents of Second Array: "<<endl;
for(int i=0;i<8;i++)
cout<<" "<<array2[i]<<endl;
SelectionSort(array2,8);
return 0;
}
//Function Definitions
//***********************
// Bubble Sort Function *
//***********************
void BubbleSort(int array[], int size)
{
bool swap;
int temp;
cout<<"Bubble Sort"<<endl;
do
{
swap=false;
for (int count=0;count<(size-1);count++)
{
if (array[count]>array[count+1])
{
temp=array[count];
array[count]=array[count+1];
array[count+1]=temp;
swap = true;
}
}
for(int i=0;i<size;i++)
cout<<array[i]<<" "<<endl;
} while (swap);
}
//**************************
// Selection Sort Function *
//**************************
void SelectionSort(int array[], int size)
{
int startScan, minIndex, minValue;
cout<<"Selection Sort: "<<endl;
for (startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=array[startScan];
for(int index=startScan+1;index<size;index++)
{
if(array[index]<minValue)
{
minValue=array[index];
minIndex=index;
}
}
array[minIndex]=array[startScan];
array[startScan]=minValue;
for(int i=0;i<size;i++)
cout<<array[i]<<" "<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.