Problem with exchange sort int float pairs

File name:CSC2144N.txt
Contents of .txt file
--------------------
10 7.35
-21 45.9
3 -4.56
85 34.1
-9 -32.7

--------------------
Hi this is my assignment for my C++ programming II class , and I`m a bit stuck. Im supposed to be able to sort the values entered from the text into two arrays (one with int values, and the other with float values) one at a time in ascending order of the menu option`s respected values, but the output does not come out right.Also I`m supposed to be able to take in a total of up to 10 pairs in the array but that doesn`t make sense because the text file only has 5 pairs. When I display the output, it shows the number pairs not in their correct order and -858993460, -1.07374e+08 5 times because theres no entered value for that space in the array. Im so confused as to how I`m supposed to swap this. Here`s the whole code.



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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

char repeat;
int menu;

int main()
{
	do
	{
		int menu;
			int i, j, k;// c;
		ifstream InputFile;

		struct numbers
		{
			int num[10];
			float fnum[10];

		}entry;
		InputFile.open("CSC2144N.txt");
		for (int counter=0;counter <= 9;counter++)
		{
			InputFile >> entry.num[counter];
			InputFile >> entry.fnum[counter];
			cout << entry.num[counter] << setw(9) << entry.fnum[counter] << endl;

		}
		cout << "Choose how you would like to display the sorted numbers?\n";
		cout << "1. Display pairs unsorted\n";
		cout << "2. Display pairs sorted in ascending order of the int values\n";
		cout << "3. Display pairs sorted in ascending order of the float values\n";
		cout << "4.Exit the program\n";
		cin >> menu;
		
		
		if (menu == 1)
		{
			for (int counter=0;counter <= 9;counter++) 
			{
				cout << entry.num[counter] << setw(9) << entry.fnum[counter] << endl;
			}
		}

		if (menu == 2)
		{
			
			for (int i = 0;i <= 8;i++)
			{
				for (j = i + 1; j <=9; j++)
				{
					int temp;

					if (entry.num[i] < entry.num[j])
					{
						temp = entry.num[i];
						entry.num[i] = entry.num[j];
						entry.num[j] = temp;
					}
				}
				for (i = 0; i < 9; i++)
				cout << entry.num[i] << setw(9) << entry.fnum[i] << endl;
				
			}
				
		}
		if (menu==3)
		{

			for (int i = 0;i <= 8;i++)
			{
				for (j = i + 1; j <= 9; j++)
				{
					float temp;

					if (entry.fnum[i] > entry.fnum[j])
					{
						temp = entry.fnum[i];
						entry.fnum[i] = entry.fnum[j];
						entry.fnum[j] = temp;
					}
				}
				for (i = 0; i < 9; i++)
					cout << entry.num[i] << setw(9) << entry.fnum[i] << endl;

			}

		}
		cin >> repeat;
	} while (repeat == 'Y' || repeat == 'y');
}
Last edited on
What I find interesting is the use of this structure,
18
19
20
21
22
    struct numbers
    {
        int num[10];
        float fnum[10];
    } entry;


Compare it with this from another related question:
1
2
3
4
5
    struct info
    {
        int member1;
        float member2; 
    } entry[10]; 


Both are performing a similar task.

In the first, a numbers object corresponds to the entire file.
In the second, an info object corresponds with a single line of the file.

Both are valid. I'd tend to prefer the second version, it makes sorting the pairs more controlled and probably easier/simpler.
(note, the other version turned up in a thread from another user some time ago).
Last edited on
I know about the second version. I`m using the first version because it sorts specifically the int or float values based on the menu choices 2 or 3. Its supposed to take in 10 pairs of int and float which it does quite well. I`m just having trouble sorting the values and I think its because of the blank values since there are only five pairs I don`t know how to sort with.
Yes, I'm looking through the rest of the code now.
The thing to do is to have the program driven by whatever it finds in the file, that is don't assume there will be 10 entries.

This will affect the way that you read the data into the array, but it also affects all the rest of the program too, you will need to get rid of hard-coded numbers such such as 8, 9 and 10 (except perhaps at the very start).

Instead of this:
1
2
3
4
5
6
7
8
    ifstream InputFile;
    InputFile.open("CSC2144N.txt");
    for (int counter=0;counter <= 9;counter++)
    {
        InputFile >> entry.num[counter];
        InputFile >> entry.fnum[counter];
	cout << entry.num[counter] << setw(9) << entry.fnum[counter] << endl;
    }

You might have something like this:
1
2
3
4
5
6
    int count = 0;
    ifstream InputFile("CSC2144N.txt");
    while (count < 10 && InputFile >> entry.num[count] >> entry.fnum[count])
    { 
        count++;
    }


Then the first option would look like this:
1
2
3
4
5
6
7
    if (menu == 1)
    {
        for (int i=0; i < count; i++) 
        {
            cout<< setw(9) << entry.num[i] << setw(9) << entry.fnum[i] << endl;
        }
    }


When it comes to the sorting, remember to use the count variable to set the limit of each loop (as appropriate). Also, you will need to swap both member variables.

Each time a pair of integers are exchanged, you need to do a corresponding swap of the pair of floats. Otherwise the pairs become mismatched, half of one pair being matched with the other half of some completely unrelated pair.



Topic archived. No new replies allowed.