The following letters are stored in an alphabet array: B, J, K, M, S, and Z.
Write and test a function named adlet(), which accepts the alphabet array and a new letter
as arguments, and then inserts the new letter in the correct alphabetical order in the array.

void sortNewArray(char array[], char newLetter)
{
    for (int i = 0; i < newLetter - 1; i++)
    {
        if (array[i] > array[i + 1])
        {
            int temp = array[i + 1];
            array[i + 1] = array[i];
            array[i] = temp;
            i = -1;
        }
    }
}
void adlet(char array[], char newLetter)
{
    char newLetter;
    char newArray[7];
    newArray[7] = array[6] + newLetter;
    sortNewArray(newArray,newLetter);

}

int main()
{
 char newLetter;
    cout << " Enter a new letter" << endl;
    cin >> newLetter;
    char array[] = "BJKMSZ";
    adlet(array,newLetter);


    char a;
    cin >> a;
}

First off your code isn't following the instructions. Instead of inserting the new letter you're just sorting the array after adding the letter to the end. You will propbably need to keep track of the size of the array as well as either make the array big enough to handle the maximum size you want or make the size dynamic. For the insertion you'll have to loop through the array until you find the insertion point, then move everything down one place to insert the new letter.

Edited 2 Years Ago by tinstaafl

You are right, I just thought I can do it this way.
As long as it works, this class is only a prerequisite and I do not have much time to read the whole book, that's why I am missing concepts and syntax.

As an FYI prerequisite programing classes are very important. Everything you do in higher level programing classes builds off of them.

This article has been dead for over six months. Start a new discussion instead.