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

• Use command line arguments for input • Use malloc() and free()function. • Coll

ID: 3827091 • Letter: #

Question

• Use command line arguments for input

• Use malloc() and free()function.

• Collect input from a text file.

• Write output to a text file.

Input file: As mentioned above the input is stored in a text file. Below are first few lines from the text file

1001                          1591.63              M
1011                          1503.54              S
2002                          2000.43              M
3332                          2567.99              J
2112                          3112.67              M
……..
……..

there are 13 lines of input in the file and each line contains an account number, an amount for that account and a designation. To read input from the file use fscanf function. To begin read the, use the size or the number of accounts to run a loop and inside the loop read the inputs (one line at a time) from the file. Store the account number in an integer pointer, the amount in a double pointer and designation in a character pointers.

Output file:

You should create two files:

hw3BadOutput.txt which will have the contents of the input file. You will just write back the contents into this file. by giving the headings, look at the sample file on blackboard.

hw3GoodOutput.txt should contain the records of account numbers, corresponding amounts and designations which conform to the requirements specified by the CS department for 1050TAs. It should be grouped in the order of designation as shown in the sample file. All the ‘J’s must come first, followed by the ‘S’s and then come the ‘M’s.

Libraries to include:

Include stdlib.h library in the code to use following functions

malloc - To allocate space to pointers.

atoi - To convert string to integer number.

Include stdbool.h library in the code to handle “true” and “false” return values

Include stdio.h library in the code to handle standard I/O functions

Implement following functions for the homework assignment:

bool load_data(char*, int *, double *,char *, int ): This function takes the input file name, integer, double and character pointers and the size as integer. It opens the input file. If unable to open it, returns false. Otherwise load the account information from the text file into the integer, double and character arrays and return true at the end. The first char pointer is the string holding the name of the file to open, int * is the account ID array, double * is the amount array, the second char * is the designation array and the last int is the size or the number of records. Use fscanf or other library functions to read in the data.

void print_data(int *, double *, char *, int): This function takes integer array , double array, character array and the integer size and displays the data on the console stored in these arrays as shown in the sample output below. Use proper output formatting and heading to display the output. Remember this function prints the data on the screen.

int highest_amount(double *, int): This function takes the double amounts pointer and the number of accounts. It finds the highest amount and returns the index corresponding to the highest amount.

int lowest_amount(double *, int): Same as above function except it returns the index corresponding to the lowest amount.

float average_amount(double *, int): Same as above functions except it returns the average amount for all the accounts.

bool write_data(char* , int *, double *, char *, int): This function writes the account information (account IDs, amounts and designations) into a text file (hw3BadOutput.txt and hw3GoodOutput.txt). Following are the arguments passed to this function

char*- output file name.

int*- pointer containing account IDs information.

double*- pointer containing amount information.

char* - pointer containing the designation information.

int – denoting the number of records which need to be printed.

if unable to open the file in w mode return false else write the data and then return true at the end. Use fprintf or other library function to write the data into the text file.

This function is called twice, once to print raw data and another time to print the cleaned-up data.

int check_palindrome(int ): Takes an integer and checks if the integer is a palindrome, if it is then return 1 else return 0. A palindrome is a number which has the same value when reversed. Example:

5005, 1111, 1221 7117 etc.

int clean_data(int *, double*, char*, int *, double *, char *, int):

int * - pointer containing original all account IDs information

double * - pointer containing original all amounts information

char* - pointer containing original all designation information

int * - pointer to hold the valid account IDs information

double * - pointer to hold the corresponding valid amounts information

char * - pointer to hold the corresponding valid designation information.

int – original size

returns int – count of the valid number of IDs.

All the records which conform to the criteria specified by the CS department must be used to fill the second set of int*, double* and char* pointers and then at the end return the count of such valid rows.

