A programming question about cobol table

Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

A programming question about cobol table

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!
-------------------
Pumpkin
Pumpkin

Posts: 55
Joined: Sat Mar 05, 2011 9:12 am
Has thanked: 0 time
Been thanked: 0 time

Re: A programming question about cobol table

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.
Robert Sample
Global moderator

Posts: 3652
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 269 times

Re: A programming question about cobol table

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).
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: A programming question about cobol table

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
-------------------
Pumpkin
Pumpkin

Posts: 55
Joined: Sat Mar 05, 2011 9:12 am
Has thanked: 0 time
Been thanked: 0 time

Re: A programming question about cobol table

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.
BillyBoyo
Global moderator

Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: A programming question about cobol table

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.
Robert Sample
Global moderator

Posts: 3652
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 269 times

Re: A programming question about cobol table

and here is a link to some live demos of different sorting algorithms
http://www.pdviz.com/different-sorting- ... rated-with
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator

Posts: 2927
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 154 times