Question

struct student { char name[10]; int rank; }; Using the student structure given above, create an...

struct student { char name[10]; int rank; };

Using the student structure given above, create an array of size 5 students..

Then write a complete C program to sort the students array based on the students rank. Use the following sorting techniques in your code. Don’t forget to print initial array and final (sorted) array. a. Selection sort b. Insertion sort c. Merge sort / Quick sort.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A) using selection sort

#include<stdio.h>
//main starts from here
int main()
{
    int i,min,j;
   
    //structure for student data
    struct student
    {
        char name[20];
        int rank;
    }s[5],temp;
    printf("\nEnter the student's information\n");
    //taking input for student information
    for(i =0;i<5;i++)
    {
        printf("Enter student name: ");
        scanf("%s",&s[i].name);
       
        printf("Enter student rank: ");
        scanf("%d",&s[i].rank);  
    }
    //selection sort
    for (i = 0; i < 4; i++)
    {
        // Find the minimum element in unsorted array
        min = i;
        for (j = i+1; j < 5; j++)
        if (s[j].rank < s[min].rank)
        min = j;
        // Swap the found minimum element with the first element
        temp = s[min];
        s[min] = s[i];
        s[i] = temp;
     }
   
    printf("\nDisplay the student's information\n");
    //Displays the student information
    for(i =0;i<5;i++)
    {    
        printf("\nName: %s",s[i].name);        
        printf("\nRank : %d",s[i].rank);
       
    }
    return 0;
}

INSERTION SORT :

#include<stdio.h>
//main starts from here
int main()
{
    int i,key,j;
   
    //structure for student data
    struct student
    {
        char name[20];
        int rank;
    }s[5],temp;
    printf("\nEnter the student's information\n");
    //taking input for student information
    for(i =0;i<5;i++)
    {
        printf("Enter student name: ");
        scanf("%s",&s[i].name);
       
        printf("Enter student rank: ");
        scanf("%d",&s[i].rank);  
    }
    //Insertion sort
    for (i = 1; i < 5; i++)
    {
        key = s[i].rank;
        j = i - 1;
        while (j >= 0 && s[j].rank > key)
        {
            s[j + 1].rank = s[j].rank;
            j = j - 1;
        }
        s[j + 1].rank = key;
    }

    printf("\nDisplay the student's information\n");
    //Displays the student information
    for(i =0;i<5;i++)
    {    
        printf("\nName: %s",s[i].name);        
        printf("\nRank : %d",s[i].rank);
       
    }
    return 0;
}

QUICK SORT:

#include<stdio.h>

struct student
    {
        char name[20];
        int rank;
    }s[5],temp;

void quick_sort(struct student s[],int first,int last)
{
    int i, j, pivot;
    if(first<last)
    {
        pivot=first;
        i=first;
        j=last;
        while(i<j)
        {
            while(s[i].rank <= s[pivot].rank &&i<last)
             i++;
            while(s[j].rank>s[pivot].rank)
             j--;
            if(i<j)
            {
                temp=s[i];
                s[i]=s[j];
                s[j]=temp;
             }
        }
        temp=s[pivot];
        s[pivot]=s[j];
        s[j]=temp;
        quick_sort(s,first,j-1);
        quick_sort(s,j+1,last);
     }
}
//main starts from here
int main()
{
    int i,key,j;
   
    //structure for student data
   
    printf("\nEnter the student's information\n");
    //taking input for student information
    for(i =0;i<5;i++)
    {
        printf("Enter student name: ");
        scanf("%s",&s[i].name);
       
        printf("Enter student rank: ");
        scanf("%d",&s[i].rank);  
    }
    quick_sort(s,0,4);
   

    printf("\nDisplay the student's information\n");
    //Displays the student information
    for(i =0;i<5;i++)
    {    
        printf("\nName: %s",s[i].name);        
        printf("\nRank : %d",s[i].rank);
       
    }
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
struct student { char name[10]; int rank; }; Using the student structure given above, create an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; };...

    Given the following program: #include <stdio.h> struct student { int id; char name[20]; char grade; }; void func(struct student stud); int main() { struct student astud; astud.id=9401; strcpy(astud.name, "Joe"); astud.grade = 'A'; func(astud); return 0; } Abdelghani Bellaachia, CSCI 1121 Page: 16 void func(struct student astud) { printf(" Id is: %d \n", astud.id); printf(" Name is: %s \n", astud.name); printf(" Grade is: %c \n", astud.grade); } Modify this program to include the address of a student as a separate structure....

  • For C++ Create a student structure that has fields for first name, last name, hometown, state,...

    For C++ Create a student structure that has fields for first name, last name, hometown, state, year in college and major. Keep the number of students to a reasonabe level. Place each structure into an array sorted by last name and within last name, by first name. Read the student information from a file and print the sorted array to the screen and then to an output file. I was using the code below to guide me but it keeps...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.

  • Using Java: Create an array with the size of 10 and assign student details (Name, ID,...

    Using Java: Create an array with the size of 10 and assign student details (Name, ID, age and TotalMarks) to the array. Perform the Heap Sort to sort the students in ascending order based on their total marks. Steps: • Create the student list (use Random class in java to generate the age (15- 25) and total (0-100)) • Print the Student List in a table format • Perform Heap sort based on the total marks of the students •...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

  • Canvas → XC D Question 13 Given the code below: typedef struct p { char topic...

    Canvas → XC D Question 13 Given the code below: typedef struct p { char topic [20]; int nunWords; > page; typedef struct mag char name[20]; page pages [250); int numPages; } magazine; magazine m; How would you declare an array of 20 magazines? struct magazine[20] magazine[20] magazine myStack(20) None of the above struct m[191 Question 14 Given the code below: typedef struct char topic[20] Canvas & XC struct m[19] Question 14 Given the code below: typedef struct char topic[20];...

  • Given the structure definition in a 32-bit environment: struct contact { char name[22] char gender; int...

    Given the structure definition in a 32-bit environment: struct contact { char name[22] char gender; int phone; X, What is the total number of bytes used for the object x, including the padding bytes? O A. 27 O B. 28 O C. 30 O D. 32

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT