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) {...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT =...
Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT = 5; const int BCOUNT = 7; const int CCOUNT = 6; int a[ACOUNT] = {1, 2, 3, 4, 5}; double b[BCOUNT] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}; char c[CCOUNT] = "HELLO"; cout <<"Array A contents: \n"; arrayContents(a, ACOUNT); cout <<"Array B contents: \n"; arrayContents(b, BCOUNT); cout <<"Array C contents: \n"; arrayContents(c, CCOUNT); return 0; } template void arrayContents(const T *arr, int countSize)...
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);    } }
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
#include <stdio.h> const int DECK_SIZE = 52; int deck[DECK_SIZE]; int main(void) { /* Populate the deck...
#include <stdio.h> const int DECK_SIZE = 52; int deck[DECK_SIZE]; int main(void) { /* Populate the deck */ for(int i = 0; i < DECK_SIZE; i++) { deck[i] = i + 1; }    /* Get the cut position as an integer input */    /* Verify that the input is valid */    /* Cut the deck */    /* Print the resulting deck with one element on each line */ return 0; } Write a program that "cuts" a...
#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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT