Question: (Sorting Integers) Write a program that sorts an array of integers into ascending or descending order. Use command-line arguments to pass either argument -a for
(Sorting Integers) Write a program that sorts an array of integers into ascending or descending order. Use command-line arguments to pass either argument -a for ascending order or -d for descending order. [Note: This is the standard format for passing options to a program in UNIX.]
I am looking for help making my below code work not getting a whole new code. If you can not help make mine work do not answer! This is the 4th time I am posting the same thing.
#include
#include
#include
//initialize main
int main(int argc, char *argv[])
{
//define variables
int numArray[10];
int loopA, loopB;
int tempVar;
//to generate 10 random numbers in the array
for (loopA = 0; loopA < 10; loopA++)
{
//generate random values between 1 to 100
//store numbers in variable numArray
numArray[loopA] = 1 + rand() % 100;
}
//sort array based on entry
// if argument is -a or -A sort ascending
if ((strcmp(argv[2], "-a") == 0) ||
(strcmp(argv[2], "-A") == 0))
{
//loop for sorting array
for (loopA = 0; loopA <= 8; loopA++)
{
//sort array from 0 to 8
for (loopB = 0; loopB <= 8 - loopA; loopB++)
{
// compare the values stored in numArray
if (numArray[loopB] > numArray[loopB + 1])
{
//numArray assigned to tempVar
tempVar = numArray[loopB];
numArray[loopB] = numArray[loopB + 1];
//tempVar assigned to numArray
numArray[loopB + 1] = tempVar;
}
}
}
}
//if argument is -d or -D
else if ((strcmp(argv[2], "-d") == 0) ||
(strcmp(argv[2], "-D") == 0))
{
// loop for sorting array
for (loopA = 0; loopA <= 8; loopA++)
{
//sort array from 0 to 8
for (loopB = 0; loopB <= 8 - loopA; loopB++)
{
// compare values stored in numArray
if (numArray[loopB] < numArray[loopB + 1])
{
//numArray is assigend to tempVar
tempVar = numArray[loopB];
numArray[loopB] = numArray[loopB + 1];
//tempVar is assigned to numArray
numArray[loopB + 1] = tempVar;
}
}
}
}
//print stored array
printf(" The sorted array is: ");
//loop to print every element of array
for (loopA = 0; loopA < 10; loopA++)
{
//Display output
printf("%d\t", numArray[loopA]);
}
}
This is what I have, but I am unsure of the other headers that need to be included can someone help.
in c please
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
