1

Basicly Here is my assignment I'M NOT ASKING YOU TO COMPLETE THIS ASSIGNMENT, JUST ASSIST ME.

Sort an Array of Structs

In this assignment, you are to create an array of structs and sort the array.

  1. create a struct with (at least) 3 fields
  2. create an array of structs
  3. read data into the array of structs (10 to 15 records)
  4. print the array
  5. sort the array (in ascending order) on 1 field of the struct
  6. print the array
  7. sort the array (in descending order) on another field of the struct
  8. print the array

I am currently stuck on step 5.

this is my program as of now.

#include <iostream>
#include <sstream>
using namespace std;

struct data {
  int a,b,c;
} number [10];

int main(){
 int enterData;
 int *temp = new int[3];

 for (int i = 0; i<10; i++){
  //for (int n = 0; n<=3; n++){
   cin>> number[i].a;
   cin>> number[i].b;
   cin>> number[i].c;
  if(i >= 10) break;
  //}
 }

 for (int i = 0; i<10; i++){
 // for (int n = 0; n<=3; n++){
   cout << number[i].a << " ";
   cout << number[i].b << " ";
   cout << number[i].c << " ";
   cout << "\n";
   if(i >= 10) break;
 // }
 }

 cout <<"\n\n\n";

 for (int i = 0; i<9; i++){
  if (number[i].a > number[i+1].a){
   temp[0] = number[i].a;
   temp[1] = number[i].b;
   temp[2] = number[i].c;
   number[i].a = number[i+1].a;
   number[i].b = number[i+1].b;
   number[i].c = number[i+1].c;
   number[i+1].a = temp[0];
   number[i+1].b = temp[1];
   number[i+1].c = temp[2];
  }
 }
 for (int i = 0; i<10; i++){
 // for (int n = 0; n<=3; n++){
   cout << number[i].a << " ";
   cout << number[i].b << " ";
   cout << number[i].c << " ";
   cout << "\n";
   if(i >= 10) break;
 // }
 }
}

And this is my current output.

Numbers Entered in to the Program 3 4 8 7 6 4 2 9 0 1 4 2 5 3 6 7 3 2 3 4 5 6 7 8 9 5 4 7 5 1

First "Sort" 3 4 8 2 9 0 1 4 2 5 3 6 7 6 4 3 4 5 6 7 8 7 3 2 7 5 1 9 5 4

3 Answers 3

3

In this code

 for (int i = 0; i<10; i++){
  //for (int n = 0; n<=3; n++){
   cin>> number[i].a;
   cin>> number[i].b;
   cin>> number[i].c;
  if(i >= 10) break;
  //}
 }

There is no point in having this statement if(i >= 10) break, because the look exists when the exit criteria is met, i.e. i<10.

The problem is here:

 for (int i = 0; i<9; i++){
  if (number[i].a > number[i+1].a){
   temp[0] = number[i].a;
   temp[1] = number[i].b;
   temp[2] = number[i].c;
   number[i].a = number[i+1].a;
   number[i].b = number[i+1].b;
   number[i].c = number[i+1].c;
   number[i+1].a = temp[0];
   number[i+1].b = temp[1];
   number[i+1].c = temp[2];
  }
 }

You only loop once through the array. The simplest sorting algorithm, bubble sort, requires a double loop:

 for (int i = 0; i<10; i++){
 for (int j = 0; j<9; j++){
  if (number[j].a > number[j+1].a){
   temp[0] = number[j].a;
   temp[1] = number[j].b;
   temp[2] = number[j].c;
   number[j].a = number[j+1].a;
   number[j].b = number[j+1].b;
   number[j].c = number[j+1].c;
   number[j+1].a = temp[0];
   number[j+1].b = temp[1];
   number[j+1].c = temp[2];
  }
 }

Notice that this is not necessary:

int *temp = new int[3];

You could use your data struct.

 data temp;

 for (int i = 0; i<10; i++){
 for (int j = 0; j<9; j++){
  if (number[j].a > number[j+1].a){
   temp.a = number[j].a;
   temp.b = number[j].b;
   temp.c = number[j].c;
   number[j].a = number[j+1].a;
   number[j].b = number[j+1].b;
   number[j].c = number[j+1].c;
   number[j+1].a = temp.a;
   number[j+1].b = temp.b;
   number[j+1].c = temp.c;
  }
 }
1
  • Thank you so much! I been up too late I believe. But once again thank you. Mar 10, 2014 at 8:36
0

The fastest way to force it work - to add for(int j=0; j<10; j++) just above sort for.

-1

You can using qsort http://www.cplusplus.com/reference/cstdlib/qsort/ for sorting array of structures, and when you not need writing your own sorting code.

1
  • In C++ there is no reason at all to use qsort over std::sort .
    – M.M
    Mar 10, 2014 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.