In this function, you will loop through the original records and find all those which match the criteria specified and then copy them over to the new set of valid pointers. To do this you should check if each account number is a palindrome (call check_palindrome function on each original ID) and if the corresponding amount value is exactly between $500 and $5000.00 inclusive and the corresponding designation is anything other than P, if these matches then copy that TAs information into the new set of pointers correspondingly and at the end return how many such valid records were copied over. One of the major problems you will run across is indexing, remember that the new valid pointers should start from 0 and must be incremented after every time a new record is added.

void sort_data(int *,double *,char *, int):

int * - Integer pointer holding all the valid account IDs

double * - Double pointer holding all the corresponding valid amounts

char * - Character pointer holding all the corresponding valid designations

int – count of the number of valid records (remember this cannot be 13 anymore)

Now, you need to group them based on the designations, the first group must have the Juniors (J) and then the second group must have the Seniors (S) and the last group must be the Masters(M). Remember you are not sorting anything in ascending or descending order, you are group sorting them. When you swap the positions of designations while grouping, make sure the account IDs and the amounts match too after the grouping.

BONUS (+5):

bool bonusWriteData(char *,int *,double *,char *,int):

char *-Takes the filename - hw3GoodOutput.txt

int * - integer pointer for account IDs

double * - double type pointer for amounts

char * - character pointer to hold designations

In this function, you will sort the records based on the ascending order of the amounts and the you will print them below the existing data in hw3GoodOutput.txt file. Then you will calculate the least amount which is greater than or equal to the average value of the amounts and the greatest amount which is lesser than or equal to the average value of the amounts. You can (recommended) write 2 separate functions to perform this operation.

Look at the hw3GoodOutput.txt file for more info. Return false if unable to open the file for appending, else write all the requested info and return true.

int main(int argc, char *argv[]):

Use command line arguments to get the file names and the original number of accounts. Use the variable argc to check if there are enough inputs provided by the user. If not, display an error message and terminate the program. Get the size from command line using atoi. Allocate space to an integer, double and a character pointer using malloc to store the original records. Also, allocate another set of integer, double and character pointers to store the valid records (while doing this assume all the original records are valid, so the size for malloc will still be 13).

Call the load_data function, print_data function and call the highest_amount, lowest_amount and the average_amount functions to print all the stats (max, min and avg). Call the write_data function to write the original set of records into the hw3BadOutput.txt file. Look at the file on blackboard for reference.

Call the clean_data function to clean-up the records. This function returns a number denoting the valid number of records present after ignoring the ones which didn’t satisfy the CS department criteria. Call the sort_data function by sending the valid pointers (int,double,char and the valid size), this will group your records based on the designations. Call the print_data function by sending the valid pointers to print the cleaned data which is now grouped by designations. Call the highest_amount, lowest_amount and the average_amount functions on the valid amounts pointer and print the results as shown in the sample output below. After doing this then call the write_data function to write the cleaned set of records into the hw3GoodOutput.txt file. Remember, your valid records must be sorted (grouped by designations) both on the screen as well as on the hw3GoodOutput.txt file.

At the end, free the space allocated to the:
2 integer type pointers
2 double type pointers
2 char type pointers using the function free ().

You should show proper error messages and output messages as and when necessary and must be formatted correctly. Not doing this will result in loss of points.

Flow should be:

Create pointers (6) > Open file and load data > then print them along with stats > write the original records to the hw3BadOutPut.txt file > Update the records correctly (clean_data) > sort (group them according to the designations) > print the data again with stats >Then write the data onto hw3GoodOutput.txt file > Free the memory allocated for the pointers.

Sample Output

dpstm3:~$ compile dheeraj-hw3.c

dpstm3:~$ ./a.out

USAGE ERROR ---> ARGUMENT COUNT MUST BE 5                                          //CHECK ARGC ERROR

dpstm3:~$ ./a.out hw3Input.txt

USAGE ERROR ---> ARGUMENT COUNT MUST BE 5

dpstm3:~$ ./a.out 13 hwInput.txt

USAGE ERROR ---> ARGUMENT COUNT MUST BE 5

dpstm3:~$ ./a.out 13 hw3.txt hw3BadOutput.txt hw3GoodOutput.txt //CHECK FILENAME ERROR

