Computer Science, asked by sam7191, 9 months ago

Write a program to input 50 names along with their weights in an array and sort the names in alphabetical order using selection sort technique.​

Answers

Answered by Anonymous
4

Answer:

#include <stdio.h>

struct Details

{  int weight;

   char name[50];

};

void enter(struct Details[], int);

void display(struct student[], int);

int main()

{

   struct Details data[20];

   int n, choice, weight;

 printf("Number of details to store: ");

   scanf("%d", &n);

   enter(data, n);

   do

   {

 printf("\nResult Menu :\n");

printf("Press 1 to display all records.\n");

       printf("Press 0 to exit\n");

       printf("\nEnter choice(0-1) : ");

       scanf("%d", &choice);

       switch (choice)

       {

           case 1:

               display(data, n);

         }

   }

   while (choice != 0);

return 0;

}

void enter(struct Details list[80], int s)

{  int i;

   for (i = 0; i < s; i++)

   {

       printf("\nEnter data for Record #%d", i + 1);

       printf("\nEnter weight: ");

       scanf("%d", &list[i].weight);

       fflush(stdin);

       printf("Enter name : ");

       gets(list[i].name);

}  

}

void display(struct Detailslist[80], int s)

{     int i;

   printf("\n\nweight\tName\n");

   for (i = 0; i < s; i++)

   {   selectionSort(arr, s);

       printf("%d\t%s\t%d\n", list[i].weight, list[i].name);

   }

}

void selectionSort(char arr[][MAX_LEN], int n)

{   int i, j, min_idx;  

   char minStr[MAX_LEN];  

   for (i = 0; i < s-1; i++)  

   {

       int min_idx = i;  

       strcpy(minStr, arr[i]);  

       for (j = i+1; j < s; j++)  

       {  

       

           if (strcmp(minStr, arr[j]) > 0)  

           {  

                 

               strcpy(minStr, arr[j]);  

               min_idx = j;  

           }  

       }  

   

       

       if (min_idx != i)  

       {  

           char temp[MAX_LEN];  

           strcpy(temp, arr[i]); //swap item[pos] and item[i]  

           strcpy(arr[i], arr[min_idx]);  

           strcpy(arr[min_idx], temp);  

       }  

   }  

}

Similar questions