Solved

structure

Posted on 1997-03-24
5
273 Views
Last Modified: 2010-04-15
Write a program that uses the qsort() function to sort an array of structures of type PRODUCT.Each product record has the following fields :-
Product category(string)
Product code(int)
Description(string)
Unit Cost(float)
quantity(long int)
The array is to be sorted in alphabetical order according to the product category.In situation where there are similar product category, then the records involved should be sorted according to product code in ascending order.
0
Comment
Question by:jennifer032497
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:warren_dale
ID: 1249746
This is obviously a homework assignment. Which parts do you need help on?
0
 

Author Comment

by:jennifer032497
ID: 1249747
Below is what i have worked on. Please help me to debug.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 10

typedef struct
{
  char *category;
  int code;
  char *desc;
  float cost;
  long quantity;
}PRODUCT;

int str_cmp ();
int code_cmp ();

main ()
{
  PRODUCT record [SIZE];
  int x, num;
  clrscr();
  printf("Enter number of record [ max 10 ] : ");
  scanf("%d",&num);
  clrscr();
  printf("DATA ENTRY\n");
  printf("==========\n\n");
  for (x=0; x<num; x++)
  {
    printf("Product Category : ");
    scanf("%s",record[x].category);
    printf("\nProduct Code     : ");
    scanf("%d",&record[x].code);
    printf("\nDescription      : ");
    scanf("%s",record[x].desc);
    printf("\nUnit Cost        : ");
    scanf("%f",&record[x].cost);
    printf("\nQuantity         : ");
    scanf("%ld",&record[x].quantity);
    printf("\n\n=========================");
    printf("============================\n\n");
  }
  qsort(record,num,sizeof(PRODUCT),str_cmp);
  qsort(record,num,sizeof(PRODUCT),code_cmp);
  clrscr();
  gotoxy(20, 0);
  printf("SORTED RECORD\n");
  gotoxy(20, 1);
  printf("*************\n\n");
  printf("Category\tCode\tDescription\tCost\tQty\n");
  printf("=================================");
  printf("===================\n");
  for(x=0; x<num; x++)
  {
    printf("%s\t%d\t%s\t%f\t%d\n",record[x].category,
      record[x].code,record[x].desc,record[x].cost,
      record[x].quantity);
  }
  getch();
}

int str_cmp(PRODUCT *p1,PRODUCT *p2)
{
  return(strcmp(p1->category,p2->category));
}

int code_cmp(PRODUCT *n1,PRODUCT *n2)
{
  return(n1->code-n2->code);
}

0
 
LVL 4

Accepted Solution

by:
jos010697 earned 100 total points
ID: 1249748
It all boils down to the comparison function, used by the
qsort() routine. Here's an example:

int comp(const void* a, const void* b) {

const product* pa= a; /* use product* for convenience *.
const product* pb= b;

int r; /* comparison result */

r= strcmp(pa->category, pb->category);

if (r) /* categories are different */
   return r;

/* compare codes if categories compare equal */
if (pa->code > pb->code)
   return 1;

if (pa->code < pb->code)
   return -1;

return 0;

}

kind regards,

Jos aka jos@and.nl

0
 

Author Comment

by:jennifer032497
ID: 1249749
Thank you Jos for your help.But my main problem is when entering the records of the product. Please consider this point.
0
 
LVL 4

Expert Comment

by:jos010697
ID: 1249750
Ah, but that's not what you asked for (see your original
question above).

Hint: have a look at e.g. the category member: it's
a pointer to a character (string), but it doesn't point
to anything (same for the 'description' member).

kind regards,

Jos aka jos@and.nl
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question