Question
Write a C console application that determines whether a data stream is sorted in ascending order. A data stream can be either keyboard or a
Write a C console application that determines whether a data stream is sorted in ascending order. A data stream can be either keyboard or a file. The interface for the program is a command shell's command line.
Requirements
The program must handle any length of list so your program must work in a 64-bit build. The list will be input from the console or read from a file. This is terminated with end-of-stream ^Z on windows.
The output must indicate if the data sorted by printing the single word 'sorted' on a single line, then return the integer value 101 from the main. if the stream is not sorted, you are to print "element # (Value) not sorted where # is the offset of the first out-of-order element and value is the value of the unsorted element. In the unsorted case, main must return the integer 100.
Failure to complete the test (bad input, filename, switch) for any reason and the main should return; EXIT_FAILURE
If help was selected, main should return EXIT_SUCCESS.
The command line arguments below.
That is what it should look like if the user enters -help or --help.
How do I sort the real numbers, lines, and all the other stuff that was present in the command line arguments? here is the code.
#include
#include
int main(int argc, char *argv[]) {
int sorted = 1; // assume initially sorted
int prev = 0; // previous element in stream
int curr; // current element in stream
int count = 0; // count of elements processed
FILE *fp = NULL; // file pointer for input file
// check for help option
if (argc == 2 && strcmp(argv[1], "help") == 0) {
printf("Usage: %s [filename] ", argv[0]);
printf("If filename is not provided, input is read from console. ");
printf("Input should be a list of integers separated by spaces or newlines. ");
printf("The program will check if the input is sorted in ascending order. ");
printf("If the input is sorted, the program will print 'sorted' and return 101. ");
printf("If the input is not sorted, the program will print 'element # (value) not sorted' where # is the offset of the first out-of-order element and value is the value of the unsorted element. ");
printf("In this case, the program will return 100. ");
return EXIT_SUCCESS;
}
// check for input file
if (argc == 2) {
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Error: Unable to open file %s ", argv[1]);
return EXIT_FAILURE;
}
}
// read data stream from console or input file
while (1) {
if (fp == NULL) {
// read from console
scanf("%d", &curr);
if (feof(stdin)) {
break;
}
} else {
// read from file
fscanf(fp, "%d", &curr);
if (feof(fp)) {
break;
}
}
count++;
if (count > 1 && curr
printf("element %d (%d) not sorted ", count, curr);
sorted = 0;
}
prev = curr;
}
// print result and return status code
if (sorted) {
printf("sorted ");
return 101;
} else {
return 100;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started