What is the output of the following code? [code] void setup() { Serial.begin(9600); } void loop() { int n,c,d,position,swap;

Nguyễn Hào

New member
What is the output of the following code?
Code:
void setup() {
    Serial.begin(9600);
}
void loop() {
    int n,c,d,position,swap;
    int array[4]={1,2,4,3};
    n=4;
    for(c=0;c<n;c++) {
        position=c;
        for(d=c+1;d<n;d++) {
            if(array[position]>array[d]) {
                position=d;
	    }
	}
        if(position!=c) {
            swap=array[c];
            array[c]=array[position];
            array[position]=swap;
        }
    }
    for(c=0;c<n;c++) {
        Serial.print(array[c]);
        Serial.print(“ ”);
    }
}
A. 1 2 4 3
B. 1 3 2 4
C. 3 1 2 4
D. 1 2 3 4
 
Answer: D
Explanation: The above code takes 4 integers in an array and then attempts to sort the elements in ascending order using the Selection Sort Algorithm. This is done by repeatedly finding the smallest number from the array and putting it at the beginning.
 

Tìm kiếm

Back
Top