2015-04-23 47 views
1

我创建了一个结构并创建了一些函数来执行一些结构。我遇到sortLength函数的麻烦,因为当我运行程序时,只有长度的数据才会被排序。其他所有东西都可以正常工作,但是我想知道我的订单是否因为我使用的数组数从0开始计数而没有问题。我问我的教授澄清问题,他告诉我,如果我有这些(长度,然后宽度)数组中,并使用逗号分隔清楚他们输入数据并对结构进行排序

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

然后,它在长度排序,然后数组将是:

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

但是,我没有得到这对我的输出。 这是我得到的输出。

enter image description here

下面是对sortLength功能的说明。

请注意,函数将使用每个矩形的长度(按升序)的值进行排序。

你选择任何算法使用;泡沫,选择或插入。

请记住,您正在整理一个数组!


#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; 
      } 
     } 
    } 
} 
+0

我想这是因为你只交换矩形长度,而不是整个矩形 – Rulisp

回答

2

你得到意想不到的输出,因为你只改变了length领域,而排序。

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; 
      } 
     } 
    } 
} 

你应该做的是交换整个对象。使用

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; 
      } 
     } 
    } 
} 
相关问题