Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Directions: ( CODE IN C ONLY!!!) Complete the following homework assignment using the description given in each section. Do not email code to TAs for

Directions: ( CODE IN C ONLY!!!)
Complete the following homework assignment using the description given in each section.
Do not email code to TAs for help. They cannot help fix compile errors via screenshots
Come prepared with specific question when you come to office hours.
Use lab 10 and lab11 code and build off that.
Purpose:
Use command line arguments for input
Use malloc() and free()function.
Collect input from a text file.
Write output to a text file.
Submission information:
Filename must be: labsection-hw3.c (Include your respective lab section)
Example: If you are in lab K,
k-hw3.c
Submission Command: (Case Sensitive)
submit CS1050 HW3 k-hw3.c
Note
1. Read the complete document carefully (before starting the assignment) to understand all the homework requirements and operations to perform. Check the sample output to see how results are displayed.
2. Text files (hw3Input.txt) is required for the homework assignment. It is posted on blackboard. To read input from the text file, the file should be in the SAME directory or folder where your hw3 code (file) is. If you are unable to move the text file to your tc.rnet directory or folder, then create a text file using the vim editor. The procedure is same as creating a .c file, where the only difference is a text file is going to have an extension .txt. Example:
vim input.txt
This is going to open a text file, type in the information (exactly as posted in the text file on blackboard) and save the file using the command: wq.
4. Sample hw3BadOutput.txt and hw3GoodOutput.txt files are provided on blackboard to show how the output file will look after calling all the relevant functions.
5. Use ONLY pointer arithmetic and pointer notation in the implementation. Using array notation will result in a 0 for the whole assignment. Use library functions as and when necessary.
Description
Help us get paid.
- Jeremy, Darius, Ryan, Holt, Nicolle, Sydney, Candace and Dheeraj
Joe Guilliams had to fill out a form to pay his 1050 TAs at the end of the semester. Joe typed up the hw3Input.txt while watching his beloved St Louis Blues game. He was so engrossed in the game he didnt notice that he entered some additional data into the file. He has entered the details of his TAs from other courses. The file has 13 rows of ID, amount and the designation (possible values is J, S, M or P) of each TA, so that the department can pay all his 1050 TAs accordingly. After celebrating over the weekend, Joe later realized that he has entered details of his other TAs who are PhDs (designation as P), who have a non-conforming account ID and a larger pay scale. So, to fix this, he asked if any of the TAs could fix it by writing a small program. His TAs, being the slackers they are, didnt want to code out this problem, so they converted it into an assignment for CS1050 HW3.
CS department follows the following criteria to pay its 1050 TAs:
1. Each TA for 1050 has their account IDs as palindromes
2. Each TA for 1050 can only be paid between $500.00 and $5000.00 (both inclusive)
3. A 1050 TA can have the following designations J(Junior), S(Senior), M(Masters)
Homework assignment involves file handling; inputs are read from a text file. Input consist of account IDs, an amount corresponding to each account number and a single character denoting the designation for each ID. Details about loading the information from the file into arrays is given below. After the information is correctly loaded display the information as shown in the sample output below, at first display the raw file details, then perform the data cleaning to match the CS department criteria and display all the required results as shown below in the specified format.
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:
1. 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.
2. 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.
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
print_data function will come in very handy. So, use it extensively while writing your program.
Writing comments will help you and specially your TA while grading.
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
Guidelines for Grading Homework-3
60 Points Possible
General
Assignment will not be given any credit (0/60) if your program does not compile or fail to produce any valid output. ADDITIONALLY, if your program does not compile with compile you will receive NO credit!!!! Use gdb for debugging seg faults.
Example: compile hw3.c
-10 points Not having proper output formatting, comments error messages and header (name, lab section and pawprint) in the code.
-5 points Using array notations
10 points load_data function.
5 points print_data function
10 points write_data function for both files
12 points clean_data function (includes check_palindrome).
10 points Calculating the highest, the lowest and the average amount correctly twice.
8 points sort_data function.
5 points Using malloc function to allocate memory to integer, double and char pointers and freeing the memory at the end and command line arguments usage
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.
This is the input.txt
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
This is the badoutput.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
This is the goodoutput.txt
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

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

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

Students also viewed these Databases questions