Directions: Complete the following assignment using the description given in eac
ID: 3858243 • Letter: D
Question
Directions: Complete the following assignment using the description given in each section.
Purpose
• Use command line arguments to read file name.
• Use malloc function.
• Collect input from a text file.
• Write output to a text file.
Submission information
Submit this assignment using the instructions given by the TA. DO NOT submit on blackboard or by email!
Note
Read the complete document carefully (before starting the assignment) to understand all of the homework requirements and operations to perform. Check the sample output to see how results are displayed.
Text files (input.txt and update.txt) required for the homework assignment. They are posted on blackboard. To read input from the text file, the file should be in the SAME directory or folder where your homework code (file) is. If you are unable to move the text file to your babbage directory or folder, then create a text file using the vim editor. The procedure is same as creating a .c file, where the only difference is a text file is going to have an extension .txt. Example:
vim input.txt
This is going to open a text file, type in the information (exactly as posted in the text file on blackboard) and save the file using the command :wq.
Use of command line arguments and function calls for this assignment is going to be similar to the code provided in our class lecture.
Sample output.txt file is provided on blackboard to show how the output file will look after calling the write_data function.
Use ONLY pointer arithmetic and pointer notation in the implementation.
Description
Create the arrays using malloc. Update the account information by using the inputs from the update.txt file. Once the information is updated, perform simple operations like search, computing the average, and finding the highest and the lowest amount. Write the updated account information along with the results from the operations performed into an output text file.
Command line arguments: Use the command line arguments to collect inputs from the user. For the homework assignment all the file names and the number of accounts are provided from the command prompt. The run command will be the following
./a.out input.txt 8 update.txt output.txt
Update file: Update file contains information on four accounts. Update the original accounts by adding or subtracting the given amount in the update.txt file.
4
1109 334.15
2371 -1051.65
4981 -97.05
1109 -4.45
For example, after the update operation, the total amount in account 1109 should be 234.55+334.15-4.45= 564.25. Similarly, update the amounts for other accounts.
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).
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 update_data(char*, int *, float *, int): This function takes the input file name, integer and float pointers and the number of accounts. It opens the update.txt file and updates the account information based on the inputs stored in the update file. Return 0 if unable to open the update file, else return 1.
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.
void 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.
main():Similar to the prelab assignment 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 (as discussed in the prelab document). Call the load_data function and print_data function to load and print the account information. Call the update_data function to update 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.
Sample Output
$ ./a.out input.txt 8
Insufficient arguments
$ ./a.out input.txt 8 update.txt
Insufficient arguments
$ ./a.out inp.txt 8 update.txt output.txt
Unable to open the input file
$ ./a.out input.txt 8 update.txt output.txt
Account No. Amount
1109 234.55
2371 2011.75
3125 945.05
4981 537.65
7402 6235.75
2229 42.00
9036 3655.80
5410 1745.35
Updated account information
Account No. Amount
1109 564.25
2371 960.10
3125 945.05
4981 440.60
7402 6235.75
2229 42.00
9036 3655.80
5410 1745.35
The highest amount is $6235.75 in the account number 7402
The lowest amount is $42.00 in the account number 2229
The average amount is $1823.61
Input.txt:
ouput.txt
update.txt
Explanation / Answer
Given below is the code and output. Please rate the answer if it helped. Thank you.
#include <stdio.h>
#include <stdlib.h>
int load_data(char*, int *, float *); //load file contents and return 1 on success and 0 on failure
void print_data(int *, float *, int); //display the contents of the array
int update_data(char*, int *, float *, int); //updates the data based on the information in the filename specified
int highest_amount( float *, int ); //returns the index of hte highest value
int lowest_amount( float *, int);//return the index of the lowest value
float average_amount( float *, int );//returns the average value
void write_data(char* , int *, float *, int , int, int, float);//writes all data to the specified filename
int main(int argc, char *argv[])
{
int *accno;
float *amount;
int size;
int highestIdx, lowestIdx;
float avg;
if(argc != 5)
{
printf("Insufficient arguments ");
exit(1);
}
//get the number of accounts from command line and conver to int
size = atoi(argv[2]);
//allocate memory
accno = (int *) malloc(sizeof(int) * size);
amount = (float *) malloc(sizeof(float) * size);
//load data from input file
if(load_data(argv[1], accno, amount) != 1)
{
printf("Unable to open input file ");
exit(1);
}
print_data(accno, amount, size);
//update data and find statistics
update_data(argv[3], accno, amount, size);
highestIdx = highest_amount(amount, size);
lowestIdx = lowest_amount(amount, size);
avg = average_amount(amount, size);
//display the results
print_data(accno, amount, size);
printf("The highest amount is $%.2f in the account number %d ", *(amount+highestIdx), *(accno+highestIdx));
printf("The lowest amount is $%.2f in the account number %d ", *(amount+lowestIdx), *(accno+lowestIdx));
printf("The average amount is $%.2f ", avg);
write_data(argv[4], accno, amount, size, highestIdx, lowestIdx, avg);
//free memory
free(accno);
free(amount);
return 0;
}
int load_data(char* fname, int *accno, float *amount) //load file contents and return 1 on success and 0 on failure
{
FILE *fp = fopen(fname, "r");
int n, i;
if(fp == NULL)
return 0;
else
{
fscanf(fp, "%d", &n);
for(i = 0 ; i < n; i ++)
fscanf(fp, "%d %f", accno+i, amount+i);
//close file and return success
fclose(fp);
return 1;
}
}
void print_data(int *accno, float *amount, int size) //display the contents of the array
{
int i;
printf("Account No. Amount ");
for(i = 0; i < size; i++)
{
printf("%10d %10.2f ", *(accno+i) , *(amount+i));
}
}
//return the index of the account number being searched. If not found return -1
int index_of_account(int *accno, int size, int search)
{
int i ;
for(i = 0; i < size; i++)
{
if(*(accno + i) == search)
return i;
}
return -1;
}
int update_data(char* fname, int *accno, float *amount, int size) //updates the data based on the information in the filename specified
{
FILE *fp = fopen(fname, "r");
int n, i, idx;
int acc;
float amt;
if(fp == NULL)
{
printf("Unable to open file %s", fname);
return 0;
}
else
{
fscanf(fp, "%d", &n);
for(i = 0 ; i < n; i ++)
{
fscanf(fp, "%d %f", &acc, &amt);
idx = index_of_account(accno, size, acc);
if(idx != -1)
{
amount[idx] += amt;
}
}
//close file and return success
fclose(fp);
return 1;
}
}
int highest_amount( float *amount, int size )//returns the index of hte highest value
{
int i;
int highestIdx = 0; //consider the 1st value to be hightest
//iterate from 2nd to end
for(i = 1; i < size; i++)
{
if(*(amount+i) > *(amount + highestIdx))
highestIdx = i;
}
return highestIdx;
}
int lowest_amount( float *amount, int size)//return the index of the lowest value
{
int i;
int lowestIdx = 0; //consider the 1st value to be lowest
//iterate from 2nd to end
for(i = 1; i < size; i++)
{
if(*(amount+i) < *(amount + lowestIdx))
lowestIdx = i;
}
return lowestIdx;
}
float average_amount( float *amount, int size )//returns the average value
{
int i;
double avg = 0;
for(i = 0; i < size; i++)
{
avg += *(amount + i);
}
avg /= size;
return avg;
}
void write_data(char* fname , int *accno, float *amount, int size , int highestIdx, int lowestIdx, float avg)//writes all data to the specified filename
{
FILE *fp = fopen(fname, "w");
int i;
if(fp == NULL)
{
printf("Could not write to file %s", fname);
return;
}
else
{
for(i = 0; i < size; i++)
fprintf(fp, "%d %.2f ", *(accno + i), *(amount + i));
fprintf(fp, " The highest amount is $%.2f in the account number %d ", *(amount+highestIdx), *(accno+highestIdx));
fprintf(fp, "The lowest amount is $%.2f in the account number %d ", *(amount+lowestIdx), *(accno+lowestIdx));
fprintf(fp, "The average amount is $%.2f ", avg);
fclose(fp);
}
}
inptut file : input.txt
8
1109 234.55
2371 2011.75
3125 945.05
4981 537.65
7402 6235.75
2229 42.00
9036 3655.80
5410 1745.35
input file: update.txt
4
1109 334.15
2371 -1051.65
4981 -97.05
1109 -4.45
output
$ gcc updateacc.c
$ ./a.out inp.txt 8 update.txt output.txt
Unable to open input file
$ ./a.out
Insufficient arguments
$ ./a.out input.txt 8 update.txt output.txt
Account No. Amount
1109 234.55
2371 2011.75
3125 945.05
4981 537.65
7402 6235.75
2229 42.00
9036 3655.80
5410 1745.35
Account No. Amount
1109 564.25
2371 960.10
3125 945.05
4981 440.60
7402 6235.75
2229 42.00
9036 3655.80
5410 1745.35
The highest amount is $6235.75 in the account number 7402
The lowest amount is $42.00 in the account number 2229
The average amount is $1823.61
$ cat output.txt
1109 564.25
2371 960.10
3125 945.05
4981 440.60
7402 6235.75
2229 42.00
9036 3655.80
5410 1745.35
The highest amount is $6235.75 in the account number 7402
The lowest amount is $42.00 in the account number 2229
The average amount is $1823.61
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.