Sorting arrays in ascending order

// OK this program outputs an array of numbers which are read from two .txt //files which are set1 and set2.

// set1.txt has: 8 37 29 31 40 25 18 38 4 45 34 39 12 21 24 3 5 23 26 44

// set2.txt has: 46 42 25 20 19 29 49 4 32 2 10 12 39 17 33 6 3 15 45 21

// But the problem is that when you run the program, the numbers do not come out // in numerical order. How do I fix this?



#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> // exit function defined in this one
using namespace std;

const int CAP = 100;
const int NOT_FOUND = -1;

void printArr( int array[], int count );
int indOf( int array[], int count, int key );
int indOfMin( int array[], int count );
int indOfMax( int array[], int count );

// we will explain these two below on Friday

void calcIntersect( int set1[], int cnt1,
int set2[], int cnt2,
int interSet[], int &interCnt );

void calcUnion( int set1[], int cnt1, int set2[],
int cnt2, int unionSet[], int &unionCnt );

int main()
{
ifstream infile1,infile2;
string fname1,fname2;
cout << "Enter two input filenames: ";
cin >> fname1 >> fname2;

infile1.open( fname1.c_str(), ios::in );
if ( ! infile1 )
{
cout << " ERROR: open attempt failed on input file: " << fname1 << "\n";
exit(0);
}
infile2.open( fname2.c_str(), ios::in );
if ( ! infile2 )
{
cout << " ERROR: open attempt failed on input file: " << fname2 << "\n";
exit(0);
}


int set1[CAP];
int cnt1=0;


int set2[CAP];
int cnt2=0;


int interSet[CAP];
int interCnt=0;


int unionSet[CAP];
int unionCnt=0;


while( cnt1<CAP && infile1 >> set1[cnt1++] )
{}
infile1.close();

while( cnt2<CAP && infile2 >> set2[cnt2++] )
{}
infile2.close();

// WE HAVE LOADED UP THE 2 SETS. PRINT THEM
cnt1 = cnt1 - 1;
cnt2 = cnt2 - 1;
cout << "SET#1: ";
printArr( set1, cnt1 );
cout << "SET#2: ";
printArr( set2, cnt2 );

/*do
{
cout << "# to search for? "; // negative # means quit loop
int key;
cin >> key; // read user response from kbd into n

if (key < 0) break;

int index = indOf( set1, cnt1, key );

if ( index == -1 )
cout << key << " not found :(\n";
else
cout << key << " found at index " << index << "\n";
}
while ( true ); // infinite loop you must break out*/

calcIntersect( set1, cnt1, set2, cnt2, interSet, interCnt );
calcUnion( set1, cnt1, set2, cnt2, unionSet, unionCnt );

cout << "Intersection of set#1 and set#2: ";
printArr( interSet, interCnt );
cout << "Union of set#1 and set#2: ";
printArr( unionSet, unionCnt );

return 0;
}

void printArr( int array[], int count )
{
// 50 12 3 41 23 63 61 . . .
for ( int i=0 ; i<count ; i++ )
cout << array[i] << " ";
cout << "\n" ;
}

int indOf( int array[], int count, int key )
{
for ( int i=0 ; i<count ; i++ )
if ( array[i] == key )
return i;

return NOT_FOUND; // i.e. -1 the "not found" value
}

void calcIntersect( int set1[], int cnt1, int set2[], int cnt2, int interSet[], int &interCnt )
{
for (int i = 0; i < cnt1; i++)
{
for (int j = 0; j < cnt2; j++)
{
if (set1[i] == set2[j])
interSet[interCnt++] = set1[i];


}
}

}

void calcUnion( int set1[], int cnt1, int set2[], int cnt2, int unionSet[], int &unionCnt )
{
bool match = false;


for (int i = 0; i < cnt1; i++)
{
unionSet[unionCnt++] = set1[i];
}
for (int j = 0; j < cnt2; j++)
{
for (int x = 0; x < unionCnt && !match; x++)
{
if ( set2[j] == unionSet[x] )
{
match = true;
}


}
if (!match)
{
unionSet[unionCnt++] = set2[j];
}


match = false;
}



}
// YOUR CODE
closed account (2UD8vCM9)
sup my nigga mistersl56

i didnt see anyone else helping u out so i thought i'd try to help u out you know what im saying

yo dawg so check this out right

i made dis function its called SortThatShit - it does exactly what its supposed to - it sorts that shit in ascending order

