Question
C++ Help with Sorting in my code. Create functions to sort the array. I need to sort the the .txt input and output it in
C++ Help with Sorting in my code. Create functions to sort the array.
I need to sort the the .txt input and output it in a certain way that's in the examples below. It's already in arrays from the code, could really use help with sorting it.. Need main to keep main as clean as possible so need some functions, if necessary. Please add comments so i know what's going on.
Each input line has 4 attributes(num, color, shading, shape) To be considered a group, three input lines either have to match atleast 3 attributes(Examples below: Please Look at them before starting). Three input lines are also considered a group if none of their values match.
input line: num color shading shape
The numbers are ordered 1, 2, 3, the shading is ordered empty, striped, filled, the color is ordered brown, orange, yellow, and the shapes are ordered circle, square, triangle.
Each group is output by first sorting the cards in the group, and then printing out each of the cards in the group (using the same format as the input file) separated by a period and a space.
One group is output per line, and the groups are ordered based on the first input line in the group, then the second, and then the third.
Example
For this example, the input file input1.txt contains:
2 filled orange circle
1 empty yellow triangle
2 empty orange circle
3 filled brown square
1 striped yellow triangle
2 striped orange circle
1 empty brown triangle
3 empty yellow square
1 filled orange triangle
And the input file input2.txt contains
2 filled brown triangle
1 striped yellow square
1 filled brown triangle
3 filled yellow circle
Empty Striped Filled
Output from the program should look like the following (inputs are in blue text):
Enter the set input file name (or done to terminate the program):
textfileB.txt
1 empty brown triangle. 1 striped yellow triangle. 1 filled orange triangle
1 empty brown triangle. 2 empty orange circle. 3 empty yellow square
1 empty yellow triangle. 2 striped orange circle. 3 filled brown square
1 striped yellow triangle. 2 empty orange circle. 3 filled brown square
2 empty orange circle. 2 striped orange circle. 2 filled orange circle
Enter the set input file name (or done to terminate the program):
textfileB.txt
No Groups
//*****Code*****
#include
#include
#include
#include
using namespace std;
int main () {
//decalre variabe
string input;
string line;
//declare array to store int col values
int col1[1000];
//declare arrays to store the string column values
string col2[1000], col3[1000],col4[1000];
int lineno =0;
cout << "Enter in your text file name or type 'done' to end the program:" << endl;
cin>>input;
//read inpupt unitll user input done
while(strcmp(input.c_str(),"done")!=0)
{
//Read file
ifstream myfile (input.c_str());
if (myfile.is_open())
{
//lineno variabel to add row to index lineno in the array
lineno = 0;
while ( getline (myfile,line) )
{
//split the line and read into variable below
istringstream ss(line);
int c1;
string c2,c3,c4;
ss >> c1 >> c2 >> c3 >> c4 ;
//store the values in the array
col1[lineno] = c1;
col2[lineno] = c2;
col3[lineno] = c3;
col4[lineno] = c4;
lineno+=1;
}
myfile.close();
//printing content of the array
for(int i=0;i
{
cout<
}
}
else
cout << "Unable to open file";
cout << "Enter in your text file name or type 'done' to end the program:" << endl;
cin>>input;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started