Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose: Use command line arguments for input Use malloc() and free()function. Collect input from a text file. Write output to a text file. Creating integer,

Purpose:

Use command line arguments for input

Use malloc() and free()function.

Collect input from a text file.

Write output to a text file.

Creating integer, double and character arrays. To create these pointers, use malloc function. Use the value of the number of accounts given by the user from the command prompt (see description below) in the malloc function and allocate space (like lab-10 and lab-11). The total number of entries is 13 in the input file.

Command line arguments: Use the command line arguments to collect inputs from the user. 5 inputs are provided from the command prompt, the number of accounts, the input file name, the bad output file name, and the good output file name. Reading inputs from the command prompt is shown below. The run command will be following

./a.out 13 hw3Input.txt hw3BadOutput.txt hw3GoodOutput.txt

argc -> count of arguments (in this case 5)

*(argv+0) -> ./a.out *(argv+2) -> 13

Here the input file name is hw3Input.txt (at argv[2]) and the number 13 is the number of accounts (at argv[1]). Use this size to allocate the space for the integer (for the IDs), double (for the amounts) and char (for the designations) pointers using malloc function. Since the number 13 is stored in a character array so convert it from string to integer value by using the library function atoi. Function atoi is a library function that converts a string to corresponding integer value so the code will look like the following.

int count = atoi(argv[1]);

The variable count has the value 13 and this number is integer now. Use the value of count in the malloc function to allocate space for the integer, double and char pointers.

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 Js must come first, followed by the Ss and then come the Ms.

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 didnt 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 Js first, Ss next and Ms 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago