Create an integer and a float array use malloc function

Assignment Help Computer Engineering
Reference no: EM131942325

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.

Reference no: EM131942325

Questions Cloud

What are the consequences of planning for change : What is planning for change and what are the consequences of planning for change and the way to improve it? Need at list 2 references.
Describe any attempts that the employer has made : Describe any attempts that the employer has made to increase job range or job depth, and evaluate whether or not these efforts appeared to be motivational.
Build nuclear bombs : Philip Kotler argues that professional marketers "should have the same ambivalence as nuclear scientists who help build nuclear bombs." Is that a valid argument
Evidence of unethical business practices : Would you leave your position within a company if you saw evidence of unethical business practices? Why or why not?
Create an integer and a float array use malloc function : 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.
Find the optimal order quantity : The retailer reduces the price of the product by $1 per unit for every week that delivery is delayed in order to keep the customer waiting for the order.
Prepare income statement and statement of retained earnings : December 1 - Issued capital stock for $50,000 to start plumbing business. Prepare an Income Statement, Statement of Retained Earnings and a Balance Sheet
Full-time employees in order to preserve profits : Papa John's, a leading family style pizza restaurant chain, decided they would cut full-time employees in order to preserve profits at the restaurant chain.
Why you choose videos regarding federal reserve system : In the Discussion Forum, post articles, pictures, and or videos, and a explain why you chose that regarding the Federal Reserve System.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd