c++ - Inputting data into and sorting a struct -


i created struct , created functions perform several things on it. having trouble sortlength function, because when run program, data in length gets sorted. every thing else works fine although wondering if okay order off bit since using arrays start count @ 0. asked professor clarification , told me if have these (length , widths) in array , using comma separate them

10,8       4,3       6,5      5,1       2,1      3, 2   

then sorted on length, array be:

2 ,1    3,2     4, 3     5,1      6,5     10,8 

however, not getting output. here output getting.

enter image description here

here instructions sortlength function.

note function sort using value length of each rectangle (in ascending order).

you pick algorithm use; bubble, selection or insertion.

remember sorting array!


#include "stdafx.h" #include <iostream>  using namespace std;  struct rectangle {     int length;     int width;     int area; };  const int size = 4;  // function prototypes void getvalues(rectangle[]); void print(rectangle[]); int findmaxarea(rectangle[]); void sortlength(rectangle[]);  int main() {     rectangle arrrect[size];     //an array of type rectangle      int wheremax; //where find max area      //put values each element of array        getvalues(arrrect);      cout << endl << endl;      //print out each element of array     print(arrrect);      wheremax = findmaxarea(arrrect);     //find max area       cout << endl;      cout << "max area " << arrrect[wheremax].area << " @ position " << wheremax;      cout << endl;      sortlength(arrrect);  //sort base on length      cout << endl;      //print out each element of array     print(arrrect);      return 0; }  /** * pre-condition: function accepts array of type rectangle.  * post-condition: prompts user length , width value * , calculates area.  */ void getvalues(rectangle arrrect[]) {     //put values each element of array        (int = 0; i<size; i++)     {         cout << "\nenter length , width : ";         cin >> arrrect[i].length >> arrrect[i].width;          arrrect[i].area = arrrect[i].length * arrrect[i].width;         //calculate area     } }  /** * pre-condition: function accepts array of type rectangle.  * post-condition: prints data length, width, , area.  */ void print(rectangle arrrect[]) {     //print out each element of array     cout << "length       width     area" << endl;     (int = 0; i<size; i++)     {         cout << arrrect[i].length << "\t\t" << arrrect[i].width << "\t"             << arrrect[i].area << endl;     } }  /** * pre-condition: function accepts array of type rectangle.  * post-condition: returns int represents position  * of highest area in data.  */ int findmaxarea(rectangle arrrect[]) {     int maxindex = 0;      int max = arrrect[0].area;       (int = 0; i<size; i++)     {         if (max < arrrect[i].area)         {             max = arrrect[i].area;              maxindex = i;          }     }     return maxindex; }  /** * pre-condition: function accepts array of type rectangle.  * post-condition: sorts data in array according  * length value.  */ void sortlength(rectangle arrrect[]) {     int temp;      (int = 0; < (size - 1); i++)     {         (int j = + 1; j < size; j++)         {             if (arrrect[i].length > arrrect[j].length)             {                 temp = arrrect[i].length;                  arrrect[i].length = arrrect[j].length;                  arrrect[j].length = temp;             }         }     } } 

you getting unexpected output since changing length field while sorting.

void sortlength(rectangle arrrect[]) {     int temp;      (int = 0; < (size - 1); i++)     {         (int j = + 1; j < size; j++)         {             if (arrrect[i].length > arrrect[j].length)             {                 // ******  problem ******                 // lines below swap length field.                 temp = arrrect[i].length;                     arrrect[i].length = arrrect[j].length;                 arrrect[j].length = temp;             }         }     } } 

what should swap entire object. use

void sortlength(rectangle arrrect[]) {     // make temp rectangle.     rectangle temp;      (int = 0; < (size - 1); i++)     {         (int j = + 1; j < size; j++)         {             if (arrrect[i].length > arrrect[j].length)             {                 // swap entire object.                 temp = arrrect[i];                     arrrect[i] = arrrect[j];                 arrrect[j] = temp;             }         }     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -