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

In C++ Write a program, which will ask the user how many names (one word, no spa

ID: 3811950 • Letter: I

Question

In C++ 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) and modify using the old bubble sort file to do the sorting and no using qsort.

Explanation / Answer

#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;

}

OR

#include <stdio.h>

//Always use meaningful names for types

typedef unsigned char boolean;

#define True 't'

#define FALSE (!True)

//this is a really neat trick for swapping values efficiently

void swap(long* a,long *b) { *a=*a^*b;*b=*b^*a;*a=*a^*b; }

//Here's a readability improvement

#define until(condition) while(!(condition))

int main(int n, char*args[]){

double *d;

int i;

char input[5]; //should be long enough for most doubles.

boolean sorted = FALSE;

//In C, you need to specify the array size beforehand, so ask

printf("Please enter the length of the array ");

gets(input);

//scan the input string and convert to a value

sscanf(input,"%s",&input[0]);

n=(long)atol(input);

//allocate space, make sure you get the order of arguments right.

d = calloc(sizeof(double),n);

//Get and sort the array

until (sorted) {

   for (i=0;i<n;i++) {

        //It's important to always ask nicely

        printf("Please enter the %d%s array item ",i,i==1?"st":"th");

        scanf("%lf",d+i);

     }

     //do a compare and exchange sort:

     sorted = !sorted; //not sorted

     //check all the items

     printf("%d %d ",i,n);

     for (i=1;i<n;i++) {

        //compare

        if (d[i]<d[i-1]) {

          //exchange

          swap(d+i,d+i-1);

          sorted = FALSE;

        }

     }

     //show results

     printf("The array is%ssorted ",sorted?" ":" not "); }

//use the --> "downto operator" for counting downto 0.

for (;n-->0;) printf("%lf ",*d++);

}