I need an example for bubble sort and selection sort that will work for this code. CensusData.csv is in the form of: Alabama 4874747 3779274 77.5 I need to understand how to get it to compile. #include #include #include #include #include using namespace std; //create structure for CensusData struct CensusData { string state, totPop, adultPop, percentPop; }; void bubbleSort(CensusData CenDataArr[], int); void selectionSort(CensusData CenDataArr[], int ); void startarray(CensusData CenDataArr[]); int main() { CensusData CenDataArr[52]; //create structure array startarray(CenDataArr); selectionSort(CenDataArr,52); cout< intData2) //if the first value it looks at is greater, then it will swap it { // INSERT YOUR CODE HERE } } } while (L_madeAswap); // Loop again if a swap occurred on this pass. cout<<"Bubble sort has made "<
I need an example for bubble sort and selection sort that will work for this code. CensusData.csv is in the form of:
Alabama | 4874747 | 3779274 | 77.5 |
I need to understand how to get it to compile.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
//create structure for CensusData
struct CensusData
{
string state, totPop, adultPop, percentPop;
};
void bubbleSort(CensusData CenDataArr[], int);
void selectionSort(CensusData CenDataArr[], int );
void startarray(CensusData CenDataArr[]);
int main()
{
CensusData CenDataArr[52]; //create structure array
startarray(CenDataArr);
selectionSort(CenDataArr,52);
cout<<endl;
startarray(CenDataArr);//run bubble sort
BubbleSort(CenDataArr,52);
return 0;
}
void startarray(CensusData CenDataArr[])
{
//intermittent variable to hold string variables temporarily
string holdData;
//declare MyData as my designated file
ifstream MyData;
//open the file designated by MyData
MyData.open ("CensusData.CSV");
//utilize an if check to see if the file exists,
//if it doesn't then an error will be returned
if (!MyData)
cout<<"The file could not be found";
//proceed with program if the file exists
else
{ //begin loop to enter in all string items of structure
for (int i = 0; i <52;i++)
{
getline(MyData,CenDataArr[i].state,','); //each getline uses a comma as a delimiter
getline(MyData,CenDataArr[i].totPop,',');
getline(MyData,CenDataArr[i].adultPop,',');
getline(MyData,CenDataArr[i].percentPop);
}
}
//close input file now.
MyData.close();
}
void bubbleSort(CensusData CenDataArr[], int size)
{
CensusData temp[1]; //turn temporary variable in to structure object to hold values
bool L_madeAswap; //bool flag to determine if a swap was made
long intData = 0, intData2 = 0; //number values to hold converted formats
int swapsMade = 0; //counter to determine how many swaps are made
do //do loop makes sure the swap goes through the list at least once
{ L_madeAswap = false;
for (int count = 0; count < (size - 1); count++)
{
//These four lines are crucial as they convert the string variables to long data types for comparison
// INSERT YOUR CODE HERE
if (intData > intData2) //if the first value it looks at is greater, then it will swap it
{
// INSERT YOUR CODE HERE
}
}
} while (L_madeAswap); // Loop again if a swap occurred on this pass.
cout<<"Bubble sort has made "<<swapsMade<<" swaps which created the following array:\n\n";
// Format headings
cout << "BUBBLE SORT:" << endl;
cout<<fixed<<setw(21)<<left<<"State/Territory"
<<fixed<<setw(20)<<"Total Population"
<<fixed<<setw(20)<<"Adult Population"
<<fixed<<setw(5)<<"Percentage"<<endl;
cout<<"\n----------------------------------------------------------------------------\n\n";
ofstream writeFile;
writeFile.open("CensusDataBubbleSorted.csv");
if (!writeFile)
cout<<"Error, bubble sort file couldn't be opened";
else
{
// Display data in order
for(int i = 0; i < size;i++)
{ cout<<fixed<<setw(21)<<CenDataArr[i].state
<<fixed<<setw(20)<<CenDataArr[i].totPop
<<fixed<<setw(20)<<CenDataArr[i].adultPop
<<fixed<<setw(20)<<CenDataArr[i].percentPop
<<endl;
writeFile << CenDataArr[i].state<<","<<CenDataArr[i].totPop<<","
<<CenDataArr[i].adultPop<<","<<CenDataArr[i].percentPop<<"\n";
}
writeFile.close();}
}
/**************************************************************
* selectionSort *
* This function performs an ascending-order selection sort *
* on array. The parameter size holds the number of elements *
* in the array. *
**************************************************************/
void selectionSort(CensusData CenDataArr[], int size)
{
int num1,num2, minIndex,swapsMade2 = 0; //all of these will hold a temporary value
CensusData minValue[1]; // this creates an object to hold temporary object values
for (int startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan; //the first index starts at 0
minValue[0] = CenDataArr[startScan]; //the minimum value is the very first number now
for(int index = startScan + 1; index < size; index++)
{
//these next four lines change the string variables in to usable integer types for comparison
// INSERT YOUR CODE HERE
if (num1 < num2) //if the number we have is less than number 2
{
// INSERT YOUR CODE HERE
}
}
// INSERT YOUR CODE HERE TO change the list to reflect the lowest value we found
// INSERT YOUR CODE HERE TO add the smallest value and repeat the loop
}
// Format headings
// INSERT YOUR CODE HERE FOR SELECTION SORT HEADINGS
ofstream writeFile; // create an ofstream object called writefile
writeFile.open("CensusDataSelectionSorted.csv"); //determine new destination
if (!writeFile)
cout<<"Error, selection sort file couldn't be opened"; //error handling if it cannot be found
else
{
// INSERT YOUR CODE HERE TO DISPLAY THE SELECTION SORT OUTPUT WHILE WRITING EACH RECORD
for(int i = 0; i < size;i++)
writeFile << CenDataArr[i].state<<","<<CenDataArr[i].totPop<<","<<CenDataArr[i].adultPop<<","<<CenDataArr[i].percentPop<<"\n";
writeFile.close();}
cout<<"selection sort made "<<swapsMade2<<" swaps.";
}
Step by step
Solved in 4 steps with 3 images