Question:
sorting strings Lexicographically?
Asaf Tolkovsky
2012-01-16 04:07:35 UTC
In this question I have to use bubble sorting, and the strings will be saved as an array of strings (2 dimensional array of characters). The sorting will be done on an array of pointers which will point to those strings - so in fact I am required to sort the pointers array and not the original strings array. Also - I am not allowed to use the operator [] in order to get direct access to a specific value in the pointers array, so basically I can't use pointers[i] etc. This limitation is for the pointers array only, not for the strings array.

(a) write a function that gets a list of strings into a 2-dimensional array of characters so in every line you'll have 1 string. The maximal length of a string is 100 characters and it can contain the space character (spaces between the words). Every string you get from the user ends with \n.
You can assume that the user will not use more than 100 characters for every string and that all the string's characters are in the form of a-z and not A-Z.
The function has to be defined as follows: void readStrings (char strings[][COL_NUM], int num);
num is the number of lines in the matrix (the number of strings) and COL_NUM is the number of columns.
(b) Write a function that gets as input an array of strings (which is represented as a 2-dimensional array of chars like in (a)), an array of pointers and the number of strings. The function has to fill the pointers array so it points to the string in the strings list: The first place points to the first string, the second place to the second string etc. You can assume that the pointers array is in suitable size. The function has to be defined as follows: void pointStrings (char strings[][COL_SIZE], char* pointers[], int num);
(c) Write a function that gets as input an array of pointers and 2 locations in the array and switches between these 2 locations. The function has to be defined as follows: void swap (char* pointers[], int i, int j);
(d) Write a function that gets as input an array of pointers, its size, and a certain location in the array. Every pointer points to a string. The function returns the location of the pointer that points to the first string in lexicographic order from that location to the end of the array.
*Lexicography is the way a dictionary is sorted. In order to check who comes first use strcmp from string.h. This function gets as input 2 strings and returns 0 if they are identical, a negative number if the first priors the second Lexicographically and a positive number if the second priors the first Lexicographically.
The function has to be defined as follows: int find_min_string (char *pointers[], int size, int start);
size is the size of the array and start is the location from which you have to find the first string lexicographically.
(e) Write a function that gets as input an array of pointers to strings and its size and sorts it with bubble sorting. The sorting is done lexicographically. The function has to be defined as follows: void bubble_sort_strings (char* pointers[], int size);
(f) Write a program which gets as input from the user 5 strings (using the function in (a)). The program allocates an array of pointers to those strings and then prints, using the array, the strings in the order they were given by the user. Then - the program sorts the pointers according to the Lexicographic order of the strings and prints the sorted strings.

Example:
Please insert 5 strings:

this is the first string
and now the second
how about the third?
two more to go
last string...

not sorted:
this is the first string
and now the second
how about the third?
two more to go
last string...

sorted:
and now the second
how about the third?
last string...
this is the first string
two more to go...
Three answers:
cja
2012-01-16 05:50:01 UTC
Kudos to your instructor for providing such a detailed assignment. It's your job, not mine, to write the program to those specifications. I'll do it how I like, although bubble sort would be my last choice, but this will show you the basics of what you need to do for your assignment.



#include

#include

#include



#define MAX_LINE_LEN 102

#define N 5



size_t readStrings(char **, size_t);

void bubbleSortStrings(char **a, size_t);

void printArray(char **a, size_t);



int main(int argc, char *argv[]) {

    char **lines = calloc(N, sizeof(char *));

    size_t n, i;



    n = readStrings(lines, N);

    bubbleSortStrings(lines, n);

    printArray(lines, n);



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

        free(lines[i]);

    }

    free(lines);

    return 0;

}



size_t readStrings(char **a, size_t maxLen) {

    char in[MAX_LINE_LEN];

    size_t i;



    printf("Enter up to %d lines (blank to quit):\n", maxLen);

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

        printf("> ");

        fgets(in, MAX_LINE_LEN, stdin);

        *(strchr(in, '\n')) = '\0';

        if (strlen(in) > 0) {

            a[i] = calloc(strlen(in) + 1, sizeof(char));

            strcpy(a[i], in);

        } else {

            break;

        }

    }

    return i;

}



void bubbleSortStrings(char **a, size_t len) {

    int i,j;

    char *t;



    if (a != NULL) {

        for (i = len-1; i >= 0; i--) {

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

                if (strcmp(a[j-1], a[j]) > 0) {

                    t = a[j-1];

                    a[j-1] = a[j];

                    a[j] = t;

                }

            }

        }

    }

}



void printArray(char **a, size_t len) {

    size_t i;



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

        printf("%s\n", a[i]);

    }

}



#if 0



Enter up to 5 lines (blank to quit):

> one line of text

> another line

> last one

>

another line

last one

one line of text



#endif
waltman
2016-12-03 13:58:23 UTC
via form them lexicographically, i'm assuming which you fairly choose to print them out in looked after order. #contain iostream #contain string employing namespace std; void findMaxMinAndMid(string& s1, string& s2, string& s3, string& min, string& max, string& mid); significant () { string str1, str2, str3; cout<<"enter string #a million: "; cin>>str1; cout<<"enter string #2: "; cin>>str2; cout<<"enter string #3: "; cin>>str3; string min, max, mid; findMaxMinAndMid(str1, str2, str3, min, max, mid); cout<<"the 1st is ""< max) { max = str2; } if(str3>max) { max=str3; } if(str2 < min) { min = str2; } if(str3
Vinod
2012-01-16 04:40:58 UTC
its too lengthy to read. and i'm lazy. if u cud just ask wat exactly need short n sweet?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...