sorting help needed

hi guys, I have a problem with a sorting loop... here is the whole program; it may be confusing...

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
	float catk, ccr, ccd, camp;
	float samp[32], saatk[32], scd[32], sct[32], sdmg[32], sdps[32];
	int temp;
	char yn;
	string sname[32];

	cout << "catk = "; cin >> catk;
	cout << "ccr  = "; cin >> ccr;
	cout << "ccd  = "; cin >> ccd;
	cout << "camp = "; cin >> camp;

	cin.ignore();

	system("cls");

	for (int i = 1;; i++)
	{
		cout << "sname = "; getline(cin, sname[i]);
		cout << "samp  = "; cin >> samp[i];
		cout << "saatk = "; cin >> saatk[i];
		cout << "scd   = "; cin >> scd[i];
		cout << "sct   = "; cin >> sct[i];

		for (;;)
		{
			cout << "continue?"; yn = _getch();

			system("cls");

			switch (yn)
			{
			case 'y':
				break;
			case 'n':
				system("cls");
				
				for (int j = 1; j <= i; j++)
				{
					sdmg[j] = catk + (catk / 100 * (camp + samp[j])) + (catk + (catk / 100 * (camp + samp[j]))) / 100 * ((ccd + scd[j]) / 100 * ccr);
					sdps[j] = (catk + (catk / 100 * (camp + samp[j])) + (catk + (catk / 100 * (camp + samp[j]))) / 100 * ((ccd + scd[j]) / 100 * ccr)) / sct[j];
				}
				
				cout << "DAMAGE" << endl;
				
				temp = 1;

				for (int j = 1; j <= i; j++)
				{
					for (int k = 2; k <= i; k++)
					{
						if (sdmg[k] > sdmg[k - 1])
						{
							temp = k;
						}
					}

					cout << j << ". " << sname[temp] << " - " << sdmg[temp] << endl;
					sdmg[temp] = 0;
					temp = 1;
				}

				cout << endl;
				cout << "DAMAGE PER SECOND" << endl;

				temp = 1;

				for (int j = 1; j <= i; j++)
				{
					for (int k = 2; k <= i; k++)
					{
						if (sdps[k] > sdps[k - 1])
						{
							temp = k;
						}
					}

					cout << j << ". " << sname[temp] << " - " << sdps[temp] << endl;
					sdps[temp] = 0;
					temp = 1;
				}

				cout << endl;

				system("pause");
				exit(0);
			default:
				continue;
			}

			cin.ignore();

			break;
		}
	}

	system("pause");

	return 0;
}


the part that I don't get why isn't it working is this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
temp = 1;

				for (int j = 1; j <= i; j++)
				{
					for (int k = 2; k <= i; k++)
					{
						if (sdmg[k] > sdmg[k - 1])
						{
							temp = k;
						}
					}

					cout << j << ". " << sname[temp] << " - " << sdmg[temp] << endl;
					sdmg[temp] = 0;
					temp = 1;
				}


so here is the thing: I'm trying to find out what is the biggest "sdmg" and then output it's name... "sname" and do this for every one of them to sort them in descending order; but only to sort their "sname" and "sdmg", not to change their values. when I run the program it doesn't work!!! the calculations are right, but they're not sorted :-< can someone help me, please?
@chippzanuff93
Your code isn't checking the string's lengths, but, I believe, the ASCII values of the letters. A name of Joe would beat out a name like Alexander, since the 'J' in Joe, has a higher value than the 'A' in Alexander.

I didn't test this section of code, but have used similar code in other programs I've developed.
Hope it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int j = 1; j <= i; j++)
	{
		for (int k = 2; k <= i; k++)
		{
			int len1 = sdmg[k].length(); // len1 gets the value of the string name
                        int len2 = sdmg[k-1].length();  // len2 gets the value of this string name
                        if (len1 > len2) // Check for lengths here.
			{
				temp = k;
			}
		}
		cout << j << ". " << sname[temp] << " - " << sdmg[temp] << endl;
		sdmg[temp] = 0;
		temp = 1;
}
Last edited on
I appologise, but I think you misunderstood the code/me. "sdmg" is a float array. it stores numbers, not strings. the thing is... I wanna store the "place" of the highest number to keep track of the "name" that it stands for (sname) so that I can list the name and it's coresponding number in descending order. I really could use some help, please :-s
@chippzanuff93