Unable to open the input file hw3.txt for load data

Exiting!!!

THERE IS NO RESTRICTION ON THE FILENAME NAMING CONVENTION, JUST KEEP A TRACK OF IT

You should only submit your hw3.c file, nothing else.

dpstm3:~$ ./a.out 13 hw3Input.txt hw3BadOutput.txt hw3GoodOutput.txt

ACCOUNTS         AMOUNTS         DESIGNATION

1001                            1591.63              M

1011                            1503.54              S

2002                            2000.43              M

3332                            2567.99              J

2112                            3112.67              M

4334                            5111.34              S

6134                            7172.23              P

9009                            4999.00              P

  3003                            4000.00              S

4004           5000.00              S

5005           3000.00              J

6116           4599.91              S

7007                           2890.58              J

The TA with ID 6134 has the maximum amount of 7172.23 from the original file data

The TA with ID 1011 has the minimum amount of 1503.54 from the original file data

The average amount across all the TAs is 3657.64

Data from hw3Input.txt is now written to hw3BadOutput.txt

*********DATA AFTER CLEANING********

ACCOUNTS         AMOUNTS         DESIGNATION

7007                           2890.58              J

5005          3000.00              J

3003                          4000.00              S

4004                          5000.00              S

6116                           4599.91              S

2002                           2000.43              M

1001                           1591.63              M

2112                           3112.67              M

The TA with ID 4004 has the maximum amount of 5000.00 from the updated file data

The TA with ID 1001 has the minimum amount of 1591.63 from the updated file data

The average amount across all the TAs is 3274.40

Updated output written to hw3GoodOutput.txt

Bonus appended to hw3GoodOutput.txt

dpstm3:~$ ./a.out 13 hw3Input.txt hw3BadOutput.txt hw3GoodOutput.txt

ACCOUNTS         AMOUNTS         DESIGNATION                                    //RED DENOTES INVALID

1001                            1591.63              M

1011                            1503.54              S

2002                            2000.43              M

3332                            2567.99              J

2112                            3112.67              M

4334                            5111.34              S

6134                            7172.23              P

9009                            4999.00              P

3003                            4000.00              S

4004           5000.00              S

5005           3000.00              J

6116           4599.91              S

7007                           2890.58              J

The TA with ID 6134 has the maximum amount of 7172.23 from the original file data

The TA with ID 1011 has the minimum amount of 1503.54 from the original file data

The average amount across all the TAs is 3657.64

Data from hw3Input.txt is now written to hw3BadOutput.txt

*********DATA AFTER CLEANING********

//LOOK AT DESIGNATION IT IS GROUPED & ORDERED as J’s first, S’s next and M’s next

ACCOUNTS         AMOUNTS         DESIGNATION                   

7007                           2890.58              J

5005          3000.00              J

3003                          4000.00              S

4004                          5000.00              S

6116                           4599.91              S

2002                           2000.43              M

1001                           1591.63              M

2112                           3112.67              M

The TA with ID 4004 has the maximum amount of 5000.00 from the updated file data

The TA with ID 1001 has the minimum amount of 1591.63 from the updated file data

The average amount across all the TAs is 3274.40

Updated output written to hw3GoodOutput.txt

Bonus appended to hw3GoodOutput.txt

inputfile:

Bad Output:

Good Output:

Good Output Bonus:

Explanation / Answer

Here I am explain the sample code for the given problem.

In C programming, file is a place on your physical disk where information is stored.

-Create a variable of type "FILE*".

-Open the file using the "fopen" function and assign the "file" to the data variable.

-Check to make sure the file was successfully opened by checking to see if the data == NULL. Otherwise error occurred.

- fprintf or fscanf functions to write/read from the file.

Here I am providing sample code for file operation for reading and writing

Normally file operations include

-Creating a new file

-Opening an existing file

-Closing a file

-Reading from and writing information to a file

-For reading and writing to a text file, the functions arefprintf() and fscanf().

-They are just the file versions of printf() and scanf(). The only difference is that, fprint and fscanf expects a pointer to the structure FILE.