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

1. (38 points) Create a directory named \"problem-1\". In that directory, write

ID: 3879611 • Letter: 1

Question

1. (38 points) Create a directory named "problem-1". In that directory, write a program that reads in, sorts, and outputs an array of structures (8 points) Declare the struct in its own header file with any support functions (eg., input, output, sort, etc.) and define the support functions in a corresponding C file The first and last name fields of the struct should be C strings and will never be more than twenty characters. The height should be type float. Use a typedef to name the struct o (8 points) Make the program take at least one command-line argument and at most two the first argument is the sort key: "last" for last name or "height" the second argument is the name of an optional file with data for the array If the optional second argument is provided, then use fopen to open the file for reading and get a FILE' to use with fscanf Output a helpful message if arguments are omitted or if too many are provided o (2 points) Fill the array using fscanf: the first input is an integer indicating the number of structs to follow. The structs follow with each field separated by whitespace. o (2 points) Use malloc and free to allocate the array of structures once the number of entries is known (3 points) Use the lib-C qsort routine to sort the input structures according the the key indicated on the command line. Use the strcmp method in the case of the "last" option (1 point) Output the sorted records to the console with a new line separating each record o (3 points) Check the return values for all functions and on error call exit with status 1 with an appropriate message output to stderr o (1 point) Use Input/Output redirection with the cat command to test the stdin input method o (2 point) Put the main function in a file on its own o (4 points) Create a Makefile to build your program (4 points) Create an "README" file that describes how to build/run your program and how you tested it including the command you used to test the stdin input

Explanation / Answer

OUTPUT:

------------

/*This is data file to fill array*/
MyLinxu|~/rnd/make$ cat data
3
Jon 2.34 Rich
Angelena 3.4 jolly
Tom 0.00143 Curse
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$ make
gcc -o program program.c -I.
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$ ./program height
Give file name also
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$ ./program height data 1
Max 2 argument supported!!
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$ ./program height data  
First argument height is given
numStruct data is 3
Tom 0.001430 Curse
Jon 2.340000 Rich
Angelena 3.400000 jolly
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$ ./program first data
First argument first is given
numStruct data is 3
Angelena 3.400000 jolly
Jon 2.340000 Rich
Tom 0.001430 Curse
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$
MyLinxu|~/rnd/make$ ./program last data
First argument last is given
numStruct data is 3
Tom 0.001430 Curse
Jon 2.340000 Rich
Angelena 3.400000 jolly
MyLinxu|~/rnd/make$

program.h:

------------------

typedef struct
{
char first[20];
float height;
char last[20];
}Data;

program.c

-----------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*Own Header*/
#include <program.h>

int heightSort (const void *a, const void *b)
{
Data *d1 = (Data *)a;
Data *d2 = (Data *)b;

return (int)(d1->height - d2->height);
}

int firstSort (const void *a, const void *b)
{

Data *d1 = (Data *)a;
Data *d2 = (Data *)b;

return strcmp (d1->first, d2->first);

}
int lastSort (const void *a, const void *b)
{

Data *d1 = (Data *)a;
Data *d2 = (Data *)b;

return strcmp (d1->last, d2->last);

}
int main(int argc, char **argv)
{

FILE *fp = NULL;
int numStruct = 0;

Data *data;

int i;

if (argc == 1)
{
printf("Give atleast one argument!! ");
return 0;
}
else if (argc == 2)
{
printf ("Give file name also ");
return 0;
}
else if (argc > 3)
{
printf ("Max 2 argument supported!! ");
return 0;
}

printf ("First argument %s is given ", argv[1]);

fp = fopen (argv[2], "r");

if (fp == NULL)
return 0;

fscanf (fp, "%d", &numStruct);

printf ("numStruct data is %d ", numStruct);

data = (Data *) malloc(sizeof (Data) * numStruct);

if (data == NULL)
{
fclose (fp);
return 0;
}

for (i=0; i<numStruct; i++)
{
fscanf (fp, "%s %f %s", data[i].first, &data[i].height, data[i].last);
}

if (strcmp (argv[1], "height") == 0)
{
qsort (data, numStruct, sizeof (Data), heightSort);
}
else if (strcmp (argv[1], "first") == 0)
{
qsort (data, numStruct, sizeof (Data), firstSort);
}
else if (strcmp (argv[1], "last") == 0)
{
qsort (data, numStruct, sizeof (Data), lastSort);
}

for (i=0; i<numStruct; i++)
{
printf ("%s %f %s ", data[i].first, data[i].height, data[i].last);
}

free (data);
fclose (fp);

return(0);
}

Makefile: NOTE: If make file return error then remove spaces in second line and insert a tab.

----------------

hellomake: program.c
gcc -o program program.c -I.