2d arraya strings webinars issue

1.what is the code for bubblesort in 2d arrays of strings?
2.what is the significance cin.ignore()?what happens if we do not use it?
3.what happens if we use key<a[i] instead of strcmp function?
4.what happens if we use a[i]=a[i+1] instead of strcpy function?

Hello @Rj.25,

  1. Here’s the code:
    https://ide.codingblocks.com/s/165530
    Though it was your assignment.

  2. If you won’t use cin.ignore(), getline() will not read the first string in the first iteration of the loop.

    The reason for this behaviour is,
    If getline is used after “cin >>” statement then getline() sees this newline character as leading whitespace, and it just stops reading any further.

    This leading whitespace is added to mark the end of line.

    When you do cin >> n;, that gets the value of n from the input stream, but it leaves whitespace on the stream. Specifically, it will leave a newline on the input stream, which then gets read by the next getline call as an empty line.
    hence, ignoring the actual string.

    solution:
    Call cin.ignore() before calling getline()

    syntax:
    istream& ignore (streamsize n = 1, int delim = EOF);
    where,
    n is the number of characters you wants to discard, and
    delim is the delimiter.

    It basically, extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.
    thye default value is 1 character.

    NOTE:
    If you’ll use this statement inside the loop. It will discard the first character of each input.
    Thus, write it outside the loop after cin >> statement.

    I would suggest you to try it yourself and observe the outputs it produces.

  3. Yes, you can do that.
    But, you have to keep in mind that a[i] is the reference to the first element in the ith row the 2D array.
    The correct way of comparison is specified in the code shared.

  4. No, cannot perform the assignment this way.
    Even if you would use the dereference operator, it will only assign the first element of i+1th row to the first index of the ith row.
    While strcpy() will copy the entire string in the i+1th row to ith row.

Hope, this would help.
Give a like if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.