c++ - Struct Not Completing Sort -


basicly here assignment i'm not asking complete assignment, assist me.

sort array of structs

in assignment, create array of structs , sort array.

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

i stuck on step 5.

this program 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];   (int = 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;   //}  }   (int = 0; i<10; i++){  // (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";   (int = 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];   }  }  (int = 0; i<10; i++){  // (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 current output.

numbers entered in 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

in code

 (int = 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 no point in having statement if(i >= 10) break, because exists when exit criteria met, i.e. i<10.

the problem here:

 (int = 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 loop once through array. simplest sorting algorithm, bubble sort, requires double loop:

 (int = 0; i<10; i++){  (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 not necessary:

int *temp = new int[3]; 

you use data struct.

 data temp;   (int = 0; i<10; i++){  (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;   }  } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -