Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I need a program in C that uses a generic sort function to do the followi

ID: 3815348 • Letter: H

Question

Hello, I need a program in C that uses a generic sort function to do the following:

(i) sort numbers ascending by numerical value,

(ii) sort people alphabetically (lexicographically) by name, and to

(iii) sort people descending by age, where people of the same age should be sorted alphabetically (lexicographically).

The point here is to use the same function to do all 3 different sort operations. Try to reuse as much of your code and focus on clarity and brevity.

Data to use to sort:

The sequence of floating point numbers: (645.32, 37.40, 76.30, 5.40, -34.23, 1.11, -34.94, 23.37, 635.46, -876.22, 467.73, 62.26)

The following sequence of people with name and age of each person. The name is a string and the age an integer: (Hal, 20; Susann, 31; Dwight 19; Kassandra, 21; Lawrence, 25; Cindy, 22; Cory, 27; Mac, 19; Romana, 27; Doretha, 32; Danna, 20; Zara, 23; Rosalyn, 26; Risa, 24; Benny, 28; Juan, 33; Natalie, 25)

Explanation / Answer

Hello ,

DESCRIPTION:-

The code is using a function sorting() which will be reused for all the three condition .

when option 1 will be passed it will perform sorting of floating point numbers

when option 2 will be passed it will sort the names

when option 3 will be passed it will sort people as per descending age of people and if same age it will sort the people alphabetially.

necessary comments are given in the code.

CODE

#include <iostream.h>
#include <string.h>
//declaring arrays of floating point numbers
double numbers[12]={645.32, 37.40, 76.30, 5.40, -34.23, 1.11, -34.94, 23.37, 635.46, -876.22, 467.73, 62.26};
//declaring arrays of names
const char *names[17]={"Hal","Susann","Dwight","Kassandra","Lawrence","Cindy","Cory","Mac","Romana","Doretha","Danna","Zara","Rosalyn","Risa","Benny","Juan","Natalie"};
//declaring arrays of age respective to names
int age[17]={20,31,19,21,25,22,27,19,27,32,20,23,26,24,28,33,25};
//declare sorting function which will perform operation as per the given option when called in main function
void sorting(int operation);
void sorting(int op)
{
//if option 1 is passed the floating numbers are sortedin ascending order using bubble sort
if (op==1)
{
int count = 12;
double=0.00;
printf("The sorted floating numbers are: ")
for(int a=0; a<count; a++)
for(int b=a+1; b<count; b++)
if(numbers[b] < numbers[a])
{
t = numbers[a];
numbers[a] = numbers[b];
numbers[b] = t;
  
}
for(int i=0; i<count; i++)
printf("%f ",numbers[i]);
}
//if option 2 is passed the names are orted in alphabetical order using strcmp
if(op==2)
{
char * temp[1];
for (int k = 0; k < 17; k++) {
for (l = k+1; l < 17; l++) {
if (strcmp(names[l], names[l + 1]) > 0) {
strcpy(temp[0], names[l]);
strcpy(names[l], names[l + 1]);
strcpy(names[l + 1], temp[0]);
}
}
}
printf("alphabetically sorted name are: ");
for (int k = 0; k < 17; k++)
puts(names[k]);
}
//if option 2 is passed the names are sorted descending by age, where names of the same age should be sorted alphabetically
if (op==3)
{
char *temp1[1];

int t=0;
for(int a=1; a<17; a++)
for(int b=a+1; b<17; b++)
if(age[a] <= age[b])
{
if(age[a]==age[b]) //if person are of same age
{
if (strcmp(names[a], names[b])>0)
{
strcpy(temp1[0], names[a]);
strcpy(names[a], names[b]);
strcpy(names[b], temp1[0]);
}
}
t = age[a];
age[a] = age[b];
age[b] = t;
strcpy(temp1[0], names[a]);
strcpy(names[a], names[b]);
strcpy(names[b], temp1[0]);
}
printf("Sorting people descending by age is ")
for(int i=0; i<17; i++)
printf("%d %s ",numbers[i],names[i]);
}
}
void main()
{
//sorting the floating numbers by passing option 1
sorting(1);
//sorting the names by passing option 2
sorting(2);
//sorting the people by descending age by passing option 3
sorting(3);
}

=======================================================================================

Please let me know if more information is required.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote