Home > Back-end >  Input a set of digital output after sorting
Input a set of digital output after sorting

Time:04-20

Dry as questions with this problem is to practice a pointer and a custom function
This code is what can be improved?
I think you can when inputting user input data to add a judgment, the input data is correct (such as input number is a decimal integer)
But I don't know how to implement
The code is as follows:
 # include 
# include

Using STD: : cin;
Using STD: : cout;
Using STD: : endl;

//declare custom function
Void initNumGroup (int * p_num);
Int setNumGroup (int * p_num);
Void the bubbleSort (int * p_num, int times);
Void soutNumGroup (int * p_num);
Void getWorkTime clock_t (t);

//the main function
Int main () {
Clock_t t=0;
Int numGroup [100].
Int * p_num=numGroup;
InitNumGroup (p_num);
Int times=setNumGroup (p_num);//get the number of digital
The bubbleSort (p_num, times);
SoutNumGroup (p_num);
GetWorkTime (t);
return 0;
}

//initialize array
Void initNumGroup (int * p_num) {
Cout & lt; <"Initialize array... "& lt; for (int i=0; I & lt; 100; I++)
* (p_num + I)=97627;
Cout & lt; <"Complete... "& lt; }

//the user assignment for array and returns the record number I
Int setNumGroup (int * p_num) {
int i=0;
Cout & lt; <"Input - 97627 to stop." & lt; Do {
Cout & lt; <"Please enter a numerical value:";
Cin & gt;> * (p_num + I);
} while (* (p_num + (i++))!=- 97627);
The return I - 1;
}

//bubble sort method
Void the bubbleSort (int * p_num, int times) {
Int I, j, temp=0;
For (j=0; J & lt; Times - 1; J++) {
For (I=0; I & lt; Times - j - 1; I++) {
If (* (p_num + I) & gt; + I + 1) * (p_num) {
Temp=* (p_num + I);
* (p_num + I)=* (p_num + I + 1);
* (p_num + I + 1)=temp.
}
}
}
}

//output sort the results
Void soutNumGroup (int * p_num) {
int j=0;
Cout & lt; While (* (p_num + j)! {
=97627)Cout & lt; <* (p_num + (j++)) & lt; <"\ t";
If (8 j %==0)
Cout & lt; }
}

//get program running time
Void getWorkTime (clock_t t) {
T=clock ();
Printf_s (" \ n \ n already running time: % 3 f second ", (double) t/CLOCKS_PER_SEC);
}
  • Related