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

in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven...
in C++, #include<iostream> using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i<NUM; i++) cout << arr[i] <<" "; cout <<endl; return 0; } void prepareArr(int a[]) { //randomly generate NUM integers in the range [0,99] and save them in...
#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.
Make pesodocode of this code. void loadData() { char line[MAX_LENGTH]; const char * delimiter = ",\n";...
Make pesodocode of this code. void loadData() { char line[MAX_LENGTH]; const char * delimiter = ",\n"; FILE * fp; fp = fopen("patients.txt", "r"); char c; if (fp == NULL) { printf("file not found"); return; } while (fp != NULL) { if (fgets(line, MAX_LENGTH - 1, fp) == NULL) break; if (line[1] == '\0') break; patients[patientCount].id = atoi(strtok(line, delimiter)); strcpy(patients[patientCount].name, strtok(NULL, delimiter)); patients[patientCount].age = atoi(strtok(NULL, delimiter)); patients[patientCount].annualClaim = strtok(NULL, delimiter); patients[patientCount].plan = atoi(strtok(NULL, delimiter)); strcpy(patients[patientCount].contactNum, strtok(NULL, delimiter)); strcpy(patients[patientCount].address, strtok(NULL, delimiter)); strcpy(line,...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...
#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size); int main(){ int arraySize, limit, count, srand(time(0)); print f("Enter the size of array\n"); scanf("%d", arraySize); int array[arraySize]; printf("Enter the upper limit\n"); scanf("%d", &limit); count = 0; while(count <= arraySize){ array[count] = (rand() % (limit + 1)); count++; } printArray(array, &arraySize); sort(array, arraySize); printArray(array, arraySize); Return 0; } void printArray(int a[], int size){ int i = 0; printf("Array: ["); while(i < size){ if(i != size...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT