Please help me with my c++ homework. My question is about this part of the code given by the professor: temp = list[index]; list[index] = list[index+1]; list[index+1] = temp; Since this question is asking to set the array in descending order, what if the question asks to sort the array in ascending order. Should I wrote the code like "temp = list[index]; list[index-1] = list[index]; list[index] = temp;". Am I correct? This is my c++ homework question and answer. The below function will sort an integer array in ascending (increasing) order. After calling sort the smallest element will be at index 0, the next smallest at index 1, etc. What would you need to change to sort the array in descending (decreasing) order? void sort( int list[], int length ) { int index, temp; bool sorted = false; while ( !sorted ) { sorted = true for ( index = 0; index < length - 1; index++ ) if ( list[index] < list[index+1] ) { temp = list[index]; list[index] = list[index+1]; list[index+1] = temp; sorted = false; } } }
Please help me with my c++ homework.
My question is about this part of the code given by the professor: temp = list[index]; list[index] = list[index+1]; list[index+1] = temp;
Since this question is asking to set the array in descending order, what if the question asks to sort the array in ascending order. Should I wrote the code like "temp = list[index]; list[index-1] = list[index]; list[index] = temp;". Am I correct?
This is my c++ homework question and answer.
The below function will sort an integer array in ascending (increasing) order. After calling sort the smallest element will be at index 0, the next smallest at index 1, etc. What would you need to change to sort the array in descending (decreasing) order?
void sort( int list[], int length ) { int index, temp; bool sorted = false; while ( !sorted ) { sorted = true for ( index = 0; index < length - 1; index++ ) if ( list[index] < list[index+1] ) { temp = list[index]; list[index] = list[index+1]; list[index+1] = temp; sorted = false; } } }
Step by step
Solved in 2 steps