2013-10-11 85 views
2

我學習C++,我不得不創建數組[N] [M],以整數來填充它,然後
"Characteristic of matrix rows is called the sum of its positive even elements. You need to sort the rows of the matrix in accordance with the growth of characteristics."數組排序(C++)

任務這是我的代碼

#include "stdafx.h" 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
srand((unsigned)time(NULL)); 

int n, m; 
cout << "n = "; 
cin >> n; 
cout << "m = "; 
cin >> m; 

int ** mas = new int * [n]; 
for (int i = 0; i < n; ++i) 
{ 
    mas[i] = new int[m]; 
} 

cout << "Array:\n"; 
for (int i = 0; i < n; ++i) 
{ 
    for (int j = 0; j < m; ++j) 
    { 
     mas[i][j] = rand()%41-20; 
     cout << mas[i][j] << "\t"; 
    } 
    cout << "\n"; 
} 

double * characteristic = new double[n]; 
for (int i = 0; i < n; ++i) 
{ 
    characteristic[i] = 0; 
} 

for (int i = 0; i < n; ++i) 
{ 
    for (int j = 0; j < m; ++j) 
    { 
     if((j%2 == 0) && (mas[i][j] >= 0)) 
     { 
       characteristic[i] += mas[i][j]; 
     } 
    } 
} 

cout << "Characteristics:\n"; 
for (int i = 0; i < n; ++i) 
{ 
    cout << characteristic[i] << " "; 
} 
cout << "\n"; 

for (int i = 0; i < n - 1; ++i) 
{ 
    int min = i; 
    for (int j = i + 1; j < n; ++j) 
    { 
     if (characteristic[min] <= characteristic[j]) continue; 
     min = j; 
    } 
    if (min != i) 
    { 
     double temp = characteristic[i]; 
     characteristic[i] = characteristic[min]; 
     characteristic[min] = temp; 

     for (int k = 0; k < m; ++k) 
     { 
      int temp1 = mas[i][k]; 
      mas[i][k] = mas[min][k]; 
      mas[min][k] = temp1; 
     } 

    } 
} 

cout << "\nSorted characteristics:\n"; 
for (int i = 0; i < n; ++i) 
{ 
    cout << characteristic[i] << " "; 
} 
cout << "\n"; 

cout << "Sorted array:\n"; 
for (int i = 0; i < n; ++i) 
{ 
    for (int j = 0; j < m; ++j) 
    { 
     cout << mas[i][j] << "\t"; 
    } 
    cout << "\n"; 
} 

for (int i = 0; i < n; ++i) 
{ 
    delete [] mas[i]; 
} 
delete [] mas; 

delete [] characteristic; 

system("PAUSE"); 
return 0; 
} 

我爲特性創建了另一個數組,並將它與第一個數組同時排序,但似乎我使用了太困難的方法來完成給定的任務。也許有其他方法嗎?

+2

嘗試std :: vector向量,然後std :: sort使用自定義謂詞,然後它看起來像C++ –

+0

爲什麼你使用'double'作爲特性,如果矩陣只包含'int' S' – SingerOfTheFall

+0

關於你的代碼最糟糕的是你使用'n^2'排序算法。這應該對如何使用'std :: sort'有用:http://stackoverflow.com/questions/4523220/sorting-a-vector-of-double-precision-reals-and-obtain-their-order – Adam

回答

1

@sehe給你很好的建議,但是我懷疑很多這些東西在你知道更多的C++之前是沒有意義的。

這裏有一個簡單的改進,以消除緩慢循環:

當你做掉排掉,而不是複製它們指向的每一個值的行指針。替換此:

for (int k = 0; k < m; ++k) 
    { 
     int temp1 = mas[i][k]; 
     mas[i][k] = mas[min][k]; 
     mas[min][k] = temp1; 
    } 

有了:

int* temp1 = mas[i]; 
    mas[i] = mas[min]; 
    mas[min] = temp1; 

如果你能弄清楚如何使用內置的排序算法,這將是在此之上的另一種改進,但即使這樣小的變化將獲得你很多。

+0

非常感謝您的幫助,它有效。 – Heidel

+2

@海德爾沒問題。在你的期末考試回來看看sehe的代碼後。它會讓你成爲更好的C++程序員。它還突出了C和C++之間的一些差異,以及它們爲什麼是不同的語言,只是彼此相似。 – Adam

0

由於n,m的大小在編譯時已知,因此可以使用C庫中的qsort函數。

#include <stdlib.h> 

