Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

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

Junit test labyou will be creating a junit test class for

JUnit Test Lab You will be creating a JUnit Test Class for Gradebook.java, that has been provided for you.   Add a getScoreSize() method to the Gradebook class which returns scoresSize; Add a toString() method to the Gra ...

A factory makes parts for laptop computers including screws

A factory makes parts for laptop computers, including screws. The screws are required to have the right length. The lengths of the screws obey a normal distribution with mean μ=4.25 millimeters and standard deviation σ=0 ...

Suppose partition function of quicksort algorithm always

Suppose PARTITION function of QUICKSORT algorithm always produces 9 : 1 proportional split (i.e., after partition, one sub-array contains n/10 elements and the other sub-array contains 9n/10 elements). To take advantage ...

Suppose two packets arrive to two different input ports of

Suppose two packets arrive to two different input ports of a router at exactly the same time. Also suppose there are no other packets anywhere in the router. a. Suppose the two packets are to be forwarded to two differen ...

After reading this weeks materials please respond to two 2

After reading this week's materials, please respond to TWO (2) of the following questions. PROVIDE CITATION IN APA 1. Describe the controls contained within the three Access Control categories that can be integrated with ...

Companies persue closer coordination and collaboration with

Companies persue closer coordination and collaboration with channel suppliers to better address customer needs inorder to 1) Develop human resource management activities that improve the skills , expertise and knowledge ...

Give a recursive algorithm that generates a similar series

Give a recursive algorithm that generates a similar series of coins for changing n cents. Don't use dynamic programming for this problem.

Question 1 with the explosion of users on social media

Question: 1. With the explosion of users on social media sites, businesses need to establish their presence on social media sites. Just search for "Vans" or "Starbucks" on Facebook for examples of company sites. To manag ...

Remote direct memory access rdma enables efficient memory

Remote Direct Memory Access (RDMA) enables efficient memory access from one computer to another. Read about it using Google Scholar, ACM Digital Library or any professional literature tools. Write a 150-200 words (refere ...

A small factory produces two types of toys trucks and

A small factory produces two types of toys: trucks and bicycles. In the manufacturing process two machines are used: the piece maker and the assembler. It takes 2 hours to make the pieces of the bicycles and 1 hour for t ...

  • 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