Ask Computer Engineering Expert

Assignment

Purpose

• Use command line arguments to read file name.
• Use malloc() function.
• Use FILE pointers
• Collect input from a text file.
• Write output to a text file.

Description

Assignment involves file handling, inputs are read from a text file. Input 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 array display the information as shown in the sample output below.

Creating integer and float arrays. To create an integer and a float array 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 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 the command line arguments to collect inputs from the user. Two inputs are provided from the command prompt, the input file name and the number of accounts. The run command will be following

./a.out input.txt 8 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 output where output is written and the number 8 is the number of accounts (stored in third row of the variable argv, argv[2]). Use this size to allocate the space for the integer and the float arrays using malloc function. Since the number 8 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 following.

int size=atoi(argv[2]);

The variable size has the value 8 and this number is integer now. Use the value of size in the malloc function to allocate space the integer and the float pointers.

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

8
1109 234.55
2371 2011.75
3125 945.05
.........
..........

The first line in the input file gives the number of inputs stored in the file so 8 means there are 8 lines of inputs in the file and each line contains an account number and an amount in that account. To read input from the file use fscanf function. To begin read the first line from the file this will give the size of the input (i.e. number of line to read from the file), use this size or the number to run a loop and inside the loop read the remaining inputs (one line at a time) from the file. Store the account number in an integer array and the amount in a float array.

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

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). You will use a FILE pointer to open the file. Ex: FILE* file = fopen(filename, x);

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

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92780981

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As