void qsort(void *base, size_t nmemb, size_t size, 
      int (*compar)(const void *, const void *)); 

哪裏是compar是你寫一個函數,應該把它的兩個參數爲指針,以矩陣的行。然後它可以計算兩行的特徵,並根據哪一行的特徵較大返回-1,0或1。

+1

老實說? qsort是C並且不安全。我建議std :: sort。 – cdoubleplusgood

+0

@cdoubleplusgood看到我的回答(雖然這可能是家庭作業,並且我的答案將沒有多大用處,除非老師在課堂上使用後C++的開放思想:) :) – sehe

+0

@SzG'n'和' m'在編譯時不知道。顯然你仍然可以編寫一個比較函數,但我認爲你不得不求助於全局變量。 'std :: sort'沒有這個問題,你可以使用一個比較對象。 – Adam

3

您是否想要對矩陣進行排序,使用與'特性'相同的順序?

比方說,你有C++風格的代碼來計算的特點:

std::vector<double> characteristic(n, 0.0); 
std::transform(begin(mas), end(mas), begin(characteristic), sum_); 

然後,您可以對它們進行排序:

std::sort(begin(characteristic), end(characteristic)); 

或者你可以,的確排序矩陣立刻道:

std::sort(begin(mas), end(mas), [&sum_](int_vec const& a, int_vec const& b) 
     { return sum_(a)<sum_(b); }); 

編輯修復所有版本以使用正確的「特徵的總和」(保留名稱雖然),感謝@Adam

這裏是一個完整的程序,它說明了這一點:看它Live on Coliru

#include <random> 
#include <iostream> 
#include <string> 
#include <vector> 

#include <cstdlib> 
#include <algorithm> 
#include <iterator> 

using namespace std; 

int main() 
{ 
    typedef std::vector<int> int_vec; 

    srand((unsigned)time(NULL)); 
    int n, m; 
    cout << "n = "; 
    cin >> n; 
    cout << "m = "; 
    cin >> m; 

    std::vector<int_vec> mas(n, int_vec(m)); 

    for (auto& v : mas) 
     std::for_each(begin(v), end(v), [](int& i) { i = rand()%41-20; }); 

    cout << "Array:\n"; 
    for (auto const& v : mas) 
    { 
     std::copy(begin(v), end(v), ostream_iterator<int>(cout, "\t")); 
     cout << "\n"; 
    } 

    auto sum_ = [m](int_vec const& v) { 
     double vchar = 0; 
     for (auto j = 0; j < m; j+=2) 
      if(v[j] >= 0) vchar += v[j]; 
     return vchar; 
    }; 

    std::vector<double> characteristic(n, 0.0); 
    std::transform(begin(mas), end(mas), begin(characteristic), sum_); 

    cout << "Characteristics:\n"; 
    std::copy(begin(characteristic), end(characteristic), ostream_iterator<double>(cout, " ")); 
    cout << "\n"; 

    std::sort(begin(characteristic), end(characteristic)); 

    cout << "\nSorted characteristics:\n"; 
    std::copy(begin(characteristic), end(characteristic), ostream_iterator<double>(cout, " ")); 
    cout << "\n"; 

    std::sort(begin(mas), end(mas), [&sum_](int_vec const& a, int_vec const& b) { return sum_(a)<sum_(b); }); 

    cout << "Sorted Array:\n"; 
    for (auto const& v : mas) 
    { 
     std::copy(begin(v), end(v), ostream_iterator<int>(cout, "\t")); 
     cout << "\n"; 
    } 

} 

輸出示例:

n = m = Array: 
11 15 19 18 
-20 -16 2 -11 
8 2 19 8 
Characteristics: 
30 2 27 

Sorted characteristics: 
2 27 30 
Sorted Array: 
-20 -16 2 -11 
8 2 19 8 
11 15 19 18 
+1

現在,使用[C++ 11's''header](http://en.cppreference.com/w/cpp/numeric/random)作爲練習留給讀者。請閱讀[如何從rand()縮小數字?](http://stackoverflow.com/a/4196775/85371)和['rand()'Considered Harmful](http://channel9.msdn.com)儘管如此,斯蒂芬拉瓦維傑仍然在/ Events/GoingNative/2013/rand-Considered-Harmful)。 – sehe

+0

非常感謝您的解釋,但我還沒有研究過「矢量」。 – Heidel

+0

@我是否錯過了某些東西,或者你的「mas」排序重新計算了每個比較的特徵?另外,似乎'sum_'使特徵成爲* all *元素的總和,而不僅僅是正數even *。 – Adam