Page 1 of 1

A programming question about cobol table

PostPosted: Mon Apr 11, 2011 6:44 pm
by Pumpkin
hi, i has a problem about the table programming,for example:
01 price-table.
05 price-group occurs 500 times
indexed by price-table-index.
10 item-number pic 999.
10 item-price pic s9999.

assumed this table has already loaded values,item-number not in sequence, i want to locate 10 highest item-number entries, load them to another table,
how to do this? thanks a lot!

Re: A programming question about cobol table

PostPosted: Mon Apr 11, 2011 8:03 pm
by Robert Sample
Use the first 10 values in the table as your initial high values (store the item number and the index number for each). Sort these into ascending (or descending) order using the sort method of your choice. Run through the table elements 11 through the end of the table, checking each item number to be less than the 10th highest value you've already got. If the table entry is less, ignore it and continue. Otherwise, find the right spot in the top 10 list, insert the item number and index value, then continue. When you've gone through the entire table you have the top 10 item numbers and their location.

Re: A programming question about cobol table

PostPosted: Mon Apr 11, 2011 8:07 pm
by BillyBoyo
Hi Pumpkin,

Firstly, data definitions. Nothing directly to do with your problem, but here I go.

If you are going to do a calculation with something, make it numeric, ideally comp-3 and an odd-number of digits. If you have a number which you are not going to do a calculation with, so it is something that just happens to be numbers instead of letters, make it PIC X. So item number, PIC XXX. Item price, COMP-3 PIC S9(5). If you use a subscript/loop counter, make it COMP PIC S9(4).

You have a price with no decimal places, but I'm assuming that is for your local currency.

Now your problem. If you are able to sort your data, then it is easy (sort descending, first ten entries/records are the ones you want).

If you want to do the sort yourself, I'm sure you'll find some outlines for things like Bubblesort, Quicksort and whatever is up-to-date.

As you are going to have two tables it would be possible to do an exchange-as-you-search sort of thing. Let us know if your next step requires more assistance, but think about it yourself first as much as possible, and let us know how you arrived at getting stuck (or at your solution).

Or, by the time I finished writing this, just review Robert's solution. I'd just ditch the sorting bit and do it as you go along, but this is a fine solution if you want practice putting together your own sort (which you will find future uses for, so it is not time lost).

Re: A programming question about cobol table

PostPosted: Tue Apr 12, 2011 10:36 am
by Pumpkin
thanks
actually, i stuck in how to sort, i know how to use Sort statement to sort a input sequential file in the cobol, but how to sort the table elements in the table.can you give some samples? thanks

Re: A programming question about cobol table

PostPosted: Tue Apr 12, 2011 12:07 pm
by BillyBoyo
Hi Pumpkin,

Come on, you can't just be "stuck on the sort". I'm sure if you do some research there'd be tons of stuff out there.

Maybe try an exchange-sort.

Initialise a swap flag. Look at each table entry and the one following. If the one following is greater, swap them, set a flag to say a swap was made and continue. Make sure you stay inside your table (ie for the last entry there is nothing to compare it to, so your "sweep" has finished). When the sweep has finished, look at your flag. If there are no swaps, the table is sorted. If there are swaps, start at the top again, remembering to initialise your flag.

This is the simplest to code but about the worst you can get at execution times, because it can take a "long" time for out of sequence data towards the extremes of the table to work its way up to the correct position one direct exchange at a time.

Re: A programming question about cobol table

PostPosted: Tue Apr 12, 2011 3:54 pm
by Robert Sample
Pumpkin, you need to learn to do your own research -- students start becoming professionals when they understand how to find out things on their own. Google terms like "sort algorithm" or "bubble sort" or "quicksort" and you can start learning on your own.

Re: A programming question about cobol table

PostPosted: Tue Apr 12, 2011 5:11 pm
by enrico-sorichetti
and here is a link to some live demos of different sorting algorithms
http://www.pdviz.com/different-sorting- ... rated-with