Storing inside an array and Sorting

Hello guys, I am having trouble with some storing. My issue is a bit complicated but I will try to be as detailed as possible. I'll describe the program first, second my issue, and finally my code.

So my program is suppose to read off a textfile and grab the information and store it into an array or vector. Were free to use whichever but I chose an array, since I am not to experienced with vectors.


Course Number / Section Number
Textfile Example
-------------------------
801 7045
816 1206
832 3450
864 1688
802 3285
807 3278
--------------------------

Were then suppose to sort the list by its Section Number.

Sorted Example
--------------------------
1206 816
1688 864
3278 807
3285 802
3450 832
7045 801
---------------------------

All my sorting methods are correct so far.
Bubble
Selection
Insertion
Quick
Merge
Heap (have not done yet)

Okay, now for my issue.
So i display and then store the information into an array. Since the way its written inside the file my array stores arr[0] = 802, arr[1] = 7045 etc. So that already doesn't even make sense at all to me.

I've already tried storing them in different arrays. Such as courseNum[0] = 802, SectionNum[0] = 7045 etc.
In the end, it doesn't make any sense when I sort the Sections because the Course Number is suppose to align correctly with its Section Number.

Or am I doing this entire program wrong?. because while writing this, I just realized I might need to use Queues/ dynamically allocated array for Heap .

Hope you guys understood



I will only provide the Bubble Sort files, since it will be way to long if I show all of them.


Bubble.h
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

struct BubbleSort
{

	void BubbleSorting(int a[],int);
	void print(int a[],int);

};


Bubble.cpp
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
#include <iostream>
#include "Bubble.h"
using namespace std;



void BubbleSort::BubbleSorting(int a[],int size)
{
	bool done = false; 
	int pass = 0;      
	int temp;

	while (!done) 
	{
		done = true;
		
		for (int i = 0; i < size - 1; ++i) 
		{
			if (a[i] > a[i + 1]) 
			{
				temp = a[i];
				a[i] = a[i + 1];
				a[i + 1] = temp;
				done = false;  
			}
		}
	}
}


void BubbleSort::print(int a[], int size)
{
	
	
	for (int i = 0; i < size; ++i) 
	{
		cout << a[i];
		cout << endl;
	}
}



test.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <fstream>
#include "Merge.h"
#include "Quick.h"
#include "Insertion.h"
#include "Bubble.h"
#include "Selection.h"

using namespace std;


char showMenu();


int main()
{
	char choice;
	const int SIZE = 12;
	int courseNum[12];
	int sectionNum[12];
	//int a[] {8, 5, 10, 4, 3, 11, 6, 7, 2, 1};

	ifstream file("textfile.txt");
	if (file.is_open())
	{


		for (int i = 0; i < 12; i++)
		{
			file >> courseNum[i];
			//file >> sectionNum[i];
		}
	}

	for (int i = 0; i < 12; i++)
	{
		cout << courseNum[i]; //<< " " << sectionNum[i];
		cout << endl;
	}

	QuickSort qSort;
	MergeSort mSort;
	BubbleSort bSort;
	InsertionSort iSort;
	SelectionSort sSort;
	

	do
	{
		choice = showMenu();
		switch (choice)
		{
		case 'A': 
			/*sSort.print(a, 0, SIZE - 1);
			cout << endl;
			sSort.selectionSort(a, SIZE);
			sSort.print(a, 0, SIZE - 1);
			cout << endl;*/
			break;

		case 'B':
			bSort.BubbleSorting(courseNum, SIZE);
			bSort.print(courseNum, SIZE);
			cout << endl;
			break;

		case 'C': 
			/*iSort.print(a, 0, SIZE - 1);
			cout << endl;
			iSort.insertionSort(a, SIZE);
			iSort.print(a, 0, SIZE - 1);
			cout << endl;*/
			break;

		case 'D':
			break;

		case 'E':
			/*qSort.print(a, 0, SIZE - 1);
			cout << endl;
			qSort.quickSort(a, SIZE);
			qSort.print(a, 0, SIZE - 1);
			cout << endl;*/
			break;

		case 'F': 
			/*mSort.print(a, 0, SIZE - 1);
			cout << endl;
			mSort.mergeSort(a, SIZE);
			mSort.print(a, 0, SIZE - 1);
			cout << endl << endl;
			break;*/

		default:
			break;
		}
	} while (choice != 'G');



	return 0;
}


char showMenu()
{
	char select;

	cout << "\nMenu\n";
	cout << "========\n";
	cout << "A. Selection\n";
	cout << "B. Bubble\n";
	cout << "C. Insertion\n";
	cout << "D. Heap\n";
	cout << "E. Quick\n";
	cout << "F. Merge\n";
	cout << "G. Exit\n";
	cout << endl;
	cout << "Choice: ";
	cin >> select;
	return select;
}
Last edited on
Just finished the assignment. I was able to figure it out using the two arrays.
Had to add extra things onto all the sorting implementations.

For example
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
void BubbleSort::BubbleSorting(int a[],int b[], int size)
{
	bool done = false; 
	int pass = 0;      
	int temp;
	int tempb;

	while (!done) 
	{
		done = true;
		
		for (int i = 0; i < size - 1; ++i) 
		{
			if (a[i] > a[i + 1]) 
			{
				temp = a[i];
				tempb = b[i];
				a[i] = a[i + 1];
				b[i] = b[i + 1];
				a[i + 1] = temp;
				b[i + 1] = tempb;
				done = false;  
			}
		}
	}
}




Topic archived. No new replies allowed.