Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Directions: Complete the following assignment using the description given in each section. Purpose Use command line arguments to read file name. Use malloc function. Collect

Directions: Complete the following assignment using the description given in each section.

Purpose

Use command line arguments to read file name.

Use malloc function.

Collect input from a text file.

Write output to a text file.

Submission information

Submit this assignment using the instructions given by the TA. DO NOT submit on blackboard or by email!

Note

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.

Text files (input.txt and update.txt) required for the homework assignment. They are posted on blackboard. To read input from the text file, the file should be in the SAME directory or folder where your homework code (file) is. If you are unable to move the text file to your babbage 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.

Use of command line arguments and function calls for this assignment is going to be similar to the code provided in our class lecture.

Sample output.txt file is provided on blackboard to show how the output file will look after calling the write_data function.

Use ONLY pointer arithmetic and pointer notation in the implementation.

Description

Create the arrays using malloc. Update the account information by using the inputs from the update.txt file. Once the information is updated, perform simple operations like search, computing the average, and finding the highest and the lowest amount. Write the updated account information along with the results from the operations performed into an output text file.

Command line arguments: Use the command line arguments to collect inputs from the user. For the homework assignment all the file names and the number of accounts are provided from the command prompt. The run command will be the following

./a.out input.txt 8 update.txt output.txt

Update file: Update file contains information on four accounts. Update the original accounts by adding or subtracting the given amount in the update.txt file.

4

1109 334.15

2371 -1051.65

4981 -97.05

1109 -4.45

For example, after the update operation, the total amount in account 1109 should be 234.55+334.15-4.45= 564.25. Similarly, update the amounts for other accounts.

Implement following functions for the homework assignment:

int load_data(char*, int *, float *): 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).

void print_data(int *, float *, int): This function takes integer array , float array and integer size and displays the data stored in these arrays as shown in the sample output below.

int update_data(char*, int *, float *, int): This function takes the input file name, integer and float pointers and the number of accounts. It opens the update.txt file and updates the account information based on the inputs stored in the update file. Return 0 if unable to open the update file, else return 1.

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.

void 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.

main():Similar to the prelab assignment use command line arguments to get the file names and the number of accounts from the user. 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. Allocate space to an integer and a float pointer using malloc (as discussed in the prelab document). Call the load_data function and print_data function to load and print the account information. Call the update_data function to update 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.

Sample Output

$ ./a.out input.txt 8

Insufficient arguments

$ ./a.out input.txt 8 update.txt

Insufficient arguments

$ ./a.out inp.txt 8 update.txt output.txt

Unable to open the input file

$ ./a.out input.txt 8 update.txt output.txt

Account No. Amount

1109 234.55

2371 2011.75

3125 945.05

4981 537.65

7402 6235.75

2229 42.00

9036 3655.80

5410 1745.35

Updated account information

Account No. Amount

1109 564.25

2371 960.10

3125 945.05

4981 440.60

7402 6235.75

2229 42.00

9036 3655.80

5410 1745.35

The highest amount is $6235.75 in the account number 7402

The lowest amount is $42.00 in the account number 2229

The average amount is $1823.61

Input.txt:

8 1109 234.55 2371 2011.75 3125 945.05 4981 537.65 7402 6235.75 2229 42.00 9036 3655.80 5410 1745.35 

ouput.txt

1109 564.25 2371 960.10 3125 945.05 4981 440.60 7402 6235.75 2229 42.00 9036 3655.80 5410 1745.35 The highest amount is $6235.75 in the account number 7402 The lowest amount is $42.00 in the account number 2229 The average amount is $1823.61 

update.txt

4 1109 334.15 2371 -1051.65 4981 -97.05 1109 -4.45 

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

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions