dualSort arrays - integer and string

Hi ~

Before posting my code, can someone tell me if it is possible to do a dual sort on an array of chars and an array of int, double or float? Would you have to use two separate sorts? Really confused here.

I am trying to sort an array of 12 doubles (user input) so that the console output sorts it in descending order. The corresponding months should be printed as well.

Thanks for any help! Super lost on this one.

I did get the dualSort function working properly and it sorts both arrays though both are integer arrays and not string arrays. I would really love to know how to associate the two or change the int months[] to a string array that could be compared. Is there a conversion method using static_cast or something?

One suggestion from another forum board was for mapping. unfortunately we have not reached that point of study yet nor have we covered pointers.

Thanks Again.
Last edited on
Since you are able to do a dualSort for integers, if I understand correctly, the main problem is to find a way to compare two strings and a way to assign a string to another, right?

Are you allowed to use std::string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace std;

const string bool2str[2]={"false","true"};

int main()
{
    string str1("asdf");
    string str2("qwerty");

    string str;

    cout << bool2str[str1<str2] << endl;
    cout << bool2str[str1>str2] << endl;
    cout << bool2str[str1==str2] << endl;

    if (str1<str2) str=str1;
    else str=str2;

    cout << str << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}

If not, you can use char arrays as strings (then, an array of strings would be an array of char arrays). But make sure they have enough space for the values you'll assign to them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstring>
using namespace std;

const char bool2str[2][20]={"false","true"};

int main()
{
    char str1[20]="asdf";
    char str2[20]="qwerty";

    char str[20];

    cout << bool2str[strcmp(str1,str2)<0] << endl;
    cout << bool2str[strcmp(str1,str2)>0] << endl;
    cout << bool2str[strcmp(str1,str2)==0] << endl;

    if (strcmp(str1,str2)<0) strcpy(str,str1);
    else strcpy(str,str2);

    cout << str << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}

Useful links:

http://cplusplus.com/reference/string/string/
http://cplusplus.com/reference/clibrary/cstring/strcmp/
http://cplusplus.com/reference/clibrary/cstring/strcpy/
Last edited on
@m4ster r0shi
Do you know about boolalpha? http://www.cplusplus.com/reference/iostream/manipulators/boolalpha/
Yes. But I thought I should do it like this, so that the OP can see what an array of strings looks like in both cases (using std::string and cstrings). Thanks for the info though.
@m4ster ~ Actually no. I am needing to dual sort int months[] (an array of user input integers) and char months[] (programmer defined array of "Jan", "Feb", etc.

So far I have been able to get the program to work perfectly suing integer values for both arrays however the month 'names' don not print of course.

The names index needs to coincide with the user input integers and dual sort. We have not reached the chapter on pointers and we are not to use global vars though I doubt I would need them anyway.

Most of the examples in our book thus far have been int, double or float arrays. There is but one mention of char arrays / strings and this does not include how to incorporate them while using integer arrays. I thought perhaps there may be a formula using static_cast or something?

Show me some code. Hopefully, it will help me understand what you're trying to do.
Last edited on
Well, you got a very good tip by one of the members at daniweb:

http://www.daniweb.com/forums/thread351064.html

It turns out you don't even have to mess with string comparison and assignment...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

const int N=5;

int id[N];
int integers[N];
string strings[N]; //either std::strings or cstrings

int main()
{
    //set up the id array like this:
    //id[i]=i for i=0->N-1

    //set up the strings (months) array

    //get the integers from the user

    //now, sort the id array,
    //but instead of comparing id[i] and id[j]
    //compare integers[id[i]] and integers[id[j]]

    //then you just have to output
    //integers[id[i]],strings[id[i]] pairs
    //for i=0->N-1

    return 0;
}

Though, I don't really understand why you want to do this (or if this is really what you want to do)... Maybe I'm just missing something... Explaining the problem in english is more likely to get you a good reply. Could you give examples of what the user enters as input and what your program is supposed to output?
Last edited on
Hi ~ sorry for the confusion. I guess I'm being a bit vague because it is an assignment. I am including the dualSort function and the showOrder function. Hopefully this will help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

void getRain(double rain[], int SIZE)
{
	

	I have coded this function and it is working.	
	
}

}



void dualSort(int month[], double rain[], int size)
{
	
        I have this coded as well and it is working correctly. However I would like to have the program output be string names  of the months not just integers.

}

void showOrder(int month[], double rain[], int num)
{
	cout << "\n\tMonth\t\t  Rainfall\n";
	cout << "\t--------------------------\n";
	

	for(int index = 0; index < num; index++)
	{
		cout << "\t" << month[index] << "\t\t";
		cout << "\t" << rain[index] << endl;
	}
	cout << endl;
}
Last edited on
Well, I guess you're almost done then...
All you have to do is make an array with month names and use your month array to index it...

1
2
3
4
5
6
7
8
9
10
11
12
13
//...

const char * month_name[]={"Jan","Feb" /*etc*/};

//...

for(int index = 0; index < num; index++)
{
    cout << "\t" << month_name[month[index]-1] << "\t\t";
    cout << "\t" << rain[index] << endl;
}

//... 
Last edited on
Would the * (asterik) indicate a pointer? If so that is the next chapter and I guess I should leave it as is.

Thanks so much for all of your help. It is very frustrating to be a beginning programmer. I only hope it is worth it in the end. ;D
scarlettmoon wrote:
Would the * (asterik) indicate a pointer?

Oh, yeah, sorry about that. You can do it like this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

//array of 12 strings, each being an array of 20 chars
const char month_name[12][20]=
{
    "January","February","March", "April","May",
    "June","July","August","September","October",
    "November","December"
};

int main()
{
    for (int i=0; i<12; i++)
        cout << month_name[i] << endl;

    cout << "hit enter to quit...";
    cin.get();
    return 0;
}
Am going to try this with my sort and see if I can get the names of the months to sort with the user input. I'll let you know how it goes! You have been very patient, Thanks again!
As suspected I received the following error:

dualSort' : cannot convert parameter 1 from 'const char [12][10]' to 'char []'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I knew that was the problem before . I am just unsure where to add the static_cast. Curse this book of mine! This is the reason I stuck with integers and used the month's subscript. I'd forgotten I did try your solution above with dualSort only to receive the same result!
You are not supposed to change anything in your dualSort.
Check this out, I've written the parts you need to change in bold:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;

void getRain(double[], int);
void dualSort(int[], double[], int);
void showOrder(int[], char[][20], double[], int);

int main()
{
    const int SIZE = 12; //size of array
    double rain[SIZE];
    const int NUM_MONTHS = 12;
    int month[NUM_MONTHS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

    char month_name[NUM_MONTHS][20]=
    {
        "January","February","March", "April","May",
        "June","July","August","September","October",
        "November","December"
    };

    getRain(rain, SIZE);
    dualSort(month, rain, SIZE);
    showOrder(month, month_name, rain, SIZE);

    system("pause");
    return 0;
}

void getRain(double rain[], int SIZE) {/*...*/}

void dualSort(int month[], double rain[], int size) {/*...*/}

void showOrder(int month[], char month_name[][20], double rain[], int num)
{
    cout << "\n\tMonth\t\t  Rainfall\n";
    cout << "\t--------------------------\n";

    for(int index = 0; index < num; index++)
    {
        cout << "\t" << month_name[month[index]-1] << "\t\t";
        cout << "\t" << rain[index] << endl;
    }

    cout << endl;
}
Topic archived. No new replies allowed.