help with sorting structural array ???

I'm new to sorting and writing a program with an array of struct records, not even sure what to do here..

My Problem: i'm trying to figure out how to pass the array 'List' into the void function 'SSort' so that i can sort the elements in the struct CustomerRec either by

list[].zipcode or by list[].outstandingBalance.

I've been reading and looking up info online but not quite getting there. modified my code a few times and still not able to find a way to pass my array of structs into the sort function correctly.

I originally had this array inside its own struct called CustomerData like this:

1
2
3
4
5
6
7

struct CustomerData
{
     CustomerRec list[MAX_CUSTOMERS]; 
     int ElementNums;
}


then I took it out of that struct placed it by itself and moved ElementNums into CustomerRec:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct CustomerRec
{
	string firstname;
	string lastname;
	int housenumber;
	string streetname;
	string streetdesignation;
	string city;
	string state;
	int zipcode;
	double outstandingbalance;
	string transactionType;	

	int ElementNums;

};  

CustomerRec list[MAX_CUSTOMERS];  // array named: "list" to hold customer records. 


now I have it setup like my code below. It still feels like I am just missing something thats right there in front of me...

Any help with figuring this out would be awesome!!!

I highlighted what I believe to be the key code in bold and I believe this is where I am having the problems ...

here is the relevant code that i have:

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

using namespace std;

const int MAX_CUSTOMERS = 25; //max number of entries in ARRAY

struct CustomerRec
{
	string firstname;
	string lastname;
	int housenumber;
	string streetname;
	string streetdesignation;
	string city;
	string state;
	int zipcode;
	double outstandingbalance;
	string transactionType;	

	int ElementNums;

}list[MAX_CUSTOMERS];  // array named: "list" to hold customer records


void ReadCustomers(ifstream& infile1, CustomerRec& record);
void ReadTransactions(ifstream& infile2, CustomerRec& record);
void CalcInterestNoTrans(CustomerRec& record);
void SSort(double a[ ], int size)
void PrintCustomerData(ofstream& outfile1, CustomerRec& record);
void PrintCustomerAmounts(ofstream& outfile2,CustomerRec& record);
void PrintCustomerMailings(ofstream& outfile3, CustomerRec& record);

/* this section is my function calls that I have listed in: Main */

        CustomerRec record;
	record.ElementNums = 0;

	//calls functions to read in files
	ReadCustomers(infile, record);	
	ReadTransactions(infile2, record);	
	
	//prints out customer records before calculating interest
	// on non trasnsactioned records to "thisMonth.txt" file
	PrintCustomerData(outfile, record);

	//searches array LIST and indentifies records that had
	// no transactions then charges 1.5% interest to outstandingBalance
	CalcInterestNoTrans(record);
	
	SSort(list[MAX_CUSTOMERS], record.ElementNums);

       	//prints out customer names with new amounts owed
	// to output file "amounts.txt"
	PrintCustomerAmounts(outfile2, record);

	//prints out customer and address info
	// to output file "mailings.txt"
	PrintCustomerMailings(outfile3, record);	

/* -------------------------- end of main section ---------------------- */

" What I am trying to do is, sort the array of struct Customer Rec data
by the .outstandingBalance element and then trying to print out into a txt file with the funtion below: "

void PrintCustomerAmounts(ofstream& outfile2, CustomerRec& record)
{
	double TotalAmountOwed = 0.00;	
	
	//print out NAME, and Amount Owed
	outfile2  << setw(12) << "NAME" << setw(40) << "AMOUNT OWED" << endl;
	
	for (int count = 0; count < record.ElementNums; count++)
	{
		outfile2  << setw(5) << " " << list[count].firstname << " " 
		          << left << setw(30) << list[count].lastname  << left << setw(3) << " "				  
			  << setprecision(2) << fixed << setw(7) << right << list[count].outstandingbalance << " " << endl;				 
	}
	
	// then calculate a total output..
	for (int k = 0; k < record.ElementNums; k++)
	{
		TotalAmountOwed = TotalAmountOwed + list[k].outstandingbalance;		
	}

	outfile2  << setw(51) << " ------- " << endl;
	outfile2  << setw(38) << "TOTAL" << setw(12) << TotalAmountOwed << endl;

}


HERE IS THE OUTPUT I AM GETTING FROM MY FUNCTION:
        NAME                             AMOUNT OWED
     John Smith                             429.52 
     Mark Otter                            1217.19 
     David Bender                            373.50 
     Tim Bud                                 0.00 
                                           ------- 
                                 TOTAL     2020.21


" I am having formatting issues with my output text too
somewhere in the function, void PrintCustomerAmounts
and I am not sure how to fix that either.. "

MY OUTPUT SHOULD LOOK LIKE THIS;
 
       NAME                             AMOUNT OWED
     Mark Otter                            1217.19 
     John Smith                             429.52 
     David Bender                           373.50 
     Tim Bud                                  0.00 
                                           ------- 
                                 TOTAL     2020.21


"HERE IS MY SORT FUNCTION CODE:"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  
void SSort (double a[ ], int size)
{
	double temp;					// temp place to store element during swap
	int minplace;					// index of currently smallest element
							// for each place in array

	for (int pass = 0; pass < size-1;pass++)	
	{					 
		minplace = pass;			// assume minimum already at index to fill
		for (int i = pass+1; i < size; i++)	// look through rest of array for smaller elt
		{
			if (a[i] < a[ minplace])	// if found a smaller one
				minplace = i;		// keep its index as the smallest so far
		}
		temp = a[pass];			        // swap current value with minimum value
		a[pass] = a [minplace];
		a[minplace] = temp;
	}	

}




Last edited on
So you're trying to sort an array a[] of size "size" in ascending order? If that's the case, I'd just use a vector with the sort() function.

The method you are using seems to be a bubble sort, for which you could probably find a lot of info online. I think you'll find you'll get more help if you are more specific with your problems, like just supplying the SSort function for example, and telling us exactly what it's supposed to do.
Thanks for responding.. sorry no i am trying to sort the array called list[MAX_CUSTOMERS] which is located in the struct CustomerData and is associated to the struct CustomerRec. The array is filled with customer data. The sort function I am trying to use is a selection sort.

it is included with the code I posted, its listed at the bottom and is called "void SSort (int a[ ], int size)" the parameters are wrong and not working correctly. this sort is a function I got from my professor, however I am having problems on modifying it to work with my coding. never tried to sort a struct before, not sure how to make it work . this is what i can use advise with doing. The sort function is to just sort the elements in the struct array called list. Thanks
Topic archived. No new replies allowed.