I've tried to get as close as possible to the knowledge that I have, and I just can't seem to do good on this project. If someone can help me or guide me on the right path, I would appreciate it!
Let us develop a C++ program that does the following:
1. The program first asks user to enter the number of elements for an array. Let’s call this number N.
2. It then creates a dynamic array of size N + 1, containing N random lowercase alphabet letters between
‘a’ and ‘z’. Make sure the last element is a null character ‘\0’.
– Here, make sure to use dynamic memory allocation (usingnew command) to allocate memory space
for the array, which is exactly why we call it a dynamic array.
3. After creating the array, the program displays the entire (unsorted) array.
– Here, you must define and use a function showArray to display the array. This function must have
the following prototype:
void showArray(char *);
– Note that it does not pass the array size as a parameter (you don’t need it when you use pointer).
– You are NOT allowed to use array index or array bracket [ ] within this function to access elements.
Instead, use pointer to do so.
4. Then apply selection sort to sort the array in ascending order of alphabet letters, then display the entire
(sorted) array.
– You must define and use a function selectionSort to sort the array. This function must have the
following prototype:
void selectionSort(char *);
– Note that it does not pass the array size as a parameter (you don’t need it when you use pointer).
– Again, you are NOT allowed to use array index or array bracket [ ] within this function to access
elements. Instead, use pointer to do so.
5. Ask if the user wants to continue. If yes, return to Step 1. If no, terminate program. Before going back
to Step 1 or terminating the program, make sure to release the memory space occupied by the dynamic
array (usingdelete command).
Here is the code that I've been working on all evening, I know there are probably numerous errors, but that's why I'm here to get guidance.
They show what arguments the two functions must take. Your code does not match that.
The functions may not use array indexing, like your code does.
You must use a dynamic array. Your code does not.
The "game" should repeat from step 1. Yours repeats from 3.
What does your showArray() do? What it should do? When should you use it?
PS. The indentation in your posted code is not systematic.
You are NOT allowed to use array index or array bracket [ ] within this function to access elements. Instead, use pointer to do so.
OP: this restriction is contravened on lines 40, 43, 53, 56 … of your code. Something on the following lines perhaps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# include <iostream>
int main()
{
int* myArray = newint[5];
for (size_t i = 0; i < 5; ++i)
{
*(myArray + i) = i + 1;
}
for (auto i = 0; i < 5; ++i)
{
std::cout << *(myArray + i) << "\n";
}
delete [] myArray;
}
So, effectively myArray[i] becomes *(myArray + i) and so on …
Recommended read: Understanding and Using C Pointers by Richard Reese, Chapter 4 “Pointers and Arrays” (yes it's a C book but v good for this purpose)
#include <iostream>
#include <cstdlib>
#include <ctime>
// generate a random lower case character between ‘a’ and ‘z’.
char random_lower_case_char()
{
staticconst std::size_t nchars = 26 ;
staticconstchar alphabet[ nchars+1 ] = "abcdefghijklmnopqrstuvwxyz" ;
return alphabet[ std::rand()%nchars ] ; // return a random character in the alphabet
}
char* make_array()
{
// 1. The program first asks user to enter the number of elements for an array.
// Let’s call this number N.
std::size_t N ;
std::cout << "number of elements? " ;
std::cin >> N ;
// 2. It then creates a dynamic array of size N + 1
// Make sure the last element is a null character ‘\0’
// Here, make sure to use dynamic memory allocation (using new command)
char* dyn_array = newchar[N+1] ;
// containing N random lower case alphabet letters between ‘a’ and ‘z’.
for( std::size_t i = 0 ; i < N ; ++i ) dyn_array[i] = random_lower_case_char() ;
dyn_array[N] = 0 ; // Make sure the last element is a null character ‘\0’
return dyn_array ;
}
// Here, you must define and use a function showArray to display the array.
// This function must have the following prototype: void showArray(char *);
// You are NOT allowed to use array index or array bracket [ ] within this function
// Instead, use pointer to do so.
void showArray( constchar* cstr ) // ignore the career teacher, make it const-correct
{
// print characters one by one till the terminating null character
for( constchar* ptr_current = cstr ; *ptr_current != 0 ; ++ptr_current )
std::cout << *ptr_current ;
std::cout << '\n' ;
}
// return pointer to smallest element starting at cstr
char* ptr_smallest_element( char* cstr )
{
char* ptr_smallest = cstr ;
for( char* ptr_current = cstr+1 ; *ptr_current != 0 ; ++ptr_current )
if( *ptr_current < *ptr_smallest ) ptr_smallest = ptr_current ;
return ptr_smallest ;
}
// swap the characters pointed to by pa and pb
void ptr_swap( char* pa, char* pb )
{
char temp = *pa ;
*pa = *pb ;
*pb = temp ;
}
// Then apply selection sort to sort the array in ascending order of alphabet letters,
// You must define and use a function selectionSort to sort the array.
// This function must have the following prototype: void selectionSort(char *)
// Again, you are NOT allowed to use array index or array bracket [ ] within this function
// to access elements. Instead, use pointer to do so.
void selectionSort( char* cstr )
{
if( *cstr == 0 ) return ; // end of string, nothing more to be done
char* ptr_smallest = ptr_smallest_element(cstr) ; // pointer to smallest element
ptr_swap( cstr, ptr_smallest ) ; // bring the smallest element to the front
selectionSort( cstr+1 ) ; // sort the remaining characters (characters except the smallest)
}
int main()
{
std::srand( std::time(nullptr) ) ; // step 0. seed the legacy rng
{
char* dyn_array = make_array() ; // steps 1, 2
std::cout << "unsorted array: " ;
showArray(dyn_array) ; // step 3
selectionSort(dyn_array) ; // step 4
std::cout << " sorted array: " ;
showArray(dyn_array) ;
// make sure to release the memory space occupied by the dynamic array
delete[] dyn_array ;
}
// TO DO : step 5 (Ask if the user wants to continue. etc.)
}