刚学C++,要编写一个ARRAY的简单程式,就是要看看ARRAY中有没有相同的数值,例如,
Enter integers (-1 to finish):
list[0]: 1
list[1]: 2
list[2]: 3
list[3]: 4
list[4]: 5
list[5]: 6
list[6]: 5
list[7]: 4
list[8]: 3
list[9]: 2
list[10]: 1
list[11]: -1
The array has 11 elements: 1 2 3 4 5 6 5 4 3 2 1
There is a matching number in the array.
There is no matching number in the 1st half.
Press any key to continue . . .
Enter integers (-1 to finish):
list[0]: 1
list[1]: 2
list[2]: 3
list[3]: 4
list[4]: 5
list[5]: 6
list[6]: 7
list[7]: 8
list[8]: 9
list[9]: 10
list[10]: 11
list[11]: -1
The array has 11 elements: 1 2 3 4 5 6 7 8 9 10 11
There is no matching number in the array.
Press any key to continue . . .
以下是我的code
- #include
- using namespace std;
- void display( const int list[], int n );
- void read( int list[], int &n );
- bool isMatch(const int a[], int length);
- int main()
- {
- const int MAXSIZE = 100;
- int a[MAXSIZE] = { 0 }, size;
-
- read(a, size);
-
- cout << "The array has " << size << " elements: " << flush;
- display(a, size);
-
- if (size > 0) {
- if (!(isMatch(a, size))){
- cout << "There is no matching numbers in the array." << endl;
- }
- else {
- cout << "There is a matching number in the array." << endl;
- if (size > 4){
- if (!(isMatch(a, size/2)))
- cout << "There is no matching number in the 1st half."
- << endl;
- else
- cout << "There is a matching number in the 1st half."
- << endl;
- }
- }
- }
-
- system("pause" );
- return 0;
- }//end main
- void display( const int list[], int n )
- {
- for( int i = 0; i < n; i++ )
- cout << list[i] << " ";
- cout << endl;
- }
- void read( int list[], int &n )
- {
- cout << "Enter integers (-1 to finish): " << endl;
- n = 0;
- do
- {
- cout << "list[" << n << "]: ";
- cin >> list[n];
- }
- while( list[n++] != -1 );
- --n; // don't count the last element
- }
- // Check if there is a match in the array up to length
- bool isMatch(const int a[], int length)
- {
- if (length > 1)
- {
- for( int i = 0; i < length - 1; i++ )
- for( int j = 0; j < length - 1; j++ )
- if( a [i] == a [j]) return true;
- }
- return false;
- }
复制代码但是出来的程式有问题,我想应该是最后的bool isMatch 出了问题,有没有人可以告诉我怎么写才对?? 谢谢了:c::c::c::c::c::c::c::c:
[
本帖最后由 SEELE 于 2007-6-1 06:40 PM 编辑 ]