Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Read the complete document carefully (before starting the assignment) to understand all of the homework requirements and operations to perform. Check the sample output

1. Read the complete document carefully (before starting the assignment) to understand all of the homework requirements and operations to perform. Check the sample output to see how results are displayed. 2. A text file (input.txt) is required for the homework assignment. It is posted on Canvas. To read input from the text file, the file should be in the SAME directory or folder where your hw3 code (file) is. Try to upload it to your tc.rnet server using FTP (you may ask your TA about this in lab, or during office hours (it is too long to explain in this assignment or via email). 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 Canvas) and save the file using the command: wq. 3. A sample output.txt file is provided on Canvas to show how the output file will look after calling the write_data function. Description This homework assignment involves file handling. Inputs are read from a text file. Inputs consist of bank account numbers and an amount corresponding to each account number. Read bank account numbers and the amount from the input file and store them in an integer array and float array respectively. Details about loading the information from the file into arrays is given below. After the information is correctly loaded in the two arrays, display the information as shown in the sample output below. Counting the number of lines in the input file. You will need to count the number of lines in the input file so you will know how much storage to allocate in your arrays. Creating integer and float arrays. To create an integer and a float array use the malloc() function. Use the value of the number of accounts that you determined by counting the lines in the input file (see description below) in the malloc function to allocate space to integer and float pointers (similar to lab-10). Do not create arrays using static allocation like int x[100] or float y[100]. Instead, start with a pointer and allocate it only the required amount of space using the malloc function. Command line arguments: Use command line arguments to collect inputs from the user. Two inputs are provided from the command prompt: the input file name and the output file name. So, you will run your command like this: ./a.out input.txt output.txt Here the input file name is input.txt (stored in second row of the 2D character array argv, argv[1]) , output.txt is the name of the output file where output is written. Input file: As mentioned above the input is stored in a text file. Below are the first few lines from the sample text file 1109 234.55 2371 2011.75 3125 945.05 ......... .......... Use the size determined from counting the lines in the input file 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 array and the amount in a float array. Implement following functions for the homework assignment:

int get_record_count(char *): This function takes the name of an input file. It opens and reads lines from the input file, counting the lines as it goes. It returns the number of lines in the input file. You may assume no line in the input file has more than 255 characters. You may want to use something like fgets() to read in a line at a time so you can count. If you do use fgets(), be careful about checking the return value of fgets (see man fgets fgets() returns NULL if it fails to read a line). Also be careful about checking feof() as we discussed in class (see man feof). int load_data(char*filename, int *account, float *balance, int size): This function takes the input file name, integer and float pointers. It opens the input file. If unable to open it, return 0. Otherwise load the account information from the text file into the integer and float arrays and return 1 at the end (first line of the text file). You will use a FILE pointer to open the file. Ex: FILE* file = fopen(filename, x); Where x stands for a file access mode. For more information, please check out: https://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm

void print_data(int *, float *, int): This function takes an integer array, float array and integer size and displays the data stored in these arrays as shown in the sample output below. Make sure things line up correctly as shown. int highest_amount( float *, int ): This function takes the float pointer and the number of accounts. It finds the highest amount and returns the index corresponding to the highest amount. int lowest_amount( float *, int): Same as above function except it returns the index corresponding to the lowest amount.

float average_amount( float *, int ): Same as above functions except it returns the average amount for all the accounts. int write_data(char* , int *, float *, int , int, int, float): This function writes the account information (account numbers and amounts), the highest, the lowest and the average amount information into a text file (output.txt). Following are the arguments passed to this function char*- output file name.

int*- pointer containing account number information.

float*- pointer containing amount information.

int - number of accounts.

int - index of the highest amount in the amount array.

int - index of the lowest amount in the amount array.

float - average amount. Use fprintf or other library function to write the data into the text file. Returns a 0 if the file was unable to open and write. Returns a 1 if it was a success. main(): Use command line arguments to get the file names from the user. Use the variable argc to check if there are enough inputs provided by the user. If not, display an error message that includes the proper syntax for calling the program and terminate the program. Get the number of lines in the input file, so you know how many integers and floats you will need to allocate. Allocate space to an integer and a float pointer using malloc .

Call the load_data function and print_data function to load and print the account information. Call the print_data function to display the updated information. Call the highest_amount , lowest_amount and the average_amount functions and print the results as shown in the sample output below. Call the write_data function and write the information to the output text file. At the end, free the space allocated to the integer and the float pointers using the function free. NOTE: Do NOT hardcode filenames in your code. You will lose points. Do NOT hardcode the number of lines in the input.txt file. You will lose points.

Input.txt:

1109 234.55

2371 2011.75

3125 9452.05

4981 15.65

7402 6235.75

2229 42.00

9036 3655.80

5410 1745.35

Output.txt:

Account No. Amount

1109 234.55

2371 2011.75

3125 9452.05

4981 15.65

7402 6235.75

2229 42.00

9036 3655.80

5410 1745.35

The highest amount is $9452.05 in the account number 3125

The lowest amount is $15.65 in the account number 4981

The average amount is $2924.11

Sample Output compile j-hw3.c

./a.out Incorrect number of arguments

Syntax: ./a.out input_file_name output_file_name

./a.out input.txt Incorrect number of arguments

./a.out asdf output.txt Unable to open the input file

./a.out input.txt output.txt

8 records in the file.

Account No. Amount

1109 234.55

2371 2011.75

3125 9452.05

4981 15.65

7402 6235.75

2229 42.00

9036 3655.80

5410 1745.35

The highest amount of $9452.05 in the account number 3125

The lowest amount of $15.65 in the account number 4981

The average amount of $2924.11

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

Database Design Query Formulation And Administration Using Oracle And PostgreSQL

Authors: Michael Mannino

8th Edition

1948426951, 978-1948426954

More Books

Students also viewed these Databases questions

Question

Have roles been defined and assigned?

Answered: 1 week ago