I am confused about one line code of my c++ homework, and please explain to me. My question is what does this line of code meaning?  "while ( !sorted )". I am really confused about this. I know !sorted refers to not sorted. So this line means while not sorted, sorted is true and for the following condition? Please explain this to me, I'm really really really confused! Moreover, why does professor write sorted = false at the last line? Thank you! This is the question and the answer. The function shown below will sort an integer array in ascending (increasing) order. That is, 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? Discussion: All you need to do is change the comparison in the if statement that checks if adjacent elements are out of order. Change it from ">" to "<". See change below. 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; } } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I am confused about one line code of my c++ homework, and please explain to me.

My question is what does this line of code meaning?  "while ( !sorted )". I am really confused about this. I know !sorted refers to not sorted. So this line means while not sorted, sorted is true and for the following condition? Please explain this to me, I'm really really really confused!

Moreover, why does professor write sorted = false at the last line? Thank you!

This is the question and the answer. The function shown below will sort an integer array in ascending (increasing) order. That is, 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?

Discussion: All you need to do is change the comparison in the if statement that checks if adjacent elements are out of order. Change it from ">" to "<". See change below.

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; } } }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY