Having problems sorting an array

So, for some reason I tried posting this in the beginners forum, but it wouldn't let me. So I'll post it here.

I am trying to sort a 2D array via selection sort, and before I am hounded about the inefficiency of a selection sort on the amount of data I am sorting, this is a class assignment.

My code will run, but it doesn't sort the array. I am trying to sort the array in ascending order by column 1. I have read the input from a text file which consists of an interger ID # and a double sales price.

I've also tried using nested for loops to do this and it will change my output, but it doesn't sort it. So I just deleted the outer for loop in the sort function.

What do I need to do to make this work?

My code is as follows:

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

using namespace std;

//Funtion Prototypes
void ReadArray(double ary[][2]);
void PrintArray(double ary[][2]);
void SortArray(double ary[][2]);

//Global Variables
int r;
int c;
ifstream infile;

double ary[208][2];

int main()
{
	

	infile.open("E:\\SalesPerson.txt");

	if (!infile)
	{
		cout << "Cannot not open file 'SalesPerson.txt'" << endl;
		system("PAUSE");
		return 0;
	}
	
	ReadArray(ary);  //Calling Read Function
	SortArray(ary);  //Calling Sort Function
	cout << "\nSorted: \n" << endl;
	PrintArray(ary); //Calling Print Function
	
	infile.close();
	
	
	system("PAUSE");
	return 0;
}


//Read text file into 2D-Array
void ReadArray(double ary[][2])
{
	
	for (r = 0; r < 208; r++)
		for (c = 0; c < 2; c++)
			infile >> ary[r][c];
}

//Sort Array
void SortArray(double ary[][2])
{
	double temp;
	for (c = 0; c < 2; c++)
	{
		
		temp = ary[r][c];
		ary[r][c] = ary[r + 1][c];
		ary[r + 1][c] = temp;
		
	}
	return;

}


//Print Array 
void PrintArray(double ary[][2])
{
	for (r = 0; r < 208; r++)
	{
		for (c = 0; c < 2; c++)
		{
			cout << ary[r][c] <<" ";
		}
		cout << endl;
	}



Any and all help is greatly appreciated!

Thanks,

ChrisC
The first thing I would recommend is getting rid of your global variables. Make them local to the functions or pass them as parameters. Once you do that, I think the problem will be more obvious to you.
Topic archived. No new replies allowed.