1. In this lab, you will use 5 different programming languages to solve the same
ID: 3820508 • Letter: 1
Question
1. In this lab, you will use 5 different programming languages to solve the same problem: Write and use a generic sort function. This function should be able to sort values of any type that has some notion of order. The values to sort could be integers, floating point numbers, strings, pairs of values (say a string and a number), etc.
2. Write a console application in each of these 5 languages: C, C++, C#, Python and Haskell.
3. Each application has to use the following data:
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
Use appropriate data structures to represent the data above in each of the 5 languages and define the variables numbers and people, respectively.
4. Write one generic sort function in each of the 5 languages. Hints:
(i) The objective of this assignment is to understand generics (not sorting). You can use the sort functions from Lab Assignment 1 or just use a sort function provided in some standard library for the respective language.
(ii) C doesn’t provide any generics. However, a void*can be used to point to any value.
(iii) One way to specify an order on a type is to define a comparison function that compares two values. This comparison function could be an argument to your sort function. Some languages might provide predefined comparison functions.
(iv) Try to use everything we learned about these different programing languages, e.g., Python uses duck-typing, Haskell uses the type-class Ord to express order on a type, LINQ in C# includes the orderby operator, etc.
5. Use your generic sort function to
(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).
6. 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.
7. Write a main function in each of the 5 languages that will print out the results to the console.
Explanation / Answer
Answer:
Note: Within the provided time, I completed the task in C alone.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void myGenericSortFun(void *vv[], int n, int (*compare)(void *myD1, void *myD2))
{
int kk, aa;
for (kk = 0; kk < n; kk++)
{
for (aa = kk+1; aa < n; aa++)
{
if ((*compare)(vv[kk], vv[aa]) > 0)
{
void *echg = vv[kk];
vv[kk] = vv[aa];
vv[aa] = echg;
}
}
}
}
int compIntegers(void *myD1, void *myD2)
{
int *myIp1 = (int *) myD1;
int *myIp2 = (int *) myD2;
return (*myIp1 - *myIp2);
}
int CompareFloats(void *myD1, void *myD2)
{
float *myIp1 = (float *) myD1;
float *myIp2 = (float *) myD2;
if(*myIp1>*myIp2)
return 1;
return -1;
}
int main(void)
{
char* name[17] = {"Hal", "Susann", "Dwight", "Kassandra","Lawrence","Cindy","Cory","Mac","Romana","Doretha","Danna","Zara","Rosalyn","Risa","Benny","Juan","Natalie"};
int i;
int* age[17];
age[0]=(int *) malloc(sizeof(int));
*(age[0])=20;
age[1]=(int *) malloc(sizeof(int));
*(age[1])=31;
age[2]=(int *) malloc(sizeof(int));
*(age[2])=19;
age[3]=(int *) malloc(sizeof(int));
*(age[3])=21;
age[4]=(int *) malloc(sizeof(int));
*(age[4])=25;
age[5]=(int *) malloc(sizeof(int));
*(age[5])=22;
age[6]=(int *) malloc(sizeof(int));
*(age[6])=27;
age[7]=(int *) malloc(sizeof(int));
*(age[7])=19;
age[8]=(int *) malloc(sizeof(int));
*(age[8])=27;
age[9]=(int *) malloc(sizeof(int));
*(age[9])=32;
age[10]=(int *) malloc(sizeof(int));
*(age[10])=20;
age[11]=(int *) malloc(sizeof(int));
*(age[11])=23;
age[12]=(int *) malloc(sizeof(int));
*(age[12])=26;
age[13]=(int *) malloc(sizeof(int));
*(age[13])=24;
age[14]=(int *) malloc(sizeof(int));
*(age[14])=28;
age[15]=(int *) malloc(sizeof(int));
*(age[15])=33;
age[16]=(int *) malloc(sizeof(int));
*(age[16])=25;
float* numm[12];
numm[0]=(float *) malloc(sizeof(float));
*(numm[0])=645.32;
numm[1]=(float *) malloc(sizeof(float));
*(numm[1])=37.40;
numm[2]=(float *) malloc(sizeof(float));
*(numm[2])=76.30;
numm[3]=(float *) malloc(sizeof(float));
*(numm[3])=5.40;
numm[4]=(float *) malloc(sizeof(float));
*(numm[4])=-34.23;
numm[5]=(float *) malloc(sizeof(float));
*(numm[5])=1.11;
numm[6]=(float *) malloc(sizeof(float));
*(numm[6])=-34.94;
numm[7]=(float *) malloc(sizeof(float));
*(numm[7])=23.37;
numm[8]=(float *) malloc(sizeof(float));
*(numm[8])=635.46;
numm[9]=(float *) malloc(sizeof(float));
*(numm[9])=-876.22;
numm[10]=(float *) malloc(sizeof(float));
*(numm[10])=467.73;
numm[11]=(float *) malloc(sizeof(float));
*(numm[11])=62.26;
//Sorting the float
printf(" Before sorting the floats: ");
for (i = 0; i < 12; i++)
printf("%f ", *(numm[i]));
printf(" ");
myGenericSortFun((void **) numm, 12, (int (*)(void*,void*)) CompareFloats);
printf("After sorting the floats: ");
for (i = 0; i < 12; i++)
printf("%f ", *(numm[i]));
//Sorting the name
printf(" Before sorting the names: ");
for (i = 0; i < 17; i++)
printf("%s ", (name[i]));
printf(" ");
myGenericSortFun((void **) name, 17, (int (*)(void*,void*)) strcmp);
printf("After sorting the names: ");
for (i = 0; i < 17; i++)
printf("%s ", (name[i]));
//Sorting the age
printf(" Before sorting the age: ");
for (i = 0; i < 17; i++)
printf("%d ", *(age[i]));
printf(" ");
myGenericSortFun((void **) age, 17, (int (*)(void*,void*)) compIntegers);
printf("After sorting the age: ");
for (i = 0; i < 17; i++)
printf("%d ", *(age[i]));
getchar();
getchar();
}
Sample output:
Before sorting the floats:
645.320007 37.400002 76.300003 5.400000 -34.230000 1.110000 -34.939999 23.370001
635.460022 -876.219971 467.730011 62.259998
After sorting the floats:
-876.219971 -34.939999 -34.230000 1.110000 5.400000 23.370001 37.400002 62.25999
8 76.300003 467.730011 635.460022 645.320007
Before sorting the names:
Hal Susann Dwight Kassandra Lawrence Cindy Cory Mac Romana Doretha Danna Zara Ro
salyn Risa Benny Juan Natalie
After sorting the names:
Benny Cindy Cory Danna Doretha Dwight Hal Juan Kassandra Lawrence Mac Natalie Ri
sa Romana Rosalyn Susann Zara
Before sorting the age:
20 31 19 21 25 22 27 19 27 32 20 23 26 24 28 33 25
After sorting the age:
19 19 20 20 21 22 23 24 25 25 26 27 27 28 31 32 33
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.