How do you search & sort an array?

This is a discussion on How do you search & sort an array? within the C Programming forums, part of the General Programming Boards category; ....

  1. #16
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:27 AM.

  2. #17
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:28 AM.

  3. #18
    Registered User Camilo's Avatar
    Join Date
    Oct 2001
    Posts
    78
    it is more easy using qsort() from stdlib.h!!!
    no, life is nice, just a girl fooling around, alcohol fixes everything.
    OH, I now have a High School Diploma and need of ron (drink)

  4. #19
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:28 AM.

  5. #20
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Re: I noticed a lot of hardcode.

    The hard coded data is data that will never be changed and it is used to do the calculations.

    This program prints a report with the data from cust.dat which
    contains the customer information and it gets the data from order.dat and compares the CustomerId and orderId to make sure that the figures match up with the right customer.

    Your code is way over my head. I have only learned the basics of "C". You are good at what you do. I'm impressed. I was going through my code this morning and I found the error that you caught plus an error in each scan statement. The CustomerEmailAddress "%25c" is missing in on the customer side and the orderId "%9c is on the order side.

    When I compiled it, It printed out the report with CustomerId,
    orderId, did not print first, middle, lastName, CustomerAddress,
    CityStateZip, CustomerPhoneNo or CustomerEmailAddress.

    It printed all of the data from order.dat.

    I don't understand why it did not print the rest of the cust.dat...
    That's what I am trying to fix at the moment.
    Imagination at Work

  6. #21
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:28 AM.

  7. #22
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    I didn't read too much of the post, but see if you can understand the following..probably the easiest way to sort an array...

    Code:
    #include <stdio.h>
    
    #define SRT_ASCEND 0
    #define SRT_DESCEND 1
    
    void sorter(int *tsort, int len, int attr)
    {
    	int x, y, tmp;
    	
    	if(attr == SRT_DESCEND)
    	{
    		for(x=0; x<len; x++)
    			for(y=0; y<len; y++)
    				if(tsort[x] > tsort[y])
    				{
    					tmp = tsort[x];
    					tsort[x] = tsort[y];
    					tsort[y] = tmp;
    				}
    	}
    	if(attr == SRT_ASCEND)
    	{
    		for(x=0; x<len; x++)
    			for(y=0; y<len; y++)
    				if(tsort[x] < tsort[y])
    				{
    					tmp = tsort[x];
    					tsort[x] = tsort[y];
    					tsort[y] = tmp;
    				}
    	}
    }
    
    
    int main(void)
    {
    	int x;
    	int tsort[] = {3, 5, 1, 2, 4};
    
    
    	sorter(tsort, 5, SRT_ASCEND);
    
    	for(x=0; x<5; x++)
    		printf("%i",tsort[x]);
    
    
    
    	sorter(tsort, 5, SRT_DESCEND);
    
    	for(x=0; x<5; x++)
    		printf("%i",tsort[x]);
    
    	return 0;
    }

  8. #23
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:29 AM.

  9. #24
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    bubble sort is a slow sorting algorithm and should not be used for anything more serious than an assignment on bubble sort .

  10. #25
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Also tell me about some of these calcuations

    The whole program is based on a company that sells carpet and installs it.


    The first number 10 is the length
    The second number 10 or 20 is the width

    length and width are read from the data file (not hard coded) and
    calculated to get the area ( area = (length * width)

    CarpetCharge is read in from the data file and multiplied by area
    to get the CarpetTotal ( CarpetTotal = (CarpetCharge * area).

    Labor is fixed so it is hard coded
    laborTotal = ( .35 * 2000)

    installedPrice is CarpetTotal + LaborTotal

    discount is read from the file (0.20)
    discountTotal = (installedPrice * discount)

    subTotal = installedTotal - discountTotal

    tax is fixed at 8.5% or .085
    tax = TAX * subTotal

    total = subTotal + tax

    Code:
    
    This is what the original report looks like: I think that it will help you to understand what I am speaking of.
    
    The reports that I have to do:
    1 will look like this
    1 sorted by CustomerName so I take it as first middle and last.
    1 by sorted by total
    1 history sorted by CustomerName
    1 history sorted by total
    and so on
    After I figure all of this out, I have to make menus.
    I hope this makes more since now.
    ------------------------------------------------------------------------------------
                                     Header Here
    -------------------------------------------------------------------------------------
    Customer Id: 123456789
    Order Id: 123456789
    
    First Name: James   //this is not being filled
    Middle Name:  William  //this is not being filled
    Last Name:  Madison  //this is not being filled
    
    Address:  123 Maddison AVE //this is not being filled
                    Maddison GA 30000 //this is not being filled
    
    Phone No: (333) 123-1234 //this is not being filled
    Email Address: maddison@abc.com //this is not being filled
    ------------------------------------------------------------------------------------
    
                                      MEASUREMENT
    
                          Length                          10ft
                          Width                            20ft
                          Area                            200ft
    
                                           CHARGES
    
            DESCRIPTION           COST/SQ.FT.         CHARGE/ROOM
    
           ____________         __________         ______________
    
               Carpet                       $10.00                  $2000.00
    
               Labor                               .35                      $70.00
    
                                                                        ________________
    
              Installed Price                                         $2070.00
    
              Discount                         0.20%                   414.00
    
                                                                        ________________
    
              Subtotal                                                   $1656.00
    
               Tax                                                            $140.76
    
               Total                                                        $1796.76
    Imagination at Work

  11. #26
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:29 AM.

  12. #27
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Bubble sort is very easy to understand ,quick sort is only a bit more difficult to understand , and coding the recursive version of it is quite simple

    for eg,
    Code:
    #include <stdio.h>
    void swap(int *a,int *b)
    {
         int temp;   
         temp=*a;
         *a=*b;
         *b=temp;
    }
    
    void qs(int *a,int left,int right)
    {
         int i,pl=left,pv;
         if(left>=right) return;     
         swap(&a[left],&a[(left+right)/2]);
         pv=a[left];
         for(i= left+1;i<=right;i++)  if(a[i]<pv) swap(&a[++pl],&a[i]);
         swap(&a[pl],&a[left]);
          qs(a,left,pl-1);
          qs(a,pl+1,right);     
     }
    
    int main(void)
    {
         int a[]={3,1,4,5,2,7},i;
         printf("Before\n");
         for(i=0;i<(sizeof(a)/sizeof(*a));i++) printf("a[%d]=%d\n",i,a[i]); 
         qs(a,0,(sizeof(a)/sizeof(*a))-1);
         printf("After\n");
         for(i=0;i<(sizeof(a)/sizeof(*a));i++) printf("a[%d]=%d\n",i,a[i]);
          return 0;
    }

  13. #28
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:29 AM.

  14. #29
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Nowhere the original poser say that he had to sort his structures by bubble sort .
    Extending the above method for array of structures is trivial.
    I refuse to take the flame bait.
    Go get a life .

  15. #30
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 09:29 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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