/* ************************************************************************** * Program name : 034_Mark_Six (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/13 - first version * ************************************************************************** * Description : (a) Input the size of the checkerboard pattern * * (b) Output the checkerboard pattern * * (c) Try again ? * ************************************************************************** */ #include #include #include #include const int Arraysize = 6; // Note (1) int Marksix[Arraysize]; // Note (2) void getnumbers(int Marksix[]) // Note (3) { int Index_1, Index_2; for (Index_1=0; Index_10) // Note (5) for (Index_2=0; Index_2Marksix[Index_2]) // Note (6) { tempint = Marksix[Index_1]; Marksix[Index_1] = Marksix[Index_2]; Marksix[Index_2] = tempint; } } int main() { // part 1 : declaration int Number, Index_1, Index_2; char Again; do { srand(time(NULL)); // part 2 : Repeat asking the number of lists do { cout << "\nHow many lists of number do you want ? "; cin >> Number; } while (Number <= 0); // part 3 : for (Index_1=1; Index_1<=Number; Index_1++) { getnumbers(Marksix); // Note (3) sortnumber(Marksix); cout << "\n"; for (Index_2=0; Index_2 <= 5; Index_2++) cout << setw(2) << Marksix[Index_2] << " "; } // part 4 : try another number ? cout << "\n\n\aTry again (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) The statement "const int Arraysize = 6;" define a global variable of data type int and the value of this variable can not be changed. (2) (a) The statement "int Marksix[Arraysize];" define an array of data type int and this array contains Arraysize (in this case is 6) elements. (b) The first element in this array is Marksix[0] , the second one is Marksix[1], then Marksix[2]... and the last one is Marksix[5]. (c) Compare the syntex in C++ and PASCAL C++ | PASCAL ----------------------------|------------------------------------- int Marksix[6]; | Marksix : Array [0..5] of integer ----------------------------|------------------------------------- char Name[10]; | Name : string[10] ----------------------------|------------------------------------- int Number[]={2,6,4}; | Number : Array [0..2] of integer; | Number[0] = 2; | Number[1] = 6; | Number[2] = 4; ----------------------------|------------------------------------- (3) (a) The function header for any function that receive an array as its parameter might written as : syntex : return-type function_name(data-type arrayname[]) example: void sortnumber(int Marksix[]) (b) The function prototype might written as : void function_name(int []); or void sortnumber(int Marksix[]) (c) The function call statement might written as : function_name(arrayname) e.g. sortnumber(Marksix); (4) The statement "Marksix[Index_1] = rand() % 47 + 1;" assign a random number (from 1 to 47) to an element in the array Marksix. (5) Skip all duplicate number (number that had already drawn) (6) Sort the list of elements in the array by "bubble sort". */