logo

CuriousTab

CuriousTab

Discussion


Home Technical Questions Programming See What Others Are Saying!
  • Question
  • How would you use qsort() function to sort an array of structures?


  • Correct Answer
  • #include "stringh" #include "stdlibh" struct stud { int rollno; int marks; char name[30]; }; int sort_m (struct stud *, struct stud *); int sort_name (struct stud *, struct stud *); int sort_marks (struct stud *, struct stud *); main() { static struct stud ss[] = { { 15, 96, "Akshay" }, { 2, 97, "Madhuri" }, { 8, 85, "Aishvarya" }, { 10, 80, "Sushmita" } }; int x,w; clrscr(); w = sizeof (struct stud); printf ('\nIn order of roll numbers:"); qsort (ss, 4, w, sort_rn); for(x=0; x<4;x++) printf ("\n%d%s%d", ss[x]rollno, ss[x]name,ss[x]marks); printf("\n\nIn order of names:"); qsort(ss, 4, sort_name); for (x=0; x<4;x++) printf("\n%d%s%d",ss[x]rollno, ss[x]name,ss[x]marks); printf("\n\nIn order of marks:"); qsort(ss,4,w,sort_marks); for (x=0;x<4;x++) printf ("\n%d%s%d",ss[x]rollno,ss[x]name,ss[x]marks); } int sort_rn (struct stud *t1, struct stud *t2) { return (t1->rollno-t2->rollno); } int sort_name (struct stud *t1, struct stud *t2) { return (strcmp(t1->name,t2->name)); } int sort_marks (struct stud *t1, struct stud *t2) { return (t2->marks-t1->marks); } 


  • More questions

    • 1. Which is the Stack used in 8085?
    • Discuss
    • 2. What does GUI stand for?
    • Discuss
    • 3. What is the main reason the OSI Model was created?
    • Discuss
    • 4. Ethernet uses which topology

    • Options
    • A. Star
    • B. Bus
    • C. Twisted pair
    • D. Both B & C
    • Discuss
    • 5. What are various scheduling queues?
    • Discuss
    • 6. What is the state of the processor, when a process is waiting for some event to occur?
    • Discuss
    • 7. What is Memory-Management Unit (MMU)?
    • Discuss
    • 8. SNOBOL is mainly used for

    • Options
    • A. Text Operation
    • B. String operations
    • C. List operations
    • D. Numerical operations
    • Discuss
    • 9. Convert the expression ((A + B) * C ? (D ? E) ^ (F + G)) to equivalent Prefix Notation
    • Discuss
    • 10. What would be the output of the following program? main() { char a[] = "Visual C++"; char *b = "Visual C++"; printf ("\n%d %d", sizeof (a), sizeof (b)); printf ("\n%d %d", sizeof (*a), sizeof (*b)); }
    • Discuss


    Comments

    There are no comments.

Enter a new Comment