/ lab8ExB.cpp #include using namespace std;   void insertion_sort(int *int_array, int n); /* REQUIRES *    n > 0. *    Array elements int_array[0] ... int_array[n - 1] exist. * PROMISES *    Element values are rearranged in non-decreasing order. */   void insertion_sort(const char** str_array, int n);   /* REQUIRES *   n > 0. *   Array elements str_array[0] ... str_array[n - 1] exist. * PROMISES *   pointers in str_array are rearranged so that strings: *   str_array[0] points to a string with the smallest string (lexicographicall) , *   str_array[1] points to the second smallest string, ..., str_array[n-2] *   points to the second largest, and str_array[n-1] points to the largest string */   int main(void) {     const char* s[] = { "AB", "XY", "EZ"};     const char** z = s;     z += 1;            cout << "The value of **z is: " << **z << endl;     cout << "The value of *z is: " << *z << endl;     cout << "The value of **(z-1) is: " << **(z-1)<< endl;     cout << "The value of *(z-1) is: " << *(z-1)<< endl;     cout << "The value of z[1][1] is: " << z[1][1]<< endl;     cout << "The value of *(*(z+1)+1) is: " << *(*(z+1)+1)<< endl;       // point 1       int a[] = { 413, 282, 660, 171, 308, 537 };          int i;     int n_elements = sizeof(a) / sizeof(int);          cout << "Here is your array of integers before sorting: \n";     for(i = 0; i < n_elements; i++)         cout <<  a[i] << endl;     cout << endl;          insertion_sort(a, n_elements);          cout << "Here is your array of ints after sorting:  \n" ;     for(i = 0; i < n_elements; i++)         cout << a[i] << endl; #if 0     const char* strings[] = { "Red", "Blue", "pink","apple", "almond","white",                                                "nut", "Law", "cup"};          n_elements = sizeof(strings) / sizeof(char*);          cout << "\nHere is your array of strings before sorting: \n";     for(i = 0; i < n_elements; i++)         cout <<  strings[i] << endl;     cout << endl;       insertion_sort(strings, 9);            cout << "Here is your array of strings after sorting:  \n" ;     for(i = 0; i < n_elements; i++)         cout << strings[i] << endl;     cout << endl;      #endif          return 0; }   void insertion_sort(int *a, int n) {     int i;     int j;     int value_to_insert;          for (i = 1; i < n; i++) {         value_to_insert = a[i];                  /* Shift values greater than value_to_insert. */         j = i;         while ( j > 0 && a[j - 1] > value_to_insert  ) {             a[j] = a[j - 1];             j--;         }                  a[j] = value_to_insert;     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

// lab8ExB.cpp

#include <iostream>

using namespace std;

 

void insertion_sort(int *int_array, int n);

/* REQUIRES

*    n > 0.

*    Array elements int_array[0] ... int_array[n - 1] exist.

* PROMISES

*    Element values are rearranged in non-decreasing order.

*/

 

void insertion_sort(const char** str_array, int n);

 

/* REQUIRES

*   n > 0.

*   Array elements str_array[0] ... str_array[n - 1] exist.

* PROMISES

*   pointers in str_array are rearranged so that strings:

*   str_array[0] points to a string with the smallest string (lexicographicall) ,

*   str_array[1] points to the second smallest string, ..., str_array[n-2]

*   points to the second largest, and str_array[n-1] points to the largest string

*/

 

int main(void)

{

    const char* s[] = { "AB", "XY", "EZ"};

    const char** z = s;

    z += 1;

 

    

    cout << "The value of **z is: " << **z << endl;

    cout << "The value of *z is: " << *z << endl;

    cout << "The value of **(z-1) is: " << **(z-1)<< endl;

    cout << "The value of *(z-1) is: " << *(z-1)<< endl;

    cout << "The value of z[1][1] is: " << z[1][1]<< endl;

    cout << "The value of *(*(z+1)+1) is: " << *(*(z+1)+1)<< endl;

 

    // point 1

 

    int a[] = { 413, 282, 660, 171, 308, 537 };

    

    int i;

    int n_elements = sizeof(a) / sizeof(int);

    

    cout << "Here is your array of integers before sorting: \n";

    for(i = 0; i < n_elements; i++)

        cout <<  a[i] << endl;

    cout << endl;

    

    insertion_sort(a, n_elements);

    

    cout << "Here is your array of ints after sorting:  \n" ;

    for(i = 0; i < n_elements; i++)

        cout << a[i] << endl;

#if 0

    const char* strings[] = { "Red", "Blue", "pink","apple", "almond","white",

                                               "nut", "Law", "cup"};

    

    n_elements = sizeof(strings) / sizeof(char*);

    

    cout << "\nHere is your array of strings before sorting: \n";

    for(i = 0; i < n_elements; i++)

        cout <<  strings[i] << endl;

    cout << endl;

 

    insertion_sort(strings, 9);

 

    

    cout << "Here is your array of strings after sorting:  \n" ;

    for(i = 0; i < n_elements; i++)

        cout << strings[i] << endl;

    cout << endl;

    

#endif

    

    return 0;

}

 

void insertion_sort(int *a, int n)

{

    int i;

    int j;

    int value_to_insert;

    

    for (i = 1; i < n; i++) {

        value_to_insert = a[i];

        

        /* Shift values greater than value_to_insert. */

        j = i;

        while ( j > 0 && a[j - 1] > value_to_insert  ) {

            a[j] = a[j - 1];

            j--;

        }

        

        a[j] = value_to_insert;

    }

}

 

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education