Mary
Mary

Reputation: 211

Inputting data into and sorting a struct

I created a struct and created functions to perform several things on it. I am having trouble with the sortLength function, because when I run the program, only the data in the length gets sorted. Every thing else works fine although I am wondering if it is okay that my order is off a bit since I am using arrays which start their count at 0. I asked my professor for clarification and he told me if I have these (length and then widths) in the array and using comma to clearly separate them

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

Then it is sorted on length, then array will be:

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

However, I am not getting that for my output. Here is the output I am getting.

enter image description here

Here are the instructions for the sortLength function.

Note that function will sort using the value for length of each rectangle (in ascending order).

You pick any algorithm to use; bubble, selection or insertion.

Remember that you are sorting an 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 to each element of array   
    getValues(arrRect);

    cout << endl << endl;

    //print out each element of array
    print(arrRect);

    whereMax = findMaxArea(arrRect);
    //find where max area is 

    cout << endl;

    cout << "Max area is " << arrRect[whereMax].area << " at position " << whereMax;

    cout << endl;

    sortLength(arrRect);  //sort base on Length

    cout << endl;

    //print out each element of array
    print(arrRect);

    return 0;
}

/**
* Pre-Condition: This function accepts an array of type RECTANGLE. 
* Post-Condition: It prompts the user for a length and width value
* and calculates the area. 
*/
void getValues(RECTANGLE arrRect[])
{
    //put values to each element of array   
    for (int i = 0; i<SIZE; i++)
    {
        cout << "\nEnter length and width : ";
        cin >> arrRect[i].length >> arrRect[i].width;

        arrRect[i].area = arrRect[i].length * arrRect[i].width;
        //calculate area
    }
}

/**
* Pre-Condition: This function accepts an array of type RECTANGLE. 
* Post-Condition: It prints the data for length, width, and area. 
*/
void print(RECTANGLE arrRect[])
{
    //print out each element of array
    cout << "Length       Width     Area" << endl;
    for (int i = 0; i<SIZE; i++)
    {
        cout << arrRect[i].length << "\t\t" << arrRect[i].width << "\t"
            << arrRect[i].area << endl;
    }
}

/**
* Pre-Condition: This function accepts an array of type RECTANGLE. 
* Post-Condition: It returns an int which represents the position 
* of the highest area in the data. 
*/
int findMaxArea(RECTANGLE arrRect[])
{
    int maxIndex = 0; 
    int max = arrRect[0].area; 

    for (int i = 0; i<SIZE; i++)
    {
        if (max < arrRect[i].area)
        {
            max = arrRect[i].area; 
            maxIndex = i; 
        }
    }
    return maxIndex;
}

/**
* Pre-Condition: This function accepts an array of type RECTANGLE. 
* Post-Condition: It sorts the data in the array according to the 
* length value. 
*/
void sortLength(RECTANGLE arrRect[])
{
    int temp;

    for (int i = 0; i < (SIZE - 1); i++)
    {
        for (int j = i + 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;
            }
        }
    }
}

Upvotes: 0

Views: 78

Answers (1)

R Sahu
R Sahu

Reputation: 206697

You are getting the unexpected output since you are changing only the length field while sorting.

void sortLength(RECTANGLE arrRect[])
{
    int temp;

    for (int i = 0; i < (SIZE - 1); i++)
    {
        for (int j = i + 1; j < SIZE; j++)
        {
            if (arrRect[i].length > arrRect[j].length)
            {
                // ******  PROBLEM ******
                // The lines below swap only the length field.
                temp = arrRect[i].length;    
                arrRect[i].length = arrRect[j].length;
                arrRect[j].length = temp;
            }
        }
    }
}

What you should do is swap the entire object. Use

void sortLength(RECTANGLE arrRect[])
{
    // Make temp a RECTANGLE.
    RECTANGLE temp;

    for (int i = 0; i < (SIZE - 1); i++)
    {
        for (int j = i + 1; j < SIZE; j++)
        {
            if (arrRect[i].length > arrRect[j].length)
            {
                // Swap the entire object.
                temp = arrRect[i];    
                arrRect[i] = arrRect[j];
                arrRect[j] = temp;
            }
        }
    }
}

Upvotes: 2

Related Questions