Need Help With 3 Parallel Arrays Selction Sort

This is a discussion on Need Help With 3 Parallel Arrays Selction Sort within the C++ Programming forums, part of the General Programming Boards category; I am creating program with parrallet arrays and when the three arrays are sorted by selection sort they need to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    Need Help With 3 Parallel Arrays Selction Sort

    I am creating program with parrallet arrays and when the three arrays are sorted by selection sort they need to stay parallel, but I am haveing trouble doing this in the dualsort function. Thank YOu!


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    // Function Prototypes
    void calcSales(int [], double [], double [], int);
    void showOrder(double [], int [], char[][30], int);
    void dualSort(int [], double [], char[][30], int);
    void showTotals(double [], int [], int);
    
    // NUM_PRODS is the number of products produced.
    const int NUM_PRODS = 9;
    
    int main()
    {
    	int id[NUM_PRODS] = {914, 915, 916, 917, 918, 919, 920, 921, 922};
    	int units[NUM_PRODS] = {842, 416, 127, 514, 437, 269, 97, 492, 212};
    	double prices[NUM_PRODS] = {12.95, 14.95, 18.95, 16.95, 21.95, 31.95, 14.95, 14.95, 16.95};
    	double sales[NUM_PRODS];
    	char productNames[NUM_PRODS][30] = { "Lego","Thomas", "Dora", "Doodle", "Bratz", "Magnetix", "Wagon", "Drums", "DollHouse"}; 
    
    	calcSales(units, prices, sales, NUM_PRODS);
    	dualSort(id, sales, productNames, NUM_PRODS);
    	cout.precision(2);
    	cout.setf(ios::fixed | ios::showpoint);
    	showOrder(sales, id , productNames, NUM_PRODS);
    	showTotals(sales, units, NUM_PRODS);
    	return 0;
    }
    
    /*	Definition of calcSales.  Accepts units, prices, and sales arrays as arguments.
    	The sized of there arrays is passed into the num parameter. This function calculates
    	each product's sales by multiplying its units sold by each unit's price.  The result is
    	stored in the sales array.
    */
    
    void calcSales(int units[], double prices[], double sales[], int num)
    {
    	for ( int index = 0; index < num; index++)
    	{
    		sales[index] = units[index] * prices [index];
    	}
    }
    
    /* Definition of function dual Sort.  Accepts id an sales arrays as arguments.  The size
    	of these arrays is passed into elems.  This function performs a descending order selection sort
    	on identically as those of the sales array.  elems is the number of elements in each array.
    */
    
    void dualSort(int id[], double sales[], char productNames[][30], int elems)
    {
    	int startScan, maxIndex, tempid1;
    
    	string tempid2;
    	double maxValue;
    
    	for(startScan = 0; startScan < (elems -1); startScan++)
    	{
    		maxIndex = startScan;
    		maxValue = sales[startScan];
    		tempid1 = id[startScan];
    		tempid2 = productNames[startScan][30];
    		for(int index = startScan+1; index < elems; index ++)
    		{
    			if (sales[index] > maxValue)
    			{
    				maxValue = sales[index];
    				tempid1 = id[index];
    				tempid2 = productNames[index][30];
    				maxIndex = index;
    			}
    		}
    		
    		sales[maxIndex] = sales[startScan];
    		id[maxIndex] = id[startScan];
    		productNames[maxIndex][30] = productNames[startScan][30];
    		sales[startScan] = maxValue;
    		id[startScan] = tempid1;
    	
    	}
    }
    /*	Definition of showOrder function. Accepts sales and id arrays as arguments.  The size of these arrays is passed into num.
    	The function first displays a heading, then the sorted list of products numbers and sales.
    */
    void showOrder(double sales[], int id[], char productNames[][30],int num)
    {
    	cout << "Product Number\t  Product Name          Sales\n";
    	cout << "---------------------------------------------------\n";
    	for ( int index = 0; index < num; index++)
    	{
    		cout << id[index] <<"\t\t  "<< productNames[index]<< "  \t\t$";
    		cout <<setw(8) << sales[index] << endl;
    	}
    	cout << endl;
    }
    
    /*	Definition of showTotals function.  Accepts sales and id arrays as arguments.  
    	The size of these arrays is passed into num. The function first calculates 
    	the total units (of all products) sold and the total sales.  It then displays these ammounts.
    */
    void showTotals(double sales[], int units[], int num)
    {
    	int totalUnits = 0;
    	double totalSales  = 0.0;
    
    	for (int index = 0; index < num; index++)
    	{
    		totalUnits += units[index];
    		totalSales += sales[index];
    	}
    	cout << "Total units Sold: " << totalUnits << endl;
    	cout << "Total sales:	 $" << totalSales << endl;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    for(startScan = 0; startScan < (elems -1); startScan++)
    I'm going to guess that you want just plain elems there.

    Code:
    for(int index = startScan+1; index < elems; index ++)
    And I'm going to guess that you want startScan there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    That part is correct. I cannot get a name from the char array productNames and set it as a temporay variable. How is this done?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    I need to set a variable equal to the productNames array. For example tempid2[1]=productname[1]; But this does not work.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I need to set a variable equal to the productNames array. For example tempid2[1]=productname[1];
    Ahhh, the pleaures of using char arrays for strings. With C++ strings you could do that:
    Code:
    #include <string>
    using namespaces std;
    ..
    ..
    ..
    string str1 = "hello ";
    string str2 = str1;
    string str3 = "world";
    str2 = str2 + str3;
    Which leads to the question: why aren't you using C++ strings?

    For char array strings, you have to use: the strcpy() function in <cstring>.
    Last edited by 7stud; 11-19-2005 at 10:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  3. Parallel Arrays
    By kippwinger in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2003, 03:18 PM
  4. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 11:53 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21