Sorry. I did check when I first started looking at your problem, and did see the variables were floats, but when I saw sname[], I screwed up. Anyway, here is a bubble sort that should work. I ran your program with it, and it seems correct, but I'm not quite sure what values you were actually going to use.
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
temp = 1;

for(int j = 0; j < i; j++) //Bubble Sort
{
	for(int k = 0; k < i - j; k++)
	{
		if(sdmg[j] > sdmg[j + 1])
		{
			int temp = sdmg[j];
			sdmg[j] = sdmg[j+1];
			sdmg[j+1] = temp;
		}
		cout << j << ". " << sname[temp] << " - " << sdmg[temp] << endl;
		sdmg[temp] = 0;
		temp = 1;
	}
}
cout << endl;

cout << "DAMAGE PER SECOND" << endl;

temp = 1;

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

	cout << j << ". " << sname[temp] << " - " << sdps[temp] << endl;
	sdps[temp] = 0;
	temp = 1;
}


If this still isn't quite what you needed, please explain again to me, as sometimes, I can be very dense, as shown in my previous response.
oh well, I'm afraid it isn't. here is the thing. first I create a for loop with "j" for listing some data. that data I want it to be in descending order from highest "sdmg" to lowest "sdmg" the tricky part is that I want to "memorise" the "sname" for each one of them and list not only the numbers in descending order, but the numbers with their corresponding names...

ok let's say I enter some data there... 4 names and some other data that makes 4 numbers. let's say...
Andy has the number 85
Zack has the number 96
Chriss has the number 34
Alex has the number 36
so... I want to list them like this:
1. Zack - 96
2. Andy - 85
3. Alex - 36
4. Zack - 34

btw you're not dense; if you don't understand something, it's the one who tried to explain's fault

EDIT: I think I got it... I was complicating too much tryint not to change the variable's values... but I think I'll try the bubble loop and change both the sdmg and the sname at the same time... I'll tell you if I succeeded

EDIT2: I made this bubble sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void sort(float arr1 [], string arr2 [], int size)
{
	int temp1;
	string temp2;

	for (int i = 0; i <= size; i++)
	{
		for (int j = 0; j <= size - 1; j++)
		{
			if (arr1[j] > arr1[j + 1])
			{
				temp1 = arr1[j];
				arr1[j] = arr1[j + 1];
				arr1[j + 1] = temp1;

				temp2 = arr2[j];
				arr2[j] = arr2[j + 1];
				arr2[j + 1] = temp2;
			}
		}
	}
}


with

1
2
3
4
5
6
sort(sdmg, sname, i);

				for (int j = 0; j <= i; j++)
				{
					cout << j + 1 << ". " << sname[j] << " - " << sdmg[j] << endl;
				}


in main function...

this way I can swap both the sdmg and it's corresponding names. it worked pretty well I can tell! but something is still not right though... it's in ascending order :| not descending. how can I make it to be in descending order, please?

EDIT3: I managed to make everything to work! ^_^

changed

1
2
3
4
				for (int j = 0; j <= i; j++)
				{
					cout << j + 1 << ". " << sname[j] << " - " << sdmg[j] << endl;
				}


into

1
2
3
4
for (int j = i; j >= 0; j--)
				{
					cout << i - j + 1 << ". " << sname[j] << " - " << sdmg[j] << endl;
				}


and it's sorted just like I wanted ^_^ thank you very much for your help; and I hope I helped everyone else who read this topic ^_^
Last edited on
@chippzanuff93

Glad to see you got your program running as expected. And I'm sure you have helped others understand sorting problems they may have in their programs.
Topic archived. No new replies allowed.