# How do you search & sort an array?

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 11-02-2001
Troll_King
.
• 11-02-2001
Troll_King
.
• 11-02-2001
Camilo
it is more easy using qsort() from stdlib.h!!!
• 11-02-2001
Troll_King
.
• 11-02-2001
sketchit
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,

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.
• 11-02-2001
Troll_King
.
• 11-02-2001
-KEN-
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; }```
• 11-02-2001
Troll_King
.
• 11-02-2001
pinko_liberal
bubble sort is a slow sorting algorithm and should not be used for anything more serious than an assignment on bubble sort .
• 11-02-2001
sketchit
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```
• 11-02-2001
Troll_King
.
• 11-03-2001
pinko_liberal
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; }```
• 11-03-2001
Troll_King
.
• 11-03-2001
pinko_liberal
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 .
• 11-03-2001
Troll_King
.
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last