2016-12-05 7 views
0

このコードをクリーンアップするための一般的なヒント? 私はすべての小切手を使って作業を完了できると思われます。厳密に同じ配列入力

ありがとう!

(厳密に同じ配列)2つの配列list1 []とlist2 []は、同じ長さでlist1 []が各[i]のlist2 []と等しい場合、厳密に同じです。

LIST1とLIST2は、以下のヘッダを

bool strictlyEqual(const int list1[], const int list2[], int size) 

を使用して、厳密に同一である場合、2つは厳密に同一であるかどうかを整数とディスプレイの二つのリストを入力するようにユーザに促すテストプログラムを書く真を返す関数を書きます。サンプルを実行します。入力の最初の数字は、リスト内の要素の数を示します。この番号はリストの一部ではありません。

#include <iostream> 
    using namespace std; 

    bool strictlyEqual(int const list1[], int const list2[], int size); 

    bool strictlyEqual (int x1[], int x2[], int n) 
    { 
    int f=0; int i; 
    for (i =1; i<=n; i++) 
    { 
       if (x1[i] != x2[]) 
       { 
        // breaks loop 
        f=1; 
        break; 
       } 
    } 

if (f==0) 
return (true); 
else 
return(false); 
} 


int main() 
cout << "enter list1: " << endl; 
int list1[20], i; 
cin >> list1[0]; 
for (i=1; i<= list1[0]; i++) 
cin>> list1[i]; 

cout <<"enter the list2" << endl; 
int list2[20]; 
cin >> list2[0]; 
for (i=1; i<= list2[0]; i++) 
cin >> list2[i]; 

if (list1[0] == list2[0] 
{ 
    int size = list2[0]; 
    bool v=strictlyEqual(list1, list2, size); 
    if (v== true) 
    cout << "identical" << endl; 
    else 
    cout << "not identical " << endl; 
} 

    return 0; 
} 
+0

あなたは配列要素 '5 4 3 2 1 'の間の空間を追加する必要があります。 –

+0

@ HiI'mFrogattoありがとうございました!このコードをクリーンアップするための一般的なヒントちょっと混乱しているようです。 – walletfiber

+0

まだあなたの問題は何かを理解できませんか?エラー? –

答えて

1

シンプルなソリューションmemcmp機能を使用することになります。リストのサイズは私のコードは

最大であると仮定します。

int memcmp (const void * ptr1, const void * ptr2, size_t num); 

はゼロを返す、ptr2で指さ最初numバイトptr1が指すメモリのブロックの最初のnumバイトを比較する場合、それらはすべて一致または彼らがしなければ大きい表すゼロとは異なる値ない。

bool strictlyEqual (int x1[], int x2[], int n){ 
    return memcmp(x1, x2, n * sizeof(int)) == 0; 
} 
関連する問題