Question

In: Computer Science

Given the root C++ code: void sort() {    const int N = 10;    int...

Given the root C++ code:

void sort()
{
   const int N = 10;
   int x[N];
   for(int i = 0; i < N; i++)
   {
       x[i] = 1 + gRandom-> Rndm() * 10;
       cout<<x[i]<<" ";
}

   cout<<endl;
   int t;
  
   for(int i = 0; i < N; i++)
   {
   for(int j = i+1; j < N; j++)
   {
       if(x[j] < x[i])
       {
           int t = x[i];
           x[i] = x[j];
           x[j] = t;
   }
   }
       }
   cout << endl;
  
   for(int i = 0; i < N; i++)
   {
       cout << x[i] <<" ";
   }
   cout << endl;
}

Given the order of numbers (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

Why does the program sort into (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

and not into (9, 8, 7, 6, 5, 4, 3, 2, 1, 10)?

Please explain in detail.

Solutions

Expert Solution

first given input
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 10 9
after if comparison list [9, 10, 8, 7, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 9 8
after if [8, 10, 9, 7, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 8 7
after if [7, 10, 9, 8, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 7 6
after if [6, 10, 9, 8, 7, 5, 4, 3, 2, 1]
x[i],x[j]= 6 5
after if [5, 10, 9, 8, 7, 6, 4, 3, 2, 1]
x[i],x[j]= 5 4
after if [4, 10, 9, 8, 7, 6, 5, 3, 2, 1]
x[i],x[j]= 4 3
after if [3, 10, 9, 8, 7, 6, 5, 4, 2, 1]
x[i],x[j]= 3 2
after if [2, 10, 9, 8, 7, 6, 5, 4, 3, 1]
x[i],x[j]= 2 1
after if [1, 10, 9, 8, 7, 6, 5, 4, 3, 2]

after all the inner loop iteration
[1, 10, 9, 8, 7, 6, 5, 4, 3, 2]
x[i],x[j]= 10 9
after if [1, 9, 10, 8, 7, 6, 5, 4, 3, 2]
x[i],x[j]= 9 8
after if [1, 8, 10, 9, 7, 6, 5, 4, 3, 2]
x[i],x[j]= 8 7
after if [1, 7, 10, 9, 8, 6, 5, 4, 3, 2]
x[i],x[j]= 7 6
after if [1, 6, 10, 9, 8, 7, 5, 4, 3, 2]
x[i],x[j]= 6 5
after if [1, 5, 10, 9, 8, 7, 6, 4, 3, 2]
x[i],x[j]= 5 4
after if [1, 4, 10, 9, 8, 7, 6, 5, 3, 2]
x[i],x[j]= 4 3
after if [1, 3, 10, 9, 8, 7, 6, 5, 4, 2]
x[i],x[j]= 3 2
after if [1, 2, 10, 9, 8, 7, 6, 5, 4, 3]

after inner for loop
[1, 2, 10, 9, 8, 7, 6, 5, 4, 3]
x[i],x[j]= 10 9
after if [1, 2, 9, 10, 8, 7, 6, 5, 4, 3]
x[i],x[j]= 9 8
after if [1, 2, 8, 10, 9, 7, 6, 5, 4, 3]
x[i],x[j]= 8 7
after if [1, 2, 7, 10, 9, 8, 6, 5, 4, 3]
x[i],x[j]= 7 6
after if [1, 2, 6, 10, 9, 8, 7, 5, 4, 3]
x[i],x[j]= 6 5
after if [1, 2, 5, 10, 9, 8, 7, 6, 4, 3]
x[i],x[j]= 5 4
after if [1, 2, 4, 10, 9, 8, 7, 6, 5, 3]
x[i],x[j]= 4 3
after if [1, 2, 3, 10, 9, 8, 7, 6, 5, 4]

after inner for loop
[1, 2, 3, 10, 9, 8, 7, 6, 5, 4]
x[i],x[j]= 10 9
after if [1, 2, 3, 9, 10, 8, 7, 6, 5, 4]
x[i],x[j]= 9 8
after if [1, 2, 3, 8, 10, 9, 7, 6, 5, 4]
x[i],x[j]= 8 7
after if [1, 2, 3, 7, 10, 9, 8, 6, 5, 4]
x[i],x[j]= 7 6
after if [1, 2, 3, 6, 10, 9, 8, 7, 5, 4]
x[i],x[j]= 6 5
after if [1, 2, 3, 5, 10, 9, 8, 7, 6, 4]
x[i],x[j]= 5 4
after if [1, 2, 3, 4, 10, 9, 8, 7, 6, 5]

after inner for loop
[1, 2, 3, 4, 10, 9, 8, 7, 6, 5]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 9, 10, 8, 7, 6, 5]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 8, 10, 9, 7, 6, 5]
x[i],x[j]= 8 7
after if [1, 2, 3, 4, 7, 10, 9, 8, 6, 5]
x[i],x[j]= 7 6
after if [1, 2, 3, 4, 6, 10, 9, 8, 7, 5]
x[i],x[j]= 6 5
after if [1, 2, 3, 4, 5, 10, 9, 8, 7, 6]

after inner for loop
[1, 2, 3, 4, 5, 10, 9, 8, 7, 6]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 9, 10, 8, 7, 6]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 5, 8, 10, 9, 7, 6]
x[i],x[j]= 8 7
after if [1, 2, 3, 4, 5, 7, 10, 9, 8, 6]
x[i],x[j]= 7 6
after if [1, 2, 3, 4, 5, 6, 10, 9, 8, 7]

after inner loop
[1, 2, 3, 4, 5, 6, 10, 9, 8, 7]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 6, 9, 10, 8, 7]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 5, 6, 8, 10, 9, 7]
x[i],x[j]= 8 7
after if [1, 2, 3, 4, 5, 6, 7, 10, 9, 8]

after inner loop
[1, 2, 3, 4, 5, 6, 7, 10, 9, 8]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 6, 7, 9, 10, 8]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 5, 6, 7, 8, 10, 9]

after inner loop
[1, 2, 3, 4, 5, 6, 7, 8, 10, 9]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
after loop

final output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

so,it sorts like the above ......but not (9, 8, 7, 6, 5, 4, 3, 2, 1, 10)


Related Solutions

#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT