Which of the following code fragment puts sorted values in an array using beads correctly?

Pham Quỳnh

New member
Which of the following code fragment puts sorted values in an array using beads correctly?
A.
Code:
for (int i = 0; i < n; i++) 
{ 
         int j;
        for (j = 0; j < max; j++); 
        //max is the maximum value element of given array a[]  
        a[i] = j; 
i}
B.
Code:
for (int i = 0; i < n; i++) 
{ 
         int j;
        for (j = 0; j < max &#038;&#038; beads[i * max + j]; j++); 
        //max is the maximum value element of given array a[]
        a[i] = j; 
}
C.
Code:
for (int i = 0; i < n; i++) 
{ 
         int j;
        for (j = 0; j < beads[i * max + j]; j++); 
       //max is the maximum value element of given array a[]  
        a[j] = i; 
}
D.
Code:
for (int i = 0; i < n; i++) 
{ 
         int j;
        for (j = 0; j < max &#038;&#038; beads[i * max + j]; j++); 
       //max is the maximum value element of given array a[]  
        a[j] = i; 
}
[/i][/i]
 
Answer: b
Explanation: After sorting the elements in the bead array we finally need to shift them to the original array. So we need to apply the condition j < max && beads[i * max + j] in order to achieve this.
 

Tìm kiếm

Back
Top