Problem: Write a C++ program to collect a positive integer n from the user, and then collect n character strings from the user (containing only lower case alphabets and no other characters, not even spaces). The strings need not be of equal length but the maximum length of any string should be 11. The strings should be stored in an appropriate 2D array. Your program should then include the following:

A function string_compare which takes two strings as input and returns 1 if the first string appears before the second string in alphabetical order, -1 if the second string appears before the first in alphabetical order, and 0 if the two strings are equal to each other. Do not use the inbuilt strcmp function.
A function string_sort which sorts the character strings using the mergesort algorithm taught in class. You are recommended to write a recursive implementation. You will automatically lose 50% of the marks for this program if you implemented the sorting within main.
Your program should then print out the strings in sorted alphabetical order, starting from the first to the last. Feel free to refer to the lecture slides for help.

Input Format
integer n, and a set of n character strings


Output Format
Print out the strings in the suggested order, one string on a new line


Constraints
n is a positive integer, n <= 15
For ease of display of the output, the maximum length of any of the strings should be 11, excluding the NULL character
The character strings should contain only lower case alphabets


Sample input and output




Input

Output

4

zebra

lion

tiger

monkey



lion

monkey

tiger

zebra







Note:  

Do not write any C++ statements for printing general messages. For example, the following should NOT be present in your program: 
cout << "Enter a number:",  
cout << "The computed answer is", etc.  


cout should be used to print only the computed final output. In addition, do not print unnecessary spaces unless specified in the program.

Student submission:
/*Write a C++ program to collect a positive integer n from the user,
 and then collect n character strings from the user (containing only lower case alphabets and no other characters, not even spaces). 
The strings need not be of equal length but the maximum length of any string should be 11.
The strings should be stored in an appropriate 2D array.
Your program should then include the following:

A function string_compare which takes two strings as input 
and returns 1 if the first string appears before the second string
 in alphabetical order,
 -1 if the second string appears before the first in alphabetical order,
 and 0 if the two strings are equal to each other.
 Do not use the inbuilt strcmp function.

A function string_sort which sorts the character strings using the
 mergesort algorithm taught in class.
 You are recommended to write a recursive implementation.
 You will automatically lose 50% of the marks for this program if
 you implemented the sorting within main. 

Your program should then print out the strings in sorted alphabetical order,
 starting from the first to the last. Feel free to refer to the lecture slides for help. 

 */

#include <iostream>
using namespace std;

int string_compare(char*A, char*B){
	int n1, n2;
	n1=n2=0;

	while(A[n1]!=0){
		n1++;
	}
	while(B[n2]!=0){
		n2++;
	}

	if(n1<n2){
		for(int i=0; i<n1; i++){
			if(A[i]==B[i]) continue;
			else if(A[i]<B[i]) return -1;
			else if(A[i]>B[i]) return 1;
		}

	}
	else{
		for(int i=0; i<n2; i++){
			if(A[i]==B[i]) continue;
			else if(A[i]<B[i]) return -1;
			else if(A[i]>B[i]) return 1;
		}

	}
	return 0;
}

void merge(char A[][100], char b1[][100], char b2[][100], int n1, int n2){
	int cB1, cB2, cA; cB1 = cB2 = cA = 0; // counters for B1,B2,A respectively
	while (cB1 < n1 && cB2 < n2){
	// copy an element from B1 to A as B1 has the smaller element
		if(string_compare(b1[cB1],b2[cB2])<0){
			for(int i=0; i<100; i++){
				 A[cA][i] = b1[cB1][i];
			}
		cA++; cB1++;
		}
	// copy an element from B2 to A as B2 has the smaller element
		else if(string_compare(b1[cB1],b2[cB2])>0){
			for(int i=0; i<100; i++){
				 A[cA][i] = b2[cB2][i];
			}
		cA++; cB2++;
		}
	// both have equal-valued elements, copy from both to A
		else {
			for(int i=0; i<100; i++){
				 A[cA][i] = b1[cB1][i];
			}
		cA++; cB1++;
			for(int i=0; i<100; i++){
				 A[cA][i] = b2[cB2][i];
			}
		cA++; cB2++;
		}
	}
// at most one of the following two while loops will be entered (why?) 
// copy the remaining elements from  either B1 or B2 to A
	while(cB1 < n1){
			for(int i=0; i<100; i++){
				 A[cA][i] = b1[cB1][i];
			}
		cA++; cB1++;
		}
	while(cB2 < n2){
			for(int i=0; i<100; i++){
				 A[cA][i] = b2[cB2][i];
			}
		cA++; cB2++;
		}
}


void string_sort(char A[][100], int n){

	if (n > 1){
		char b1[n/2][100], b2[n-n/2][100];
		for (int i=0;i<n/2;i++) {
			for(int j=0; j<100; j++){
				 b1[i][j] = A[i][j];
				 }
		}
		for (int i=n/2;i<n;i++) {
			for(int j=0; j<100; j++){
				 b2[i-n/2][j] = A[i][j];
			}
		}
		string_sort(b1,n/2); string_sort (b2,n-n/2);
			merge(A,b1,b2,n/2,n-n/2);

	}
}

int main(){
	int n;
	cin>>n;
	char A[n][100];
	for(int i=0; i<n; i++){
		cin>>A[i];
	}

	string_sort(A, n);

	for(int i=0; i<n; i++){
		cout<<A[i]<<endl;
	}
}

Grading criterion: Quality of variable names
Rating A: Good variable names
Rating B: Ok Variable Names
Rating C: Poor Variable Names
Rating D: 0
Assigned grade: B