For more details, this is the link of the project (scroll down to sort) https://
ID: 3822576 • Letter: F
Question
For more details, this is the link of the project (scroll down to sort) https://d1b10bmlvqabco.cloudfront.net/attach/iyalez4k9ck4ta/iylrwr4eiw87n/j0lvde5h7gev/Project_4.html using std using Ecouti using st daimutiseti strings static const char usage Supported options: "LiRL ted clone of sort sort descending grmore case. Don't output duplicate tines. -help Show this essage and exit. n I struct igncaseCosp (const stringk s1, const stringl s20 boot operator return iti sortang in a case insensitive way! int sain (int arge, char "argvl1) aseso, unique int descending 0, igorec stat static struct option long epts ignore cass (0,0,0,0) T thufcpp 2 Sort cpp int main int aron char int descending static struct option help char int opt,index 01 switch (c) break ignore case break. unique 1 break print usage, argv101) return 0 case printf usage, argv10I)I return 0 rfun teng opts, iopt index)Explanation / Answer
Below is the workig code with proper comments into that. I believe it will help you out. Take help from this. Good Luck!
#include <stdio.h>
//Always use meaningful names for types
typedef unsigned char boolean;
#define True 't'
#define FALSE (!True)
//this is a really neat trick for swapping values efficiently
void swap(long* a,long *b) { *a=*a^*b;*b=*b^*a;*a=*a^*b; }
//Here's a readability improvement
#define until(condition) while(!(condition))
int main(int n, char*args[]){
double *d;
int i;
char input[5]; //should be long enough for most doubles.
boolean sorted = FALSE;
//In C, you need to specify the array size beforehand, so ask
printf("Please enter the length of the array ");
gets(input);
//scan the input string and convert to a value
sscanf(input,"%s",&input[0]);
n=(long)atol(input);
//allocate space, make sure you get the order of arguments right.
d = calloc(sizeof(double),n);
//Get and sort the array
until (sorted) {
for (i=0;i<n;i++) {
//It's important to always ask nicely
printf("Please enter the %d%s array item ",i,i==1?"st":"th");
scanf("%lf",d+i);
}
//do a compare and exchange sort:
sorted = !sorted; //not sorted
//check all the items
printf("%d %d ",i,n);
for (i=1;i<n;i++) {
//compare
if (d[i]<d[i-1]) {
//exchange
swap(d+i,d+i-1);
sorted = FALSE;
}
}
//show results
printf("The array is%ssorted ",sorted?" ":" not "); }
//use the --> "downto operator" for counting downto 0.
for (;n-->0;) printf("%lf ",*d++);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.