Sorting Arrays and Structs | Newbie | | Join Date: Sep 2007
Posts: 17
| |
I am trying to write a program that reads in data from a user-specified file. I have to sort this file two ways, one using parallel arrays and the other using structs. I then have to output the corrected files to the user. Each file contains up to 10 different sets of data, with each set containing four members. Such as this:
Dell 512 1.7 120
HP 1024 1.4 80
Compaq 1024 1.6 100
Toshiba 512 1.8 160
Lenovo 2048 1.5 120
I have almost all of my code written, but cannot get the sort functions to work. Here is the void function I have: - void sortstring (int numbercomputers, string brand[], int memory[], float CPU[], int storage[], int size, int parameter){
-
-
-
if(parameter == 1){
-
for(int i=0; i<size-1; i++){
-
int small = i;
-
for(int j=i; j<size; j++){
-
if(brand[j] < brand[small]){
-
small = j;}
-
-
}
-
}
-
}
-
else if(parameter == 2){
-
for(int i=0; i<size-1; i++){
-
int small = i;
-
for(int j=i; j<size; j++){
-
if(memory[j] < memory[small]){
-
small = j;}
-
}
-
}
-
}
-
else if(parameter == 3){
-
for(int i=0; i<size-1; i++){
-
int small = i;
-
for(int j=i; j<size; j++){
-
if(CPU[j] < CPU[small]){
-
small = j;}
-
}
-
}
-
}
-
else{
-
for(int i=0; i<size-1; i++){
-
int small = i;
-
for(int j=i; j<size; j++){
-
if(storage[j] < storage[small]){
-
small = j;}
-
}
-
}
-
}
-
-
}
I know this does not work, and is certainly not complete. But I am trying to sort the data in ascending order, based on which criteria the user specifies. However, each line must remain intact...so if I get the brand name in order, the other data must follow. Any suggestions as to how to fix this would be greatly appreciated!
|  | Expert | | Join Date: Feb 2007
Posts: 839
| | | re: Sorting Arrays and Structs Quote:
Originally Posted by aemado I am trying to write a program that reads in data from a user-specified file. I have to sort this file two ways, one using parallel arrays and the other using structs. I then have to output the corrected files to the user. Each file contains up to 10 different sets of data, with each set containing four members. Such as this:
Dell 512 1.7 120
HP 1024 1.4 80
Compaq 1024 1.6 100
Toshiba 512 1.8 160
Lenovo 2048 1.5 120
I have almost all of my code written, but cannot get the sort functions to work. Here is the void function I have:
void sortstring (int numbercomputers, string brand[], int memory[], float CPU[], int storage[], int size, int parameter){
if(parameter == 1){
for(int i=0; i<size-1; i++){
int small = i;
for(int j=i; j<size; j++){
if(brand[j] < brand[small]){
small = j;}
}
}
}
else if(parameter == 2){
for(int i=0; i<size-1; i++){
int small = i;
for(int j=i; j<size; j++){
if(memory[j] < memory[small]){
small = j;}
}
}
}
else if(parameter == 3){
for(int i=0; i<size-1; i++){
int small = i;
for(int j=i; j<size; j++){
if(CPU[j] < CPU[small]){
small = j;}
}
}
}
else{
for(int i=0; i<size-1; i++){
int small = i;
for(int j=i; j<size; j++){
if(storage[j] < storage[small]){
small = j;}
}
}
}
}
I know this does not work, and is certainly not complete. But I am trying to sort the data in ascending order, based on which criteria the user specifies. However, each line must remain intact...so if I get the brand name in order, the other data must follow. Any suggestions as to how to fix this would be greatly appreciated! Everytime you swap you need to swap in all the other arrays too. Also, take a look at this.
ps. I had a hard time reading your code, please use code tags next time.
| | Newbie | | Join Date: Sep 2007
Posts: 17
| | | re: Sorting Arrays and Structs Quote:
Originally Posted by ilikepython Everytime you swap you need to swap in all the other arrays too. Also, take a look at this.
ps. I had a hard time reading your code, please use code tags next time. Will the code work the way I have it structured....with the if(parameter)..else(parameter)...layout??
and by swap the others, is this what you mean? - temp = brand[j];
-
brand[j] = brand[j+1];
-
brand[j+1] = temp;
-
-
temper = memory[j];
-
memory[j] = memory[j+1];
-
memory[j+1] = temper;
-
-
tmp = CPU[j];
-
CPU[j] = CPU[j+1];
-
CPU[j+1] = tmp;
-
-
tem = storage[j];
-
storage[j] = storage[j+1]
-
storage[j+1] = tem
below the final if statement in each? I am very new to this, and don't know what you mean by code tags. Do you mean comments?
Thank you so much!
|  | Expert | | Join Date: Feb 2007
Posts: 839
| | | re: Sorting Arrays and Structs Quote:
Originally Posted by aemado Will the code work the way I have it structured....with the if(parameter)..else(parameter)...layout??
and by swap the others, is this what you mean?
temp = brand[j];
brand[j] = brand[j+1];
brand[j+1] = temp;
temper = memory[j];
memory[j] = memory[j+1];
memory[j+1] = temper;
tmp = CPU[j];
CPU[j] = CPU[j+1];
CPU[j+1] = tmp;
tem = storage[j];
storage[j] = storage[j+1]
storage[j+1] = tem
below the final if statement in each? I am very new to this, and don't know what you mean by code tags. Do you mean comments?
Thank you so much! Well, your ifs will most likely work, but it's not usually good to have if statements that do the same thing more or less.
First I think it is a good idea to have a generic swap function: -
template <class T>
-
void swap(T &a, T &b)
-
{
-
T temp = a
-
a = b;
-
b = a;
-
}
-
Now, the only way your code differs from different ifs is that different arrays are being checked. Everything else stays the same: -
for .... // your algorithm
-
for....
-
if parameter is 1{
-
check with brand}
-
if parameter is 2{
-
check with memory}
-
... so on ...
-
... decide if you need to swap ...
-
swap all four:
-
swap<string> (brand[1], brand[2]) // whichever elements need swapping
-
swap<int> (memory[1], memory[2])
-
Also, did you read the article I gave you a link to? In the articles seciton, there is also an article about the selection sort that is much faster and not really harder.
Code tags are stuff you put around your code to make it look nice:
[ c o d e = c p p ] ... code here ... [ / c o d e ], without the spaces though.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,370
| | | re: Sorting Arrays and Structs
Hold on a minute. You are using a C++ string as an argument to your sort. Therefore, you can use the STL sort. Why are you wasting time re-inventing the wheel??
Just write a compare function that returns true when the first argument is less than the second argument.
Then call the sort.
| | Newbie | | Join Date: Sep 2007
Posts: 17
| | | re: Sorting Arrays and Structs
I am very new to this, and don't know what you mean by STL. In the guidelines, I have to sort this data using parallel arrays, and then sort the same data using structs (both of which end up giving the same result). I have gotten the array sort to work, but am having one final problem that I cannot figure out. When I try to sort using the fourth parameter, it simply doesn't work. It runs through the loop once, and sorts the first two, but then stops. The code works the other three, and it is almost exactly the same! Here is my void function...any suggestions you may have would be great! Thanks again! |  | Expert | | Join Date: Feb 2007
Posts: 839
| | | re: Sorting Arrays and Structs Quote:
Originally Posted by aemado I am very new to this, and don't know what you mean by STL. In the guidelines, I have to sort this data using parallel arrays, and then sort the same data using structs (both of which end up giving the same result). I have gotten the array sort to work, but am having one final problem that I cannot figure out. When I try to sort using the fourth parameter, it simply doesn't work. It runs through the loop once, and sorts the first two, but then stops. The code works the other three, and it is almost exactly the same! Here is my void function...any suggestions you may have would be great! Thanks again! -
void bubbleSort(string brand[], int memory[], float CPU[], int storage[], int &numbercomputers, int parameter)//Bubble sort function
-
{
-
if(parameter == 1){
-
-
int i,j;
-
for(i = 0; i<numbercomputers; i++)
-
{
-
for(j=0; j<i; j++)
-
{
-
if(brand[i]<brand[j])
-
{
-
string temp=brand[i]; //swap
-
brand[i]=brand[j];
-
brand[j]=temp;
-
-
int temper = memory[i];
-
memory[i] = memory[j];
-
memory[j] = temper;
-
-
float tmp = CPU[i];
-
CPU[i] = CPU[j];
-
CPU[j] = tmp;
-
-
int tem = storage[i];
-
storage[i] = storage[j];
-
storage[j] = tem;
-
}
-
}
-
}
-
}
-
There is simply no need for all that same code (I think, at least).: -
int i,j;
-
for(i = 0; i<numbercomputers; i++)
-
{
-
for(j=0; j<i; j++)
-
{
-
bool doSwap = false;
-
if(brand[i]<brand[j] && parameter == 1)
-
{
-
doSwap = true;
-
}
-
else if(memory[i]<memory[j] && parameter == 2)
-
{
-
doSwap = true;
-
}
-
else if(CPU[i]<CPU[j] && parameter == 3)
-
{
-
doSwap = true;
-
}
-
else if(storage[i]<storage[j] && parameter == 4)
-
{
-
doSwap = true;
-
}
-
if (doSwap)
-
{
-
int temp=storage[i]; //swap
-
storage[i]=storage[j];
-
storage[j]=temp;
-
-
string tem=brand[i]; //swap
-
brand[i]=brand[j];
-
brand[j]=tem;
-
-
float tmp = CPU[i];
-
CPU[i] = CPU[j];
-
CPU[j] = tmp;
-
-
int temper=memory[i]; //swap
-
memory[i]=memory[j];
-
memory[j] = temper;
-
}
-
}
-
}
-
This is much more shorter.
Btw, I think the problem with your code is your second for loop. "for (int j = i; j < 1; j++)" (you put a one).
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,370
| | | re: Sorting Arrays and Structs Quote:
Originally Posted by aemado I am very new to this, and don't know what you mean by STL. The STL is the Standard Template Library. It is also called the C++ Standard Library.
In the STL are all commonly used data structures and algorithms. Bascially, you have a choice: Learn to use the solutions provided, or rewrite everything all over again.
The sort is an algorithm. You pass in the thing to sort, the number of elements, and the address of a function that compare's two elements and returns less if the first argument is less than the second argument.
Here is all you do: -
//Dell 512 1.7 120
-
struct Data
-
{
-
string name;
-
int value1;
-
double value2;
-
int value3;
-
};
-
-
bool CompareDataByName(Data& left, Data& right)
-
{
-
if (left.name < right.name) return true;
-
return false;
-
}
-
int main()
-
{
-
const int NUM = 10;
-
Data array[NUM];
-
//load the array elements
-
sort(array, array+10, CompareDataByName);
-
-
}
-
I say again you don't need to be writing sorts unless this is some type of homework assignment.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|