Thread: Need a quick review - Welcome comments

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Question Need a quick review - Welcome comments

    Is there an easier way to do this????


    //Tamra Hinton, Chapter 11, Problem 12
    //Declare and initilize the array.
    //Pass the array and the number of elements in an array
    //Call the bubble sort routine and modify for a two dimensional array
    //Display part information with a nestled for loop

    Code:
    #include <iostream.h>
    
    void sort (int parts[100][2],int size);
    
    
    int main() 
        {
    	int parts[100][2];
    	int partsQuantity;
    	int i;
    	partsQuantity =0;
    	parts[partsQuantity][0] = 1001;
    	parts[partsQuantity++][1] = 62;
    	parts[partsQuantity][0] = 949;
    	parts[partsQuantity++][1] = 85;
    	parts[partsQuantity][0] = 1050;
    	parts[partsQuantity++][1] = 33;
    	parts[partsQuantity][0] = 867;
    	parts[partsQuantity++][1] = 125;
    	parts[partsQuantity][0] = 346;
    	parts[partsQuantity++][1] = 59;
    	parts[partsQuantity][0] = 1025;
    	parts[partsQuantity++][1] = 105;
    	
    
    	sort(parts,partsQuantity);	
    	
    	cout << "Part No.\t\tQuantity\n\n";
    	for (i = 0; i < partsQuantity; i++)
    	 cout << parts[i][0] << "\t\t" << parts[i][1];
        cout << endl;
    	
    	
    return 0;
    }
    			
    void sort (int parts[100][2],int size) 
    {
    	int t[2];
    	int i,j;
    
    	for (i = 0; i < size-1; i++ ) 
        { 
    		for ( j = 1; j < size-i; j++ ) 
                {
    			if ( parts[j-1][1] < parts[j][1] ) 
                   {  
    				t[0] = parts[j-1][0]; 
    				t[1] = parts[j-1][1]; 
    				parts[j-1][0] = parts[j][0]; 
    				parts[j-1][1] = parts[j][1]; 
    				parts[j][0] = t[0]; 
    				parts[j][1] = t[1]; 
    				} 
    			}
    	}
    return;
    }
    __________________________________________________ __i

    How would you modify this program? Would I be able to use something like this....
    int main()

    {

    const int part = 100;
    int [part ] = [1001, 949, 1050, 867, 346, 1025];
    const int quantity = 2;
    int [quantity] = [62, 85, 33, 125, 59, 105]


    Please use [code][/code]Tags

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    70

    ----------------------------------------------------------------------------

    const int part = 100;
    int [part ] = [1001, 949, 1050, 867, 346, 1025];
    const int quantity = 2;
    int [quantity] = [62, 85, 33, 125, 59, 105]

    ----------------------------------------------------------------------------


    Above mentioned code will not work definitely.

    You don't have declared variable name in 2nd and 4th line
    Chintan R Naik

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    For the first part, if you can use different numbers:

    Code:
    for (x = 0; x < 100; x++)
    {
          for (y = 0; y < 2; y++)
         {
              parts[x][y] = ...
         }
    }
    The last part will work, you just need curley braces and have a name, like so:

    Code:
    int name[part] = {1001, 949, 1050, 867, 346, 1025};
    Last edited by funkydude9; 07-02-2003 at 11:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing comments of type '//' and '/*'
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2007, 02:24 AM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  4. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  5. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 05:18 PM