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

Step by stepSolved in 3 steps with 1 images

- What is the big O notation for this function?arrow_forwardWrite the function lastOf which searches the array a for the last occurance of any value contained in the array b. Returns the index if found. If not found, return -1. arrays.cpp 1 #include 2 int last0f(const int a[], int alen, const int b[], int bLen) { 3 4 5 6 7 8 }arrow_forwardStudent* func () { unique ptr arr[] make_unique ("CSC340") }; // #1 Insert Code int main () ( // #2 Insert Code [ #1 Insert Code]: Write code to keep all the object(s) which element(s) of array arr owns alive outside of the scope of func. [#2 Insert Code]: Write code to have a weak_ptr monitor the object which survived; Then test if it has any owner; Then properly destroy it; Then test again if has any owner; Then destroy the Control Block.arrow_forward
- Write the function lastOf which searches the array a for the last occurance of any value contained in the array b. Returns the index if found. If not found, return -1. arrays.cpp 1 #include 2 3 int lastof(const int a[), int alen, const int b[], int blen) 4 { int res = 0; for (int i = 0; i =0 ; j--) if (b[j] == a[i]) { if(res[1, 9, 4, 5, 1, 5, 1, 12, 8, 11, 2, 11, 7, 8] b1->[7, 5, 9] last0f(al, 14, b1, 3): -1 Expected: 12 al->[1, 9, 4, 5, 1, 5, 1, 12, 8, 11, 2, 11, 7, 8] b2->[7, 7] lastof (al, 14, b2, 2): -1 Expected: 12 a2->[3, 2, 1, 14, 10, 6, 13, 1ө, 11, 13, 14] b2->[7, 7] lastof (a2, 11, b2, 2): -1 Expected: -1 а2->[3, 2, 1, 14, 10, 6, 13, 1ө, 11, 13, 14] b1->[7, 5, 9] last0f (a2, 11, b1, 3): -1 Expected: -1 а2->[3, 2, 1, 14, 10, 6, 13, 10, 11, 13, 14] b3->[11, 2, 1, 15] lastof (a2, 11, b3, 4): -1 Expected: 8 Score 2/5arrow_forwardcomplete the //TODOs #include<iostream> #include "swap.h" using namespace std; int main(int argc, char const *argv[]) { intfoo[5] = {1, 2, 3, 4, 5}; cout<<"Addresses of elements:"<<endl; //TODO Print the addresses of array elements cout<<endl; cout<<"Elements:"<<endl;; //TODO Print all the elements using pointers cout<<endl; inta,b; intf; intflag = 1; while(flag == 1){ cout<<"Enter indices of elements you want to swap?"<<endl; cout<<"First index"<<endl; cin>>a; cout<<"Second index"<<endl; cin>>b; cout<<"Enter 0 for pass-by-value, 1 for pass-by-pointer"<<endl; cin>>f; switch (f) { case0: // Pass by Value // Compare the resultant array with the array you get after passing by pointer cout<<"Before swapping"<<endl; for(inti = 0;i<5;i++){ cout<<foo[i]<<" "; } cout<<endl; swap(foo[a],foo[b]); cout<<"\nAfter swapping"<<endl;…arrow_forwardWhat does each line of code mean?arrow_forward
- Python Please **Bold is code** Recap of two-dimensional arrays and their numpy implementation import numpy as np my_2d_array = np.array([[1,2],[3,4],[5,6]]) print("This is my_2d_array:") print(my_2d_array) print("This array has shape " + str(my_2d_array.shape) + " as there are " + str(my_2d_array.shape[0]) + " rows and " + str(my_2d_array.shape[1]) + " columns.") print("The entry of the array with row index " + str(1) + " and column index " + str(1) + " has value " + str(my_2d_array[1,1])) 1) In this problem, implement a TwoDArray class that is meant to mimic (some of) the functionality of a two-dimensional numpy array. DO NOT use numpy at any point. 1A) Give the class an __init__ method Make sure that the variable array is a valid input. This means you should a) Make sure that array is a list of lists. and b) Make sure that each of the inner lists has the same length. You do not need to check for anything else. Rise the appropriate error(s) if these conditions are not met. It is up…arrow_forwardWrite a statement that declares a dynamic array of 5 strings and lets a pointer variable p to point to that array A/arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





