Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a list of student names and sorts the list. Your prog

ID: 3684892 • Letter: W

Question

Write a program that reads a list of student names and sorts the list. Your program should have a function called “readNames” that has two arguments: 1) a 2-D character array, 2) an integer indicating the number of student names to be read from the keyboard. Another function called “sortNames” should have the same arguments and will reorder the list using a sorting algorithm (similar to the one we implemented for integer arrays) and puts names in ascending order. Assume that names are not longer than 10 characters including the nul character (‘’). Also, maximum number of names that will be entered by the user is 10.

Explanation / Answer

Answer

#include <iostream>
#include <cstring>
using namespace std;
void sort_names(int count,char name[][10])
{
char temp[10];
for(int i=0 ; i<count-1 ; ++i)
{
for(int j=i+1 ; j<count ; ++j)
{
if(strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]);
           strcpy(name[i], name[j]);
           strcpy(name[j], temp);
}
}
}
for(int k=1;k<=count;k++)
{
cout<<" name "<<k<<" :"<<name[k];
}
}
void read_names(int count1,char name1[][10])
{
int count,i;
cout<<" Enter count: ";
cin>>count;
char name[count][10];
for(i=1;i<=count;i++)
{
cout<<" Enter name "<<i<<" :";
cin>>name[i];
}
sort_names(count,name);
}
int main()
{
char name1[1][10]={"n"};
read_names(0,name1);
return 0;
}