logo

CuriousTab

CuriousTab

Discussion


Home Technical Questions Programming Comments

  • 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); } 


  • Programming problems


    Search Results


    • 1. If I use the following printf() to print a long int why I am not warned about the type mismatch? printf ("%d",num );
    • Discuss
    • 2. What is the difference between malloc() and calloc() functions?
    • Discuss
    • 3. Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?
    • Discuss
    • 4. How would you free the memory allocated by the following program? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p, i; p = (int **) malloc (MAXROW * sizeof (int *)); for ( i = 0; i < MAXROW ; i++) p[i] = (int *) malloc (MAXCOL * sizeof (int )); }
    • Discuss
    • 5. How would you dynamically allocate a 2-D array of integers?
    • Discuss
    • 6. What will be output of following c code? #include int main() { int i; for(i=10;i<=15;i++){ while(i){ do{ printf("%d ",1); if(i>1) continue; }while(0); break; } } return 0; }
    • Discuss
    • 7. What will be output of following c code? void main() { struct bitfield { unsigned a:5; unsigned c:5; unsigned b:6; }bit; char *p; struct bitfield *ptr,bit1={1,3,3}; p=&bit1; p++; clrscr(); printf("%d",*p); getch(); }
    • Discuss
    • 8. What will be output when you will execute following c code? #include enum actor { SeanPenn=5, AlPacino=-2, GaryOldman, EdNorton }; void main() { enum actor a=0; switch(a) { case SeanPenn: printf("Kevin Spacey"); break; case AlPacino: printf("Paul Giamatti"); break; case GaryOldman:printf("Donald Shuterland"); break; case EdNorton: printf("Johnny Depp"); } }

    • Options
    • A. Kevin Spacey
    • B. Paul Giamatti
    • C. Donald Shuterland
    • D. Johnny Depp
    • Discuss
    • 9. Write a c program to create dos command type.
    • Discuss
    • 10. Why we use do-while loop in c?
    • Discuss


    Comments

    There are no comments.

Enter a new Comment