4. Now consider a set of stock records. A stock record consists of a stock code which is a null-terminated string of three uppercase letters (e.g., “APL”), a price which is a positive real number (e.g., 22.0), and a predicted profit which is also a positive real number (e.g., 148.9)

4.1 [4 marks] Write the definition of a struct type named stock_t to represent a stock record as described above.

4.2 [5 marks] Write a function int stock_cmp(void *stock1, void *stock2)

that compares two stock_t typed values pointed to by stock1 and stock2. The function should return:

• -1, if the stock record pointed to by stock1 has an alphabetically smaller stock code than that of the stock record pointed to by stock2;

• 1, if the stock record pointed to by stock1 has an alphabetically larger stock code than that of the stock record pointed to by stock2;

• 0, if the two stock records pointed to by stock1 and stock2 have the same stock code.

For example, if stock1 points to a record {“APL”, 22.0, 148.9} and stock2 points to a record {“GGL”, 85.3, 950.2}, the function should return -1 since “APL”



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *