Sorting

Hi again,

writing a program to do selection sort:
1)input values from text file to array
2)sort array

unfortunately, as usual, not going well.
I believe the algorithm is correct, and I know I need to pass the value by reference. If it was the individual tasks I could do it, but I don't know how to pass the value by reference within the sort function. Here's the 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
void selectSortDescend(double a[], int n)
{
   for (int i=0; i<n-1; i++)
   {
      double maxval=a[i]; double g=a[i];
      for (int j=i+1; j<n; j++)
      {
         if (a[j]>maxval)
         {
            maxval=a[j];
         }
      }
      if (maxval!=a[i])
      {
         a[i]=maxval;
         a[j]=g;
      }
   }         
}


using namespace std;

int main()
{
   ifstream fin("unsorted.txt");
   ofstream fout("sorted.txt");
    
   double a[1000];
   int n=0;
   
   while (fin>>a[n])
   {
      n++;
   }
   
   selectSortDescend(a[],n);
}


I'm also confused as to why I'm getting an error, line 73
expected primary expression before "]" token.

Any advice appreciated, not just looking for code to plug in, I'm finding programming exceptionally difficult conceptually, very frustrating. Thanks
Last edited on
Big update!

Got something that works, but the function should know whether the sort shall be ascending or descending. I don't know how to enter a string as the variable in the function and see which one it is? Here's the code.

How should I pass the string to the function so it can determine whether it should sort up or down?

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
void selectSort(double array[], int n, string sortType)
{
   if (sortType=="descending")
   {
      for(int i = 0; i<(n-1); i++)
      {
         int imax = i; double maxval = array[i];
         for (int j = i; j <(n-1); j++)
         if (array[j] > maxval)
         {
            imax = j;
            maxval = array[j];
         }
         array[imax] = array[i];
         array[i] = maxval;
      }
   }
   if (sortType=="ascending")
   {
      for(int i = 0; i<(n-1); i++)
      {
         int imin = i; double minval = array[i];
         for (int j = i; j <(n-1); j++)
         if (array[j] < minval)
         {
         imin = j;
         minval = array[j];
         }
         array[imin] = array[i];
         array[i] = minval;
      } 
   }
}



using namespace std;

int main()
{
   ifstream fin("unsorted.txt");
   ofstream fout("sorted.txt");
    
   double a[1000];
   int b;
   string c = "descending";
   
   for (b=0; b<1000; b++)
   {
      fin >> a[b];
   }
   
   selectSort(a,b,c);
   
   for (int x=0; x<5; x++)
   {
      fout << a[x] << endl;
   }
   
   fout << ":" << endl << ":" <<endl;
   
   for (int y=b-5; b; y++)
   {
      fout << a[y] << endl;
   }
   
   fout << endl << "n=" << b << endl;
}
Please help, won't compile, devc++ encounters and error and threatens to close. It doesn't actually shut down, though. I feel it is so close!
Have to sleep now :(. Maybe in the morning some has offered help on how to get the function to receive the string and see what it's equal to...
Thanks!!!

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
void selectSort(double array[], int n, std::string ST)
{
   if (ST=="descending")
   {
      for(int i = 0; i<(n-1); i++)
      {
         int imax = i; double maxval = array[i];
         for (int j = i; j <(n-1); j++)
         if (array[j] > maxval)
         {
            imax = j;
            maxval = array[j];
         }
         array[imax] = array[i];
         array[i] = maxval;
      }
   }
   
   if (ST=="ascending")
   {
      for(int i = 0; i<(n-1); i++)
      {
         int imin = i; double minval = array[i];
         for (int j = i; j <(n-1); j++)
         if (array[j] < minval)
         {
         imin = j;
         minval = array[j];
         }
         array[imin] = array[i];
         array[i] = minval;
      } 
   }
}

using namespace std;

int main()
{
   ifstream fin("unsorted.txt");
   ofstream fout("sorted.txt");
    
   double a[1000];
   int b;
   string c = "descending";
   
   for (b=0; b<1000; b++)
   {
      fin >> a[b];
   }
   
   selectSort(a,b,c);
   
   for (int x=0; x<5; x++)
   {
      fout << a[x] << endl;
   }
   
   fout << ":" << endl << ":" <<endl;
   
   for (int y=b-5; b; y++)
   {
      fout << a[y] << endl;
   }
   
   fout << endl << "n=" << b << endl;
}
You should use names that are easier for somebody to understand, instead of a,b,c....

The string in the sorting section works, you can add a cout << ST; at the entry point of the function and you will see it.
But there are some other mistakes...
The most obvious (and wherethe program crash) is in line 63 of your program as you last posted it. The for loop is infinite and has as a result the a[y] to go out of range... I'm not really sure what you ar tryin to do from line 53 and bellow...
And aso why you use an double array with 1000 positions and you don't initialize a new one via a pointer to handle exactly the amount of data you want?
I rewrote it for you so yo can see what was wrong.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void sorting(double nums[], int size, string method);

int main(){
	double *myArray;
	ifstream fin("unsorted.txt");//Open file for input data
	int size;
	//Find size of file
	for(size=0; !fin.eof(); size++){
		string temp;
		fin >> temp;
	}
	fin.seekg(ios::beg);//Move pointer back to the beggining of file
	myArray = new double[size];//Create new array to handle our data
	//Assign data to array
	for(int i=0; i<size; i++){
		fin >> myArray[i];
	}
	fin.close();//Close file since we don;t need it any more
	sorting(myArray, size, "ascending");//Send the array for sorting with ascending or descending parameter
	ofstream fout("sorted.txt");//Open file for output
	//Write our array to file
	for(int i=0; i<size; i++){
		fout << myArray[i] <<'\n';
	}
	fout.close();//close output file
	return 0;
}
	
void sorting(double nums[], int size, string method){
	double t;
	if (method == "ascending"){
		for(int a=1; a<size; a++)
			for(int b=size-1; b>=a; b--) {
				if(nums[b-1] > nums[b]) {
					t = nums[b-1];
					nums[b-1] = nums[b];
					nums[b] = t;
				}
			}
	}else if(method == "descending"){
		for(int a=1; a<size; a++)
			for(int b=size-1; b>=a; b--) {
				if(nums[b-1] < nums[b]) {
					t = nums[b-1];
					nums[b-1] = nums[b];
					nums[b] = t;
				}
			}
	}else{
		cout << "Method of sorting unknown...!!!'\n";
		cout << "Output file is unsorted...\n";
	}
}


Hope it will help you.
Topic archived. No new replies allowed.