To create an integer and a float array use malloc function


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.

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: To create an integer and a float array use malloc function
Reference No:- TGS02737797

Expected delivery within 24 Hours