Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ language only. /** CIS 22B: Homework 4B C-strings and Arrays of Pointers This program uses a selection sort to sort an array of c-strings.

C++ language only.

/**
 CIS 22B: Homework 4B
 C-strings and Arrays of Pointers
 
 This program uses a selection sort to sort an array of c-strings.
 It has some errors. Fix the errors and run the program.
 Save the output as a comment at the end of the program.
 
 NAME:
*/
 
/**
 A NOTE about pointers and constants: pointer constant versus pointer to constant
 
 
 const char *list[10] = {"Mary", "John", "Peter", "Anne",
 "Paul", "Andrew", "Linda", "Diane"};
 
 What is list?
 
 list is an array of 10 pointers to string literals (or constants):
 the strings in the array may not be changed but the pointers pointing to these strings could change.
 
 Therefore the following statement is incorrect
 strcpy(list[0], "Tom"); // This is an attempt to change a constant: replace "Mary" by "Tom"
 
 It is OK to change the pointers in the list:
 list[0] = list[1]; // now both pointers point to "John"
 
 Here are some examples
 
 EXAMPLE ONE: A pointer variable pointing to a constant
 const double PI = 3.14;
 const double EPSILON = 0.001;
 const double *ptr = Π
 cout << *ptr << endl;
 
 // *ptr += 1; //<==== this doesn't work because PI should not change
 ptr = &EPSILON; // but you are allowed to change ptr, to make it point to another constant
 cout << *ptr << endl;
 
 EXAMPLE TWO: A pointer constant pointing to a constant
 const double PI = 3.14;
 const double EPSILON = 0.001;
 const double * const ptr = &PI; // both ptr and its pointee (PI) are constants
 cout << *ptr << endl;
 
 // *ptr += 10; // doesn't work because its pointee is PI, a constant (it may not be changed)
 // ptr = &EPSILON; // doesn't work because because ptr is also a constant (it may not be changed)
 
 */
 
#include 
#include 
using namespace std;
 
 
void selectionSort( const char *list[], int size );
 
int main( void )
{
 const char *list[10] = {"Mary", "John", "Peter", "Anne",
 "Paul", "Andrew", "Linda", "Diane"};
 int size = 8; // 8 names in the list
 
 selectionSort( list, size );
 
 for (int i = 0; i < size; i++)
 {
 cout << list[i] << " ";
 }
 cout << endl;
 
 return 0;
}
 
/** ============================================================== */
/** This function sorts a list of strings using a selection sort
 algorithm.
 Given: ary - a list of strings
 size - its actual size
*/
void selectionSort( const char *list[], int size )
{
 const char *hold;
 int last;
 int small;
 int curr;
 int walk;
 
 last = size - 1;
 for(curr = 0; curr < last; curr++ )
 {
 small = curr;
 for( walk = curr + 1; walk < size; walk++ )
 if( list[walk] < list[small] )
 small = walk;
 
 hold = list[small];
 list[small] = list[curr];
 list[curr] = hold;
 }
 return;
}
/***************************************************************
 Save the OUTPUT below
 
 
 */
 
 
 
 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What is topology? Explain with examples

Answered: 1 week ago

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago