Sorted and Unsorted Letters

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!

Here is the assignment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 (using new 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 (using delete 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void showArray(string array[], char *N, char letters[]);
void selectionSort(string array[], char *N, char letters[]);

int main() {

  int N;
      cout << "How many arrays are there?" << endl;
      cin >> N;
  int array[N];
  char again;

  char letters[N];

  unsigned seed = time(0);
  srand(seed);

  do {
    cout << "The unsorted letters are:\n";
    showArray(letters, N);
    cout << "\nThe sorted letters are:\n";
    selectionSort(letters, N);

    cout << "Run again? (Y,N)";
    cin >> again;
  }
  while(again == 'y' || again == 'Y');
    return 0;
  }

  void showArray(char *N, char letters[])
  {

    for(int i = 0; i < *N; ++i)
         letters[i] = rand() % ('z' - 'a' + 1) + 'a';
         /* cout << "\nHere are the unsorted letters:" << endl;
      for(int i = 0; i < N; ++i)
         cout << letters[i] << endl;*/
  }
  void selectionSort(string array[], char *N, char letters[])
  {
      int startScan, minIndex;
      string minValue;

      for (startScan = 0; startScan < (*N - 1); startScan++)
      {
        minIndex = startScan;
        minValue = array[startScan];
        for (int index = startScan + 1; index < *N; index++)
        {
        if (array[index] < minValue)
        {
          minValue = array[index];
          minIndex = index;
        }
      }
      array[minIndex] = array[startScan];
      array[startScan] = minValue;
  }
}
The instructions are rather clear.

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 = new int[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)
Still needed some help on this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <cstdlib>
#include <ctime>

// generate a random lower case character between ‘a’ and ‘z’.
char random_lower_case_char()
{
    static const std::size_t nchars = 26 ;
    static const char 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 = new char[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( const char* cstr ) // ignore the career teacher, make it const-correct
{
    // print characters one by one till the terminating null character
    for( const char* 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.)

}
JLBorges wrote:
1
2
3
4
5
6
7
8
void showArray( const char* cstr )
{
    // print characters one by one till the terminating null character
    for( const char* ptr_current = cstr ; *ptr_current != 0 ; ++ptr_current )
        std::cout << *ptr_current ;

    std::cout << '\n' ;
}

@JLBorges, sorry, is there any downside in writing simply
1
2
3
4
void showArray( const char* cstr )
{
    std::cout << cstr << '\n' ;
}

?
is there any downside in writing simply

Only a slight chance for the teacher to throw a hizzy fit.
Scaring enough...
Thanks, keskiverto.
@JLBorges Wow, you saved my life. Thank you so much!
Topic archived. No new replies allowed.