2015-04-23 68 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; 
      } 
     } 
    } 
} 
相關問題