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
Topic archived. No new replies allowed.