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

  Registering user at directory service

Consider a user A who wishes to download the mp3 file with the title T.Describe how B is able to register anonymously at directory service (letting the service know that it has title T) such that A may use the directory service to receive the file..

  Implement a code in lisp to construct lists of random number

Implement a code in LISP to construct lists of random numbers, and lists of consecutive numbers. Show that length is correct for longer lists using the built in function length. Show that the average value of random list entries is as expected.

  What do you estimate is the bandwidth of a local loop

The telephone line that connects your house or business to the central office carries your conversation. What do you estimate is the bandwidth of a local loop?

  Draw architecture of one computer that will handle interrupt

Draw an architecture of one computer that will handle interrupts of different kinds, such as writing, opening, printing, spooling, and so forth.

  How many packets can be sent before transmitter has to stop

If a 7-bit sliding window size is used, how many packets can be sent before the transmitter has to stop and wait for an acknowledgment?

  How many comparisons of keys will be done by count sort

Determine how many comparisons of keys will be done by count sort. Is it a better algorithm than selection sort?

  Write a java program that allows you to enter your grades

Write a Java program that allows you to enter your grades in this course into an array of floats. Display the grades and the resulting average.

  Department in a global multinational company

CIS8000 Assignment - In the design process, the best way to show customers, managers, and programmers the product you have in mind and want to create is to make a mockup of the views that compose the application user interface.

  Show the storage of matrix to minimize the access time

Repeat Problem, if the algorithm accesses both rows and columns of the matrix.n algorithm requires access to each row of an N x N matrix.

  Construct state diagram for sequence detectors

Construct state diagram for sequence detectors that can detect the following sequences: 10010 (sequences may overlap).

  What are the services within the print services role

What are some of the management activities you can accomplish with ServerManagerCmd.exe. What are the services within the Print Services role. How do you add counter, using different sections of the Performance Monitor, in order to configure monitori..

  Write down some of the companies which make embedded

what are some of the companies that make embedded operating systems and are these operating systems just scaled down

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