let me know if you need help understanding whats going on
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> // exit function defined in this one
using namespace std;

const int CAP = 100;
const int NOT_FOUND = -1;

void printArr( int array[], int count );
int indOf( int array[], int count, int key );
int indOfMin( int array[], int count );
int indOfMax( int array[], int count );

void SortThatShit(int * myset, int mycnt);

// we will explain these two below on Friday

void calcIntersect( int set1[], int cnt1,
int set2[], int cnt2,
int interSet[], int &interCnt );

void calcUnion( int set1[], int cnt1, int set2[],
int cnt2, int unionSet[], int &unionCnt );

int main()
{
ifstream infile1,infile2;
string fname1,fname2;
cout << "Enter two input filenames: ";
cin >> fname1 >> fname2;

infile1.open( fname1.c_str(), ios::in );
if ( ! infile1.is_open() )
{
cout << " ERROR: open attempt failed on input file: " << fname1 << "\n";
exit(0);
}
infile2.open( fname2.c_str(), ios::in );
if ( ! infile2.is_open() )
{
cout << " ERROR: open attempt failed on input file: " << fname2 << "\n";
exit(0);
}


int set1[CAP];
int cnt1=0;


int set2[CAP];
int cnt2=0;


int interSet[CAP];
int interCnt=0;


int unionSet[CAP];
int unionCnt=0;


while( cnt1<CAP && infile1 >> set1[cnt1++] )
{}
infile1.close();

while( cnt2<CAP && infile2 >> set2[cnt2++] )
{}
infile2.close();



cnt1 = cnt1 - 1;
cnt2 = cnt2 - 1;
//Now we gotta fuckin sort that shit u know what im saying? damn
SortThatShit( set1, cnt1 );
SortThatShit( set2, cnt2 );
// WE HAVE LOADED UP THE 2 SETS. PRINT THEM
cout << "SET#1: ";
printArr( set1, cnt1 );
cout << "SET#2: ";
printArr( set2, cnt2 );

/*do
{
cout << "# to search for? "; // negative # means quit loop
int key;
cin >> key; // read user response from kbd into n

if (key < 0) break;

int index = indOf( set1, cnt1, key );

if ( index == -1 )
cout << key << " not found :(\n";
else
cout << key << " found at index " << index << "\n";
}
while ( true ); // infinite loop you must break out*/

calcIntersect( set1, cnt1, set2, cnt2, interSet, interCnt );
calcUnion( set1, cnt1, set2, cnt2, unionSet, unionCnt );

cout << "Intersection of set#1 and set#2: ";
printArr( interSet, interCnt );
cout << "Union of set#1 and set#2: ";
printArr( unionSet, unionCnt );
system("pause");
return 0;
}

void printArr( int array[], int count )
{
// 50 12 3 41 23 63 61 . . .
for ( int i=0 ; i<count ; i++ )
cout << array[i] << " ";
cout << "\n" ;
}

int indOf( int array[], int count, int key )
{
for ( int i=0 ; i<count ; i++ )
if ( array[i] == key )
return i;

return NOT_FOUND; // i.e. -1 the "not found" value
}

void calcIntersect( int set1[], int cnt1, int set2[], int cnt2, int interSet[], int &interCnt )
{
for (int i = 0; i < cnt1; i++)
{
for (int j = 0; j < cnt2; j++)
{
if (set1[i] == set2[j])
interSet[interCnt++] = set1[i];


}
}

}

void calcUnion( int set1[], int cnt1, int set2[], int cnt2, int unionSet[], int &unionCnt )
{
bool match = false;


for (int i = 0; i < cnt1; i++)
{
unionSet[unionCnt++] = set1[i];
}
for (int j = 0; j < cnt2; j++)
{
for (int x = 0; x < unionCnt && !match; x++)
{
if ( set2[j] == unionSet[x] )
{
match = true;
}


}
if (!match)
{
unionSet[unionCnt++] = set2[j];
}


match = false;
}



}
// YOUR CODE 

void SortThatShit(int * myset, int mycnt)
{
	for (int a=0;a<mycnt;a++) //go through list we use 2 fors so we make sure we go through and move everything and not just move everything 1 time u know what im saying
	{
		for (int i=0;i<(mycnt-1);i++) //go through list
		{
			if (myset[i] > myset[i+1])
			{
				swap(myset[i],myset[i+1]); //swap that shit
			}
		}
	}
}
Sweet man thanks! Appreciate it!
Topic archived. No new replies allowed.