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

(C) I need help for this question. Please advise what goes in each function and

ID: 3881935 • Letter: #

Question

(C) I need help for this question. Please advise what goes in each function and try to keep it as simple as possible, thank you.

4. Command Line Aguments (4 marks) a data without changing the It's often beneficial to write pr code every time. Imagine only being able to open a single file or visit a single website (although I'm sure it feels like you only use courselink anyways). A useful tool for doing this is command line arguments. Command line arguments are passed to a program when it is run after compiling. Arguments are commonly used to tell a program which flags to activate, or information about the files read/written to. Instruction: In this secion we are going to read command line arguments from the terminal, convert them all to integers then sort them from least to greatest.

Explanation / Answer

The code is

#include<stdio.h>
#include <stdlib.h>
#include <ctype.h>
void sortIntegers(int arr[],int length)
{
int i,j;
for(i=0;i<length;i++)
for(j=0;j<length-i-1;j++)
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
int convertToInt(char * s)
{
return atoi(s);
}
int* sortArguments(int argc ,char *argv[])
{
int *list=malloc (sizeof(int)*(argc-1));
int i;
for(i=1;i<argc;i++)
{
list[i-1]=convertToInt(argv[i]);
}
sortIntegers(list,argc-1);
return list;
}
int main(int argc,char *argv[]) {
int i=0;
int *result;
printf(" Before ");
for (i=1;i<argc;i++)
printf("%s, ",argv[i]);
printf(" ");
result =sortArguments(argc,argv);
printf("After ");
for (i=0;i<argc-1;i++)
printf("%d, ",result[i]);
printf(" ");
free(result);
return 0;
}

The output is

Before
1, 5, 6, 2, 4, 3, 4, 7,
After
1, 2, 3, 4, 4, 5, 6, 7,

Do give a thumbs up as it matters a lot and in case there are doubts leave a comment.