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

Hi I need help with a double sort. I have the program almost writter but somethi

ID: 3545228 • Letter: H

Question

Hi I need help with a double sort. I have the program almost writter but something is wrong with the sort.

here is the assignment.


Write a program, which will ask the user how many names (one word, no spaces) they wish to enter. The program will then read that many names, store them in a two dimensional array of characters (one row per name), print them in the order they were entered, then sort them in increasing alphabetical order, and print them in sorted order. The sort will be done without regard to the case of the letters (i.e. case insensitive sort). The printing of each name MUST be done with the characters as they were typed, you cannot change the case.
The maximum number of names to be handled will be 20.
The maximum number of printable characters in a name is 15.
Use a two dimensional array to store the names.
Create a function to read in all the names.
Create a function to do the sort (modify the bubble sort discussed in class and in the text book)


here is what i have so far.



.h file


#ifndef LAB_11_H
#define LAB_11_H

const int MaxNameLength (15);

int ReadNames (int, char [][MaxNameLength + 1]);
void Sort (int, char [][MaxNameLength + 1]);


#endif


first .cpp file with functions



#include <iostream>
#include <string.h>
using namespace std;

#include "lab11.h"

int ReadNames (int MaxNames, char Names [] [MaxNameLength + 1])
    {
    long    i;

    for (i = 0; i < MaxNames; i++)
        {
        cin.getline (Names [i], MaxNameLength + 1);
        if (Names [i] [0] == '')    
                break;
            else;
        }
    return i;


}

void Sort(int MaxNames, char Names [][MaxNameLength + 1])
{
    char temp;

    for (int i = 0; i < MaxNames; i++)
    
        for(int j = 0; j < MaxNames -i - 1; j++)
        
            if(Names[j] >Names[j+1])
            {
                temp = Names[j][0];
                Names[j][0] = Names[j+1][0];
                Names[j+1][0] = temp;
            }
    

}


main .cpp file


#include <iostream>
#include <string.h>
using namespace std;

#include "lab11.h"

void main ()
    {
    int    i;
    int    NumNamesRead;
    char    Names [20] [MaxNameLength + 1];

    cout << "Enter up to 20 names" << endl;
    NumNamesRead = ReadNames (20, Names);
    for (i = 0; i < NumNamesRead; i++)
        cout << "Names [" << i << "] = " << Names [i] << endl;

    Sort (NumNamesRead, Names);
        for (int j = 0; j < NumNamesRead; j++)
                cout << Names[j]<< " ";

        }


i think my problem is the sort. Please help..and please no vectors or pointers...im trying to do this without them.

Explanation / Answer

To really do this is C++, it would seem more appropriate to use C++ data structures
like a Vector of Strings (rather than a 2-d array of chars). Oh well. Here's a simple
example:


#include <stdio.h>
#include <strings.h>
#include <stdlib.h>

#define NAMELEN 16

char nameStrings[20][NAMELEN];
char *names[20];

void
readNames(int n)
{
  for (int i=0; i<n; i++) {
    names[i] = &nameStrings[i][0];
    fgets(names[i], NAMELEN, stdin);
  }
}

void
writeNames(const char* titleString, int n)
{
  printf("%s ", titleString);
  for (int i=0; i<n; i++)
    printf("%s", names[i]);
}

int
compareNames(const void*s1, const void*s2)
{
  char *p1 = *(char**)s1;
  char *p2 = *(char**)s2;
  return strncasecmp(p1, p2, NAMELEN);
}


int
main(int argc, char* argv[])
{
  char numBuffer[20];
  int numNames = 0;

  printf("How many names?");
  fgets(numBuffer, sizeof(numBuffer), stdin);
  numNames = atoi(numBuffer);
  if (numNames < 0 || numNames > 20)
    printf("Number of names must not exceed 20 "), exit(-1);

  readNames(numNames);
  writeNames("Original list of names", numNames);
  qsort(names, numNames, sizeof(char*), compareNames);
  writeNames(" Sorted list of names", numNames);
  return 0;
}


And to show that it performs the comparison without respect to case:

$ g++ -g names.c
$ a.out
How many names?3
sTan
paul
Stan
Original list of names
sTan
paul
Stan

Sorted list of names
paul
sTan
Stan