Use ONLY pointer arithmetic and pointer notation in the implementation. Using ar
ID: 3696054 • Letter: U
Question
Use ONLY pointer arithmetic and pointer notation in the implementation. Using array notation will result in a 0 for the whole assignment.
Homework 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. 4 inputs are provided from the command prompt, the input file name, the update file name,the number of accounts and the output file name. Reading inputs from the command prompt is shown below. The run command will be following
./a.out input.txt 8 update.txt output.txt
argc -> count of arguments ( in this case 5)
*(argv+0) -> ./a.out
*(argv+2) -> 8
Here the input file name is input.txt (stored in second row of the 2D character array argv, argv[1]) , update.txt is the name of the file name from which data to be updated is read, 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 (pointers).
Update file:
The first few lines in update.txt looks like this
1109 500.00
2229 142.00
3125 6779.73
What you need to do is for the account numbers specified in that file you need to update the amounts as specified. If originally account 1109 had $234.55 and the update file specified an update of $500. The amount must now be updated for $734.55. Do this for all the account numbers specified in this file.
Output file:
Your output.txt should always contain the records of account numbers and corresponding amounts along with the statistics (high, low and average). If your update failed then this file will have the original record and statistics, but if your update was successful then this will have the latest records in a SORTED (by account number) manner.
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 *,int ): This function takes the input file name, integer and float pointers and the size as integer. It opens the input file. If unable to open it, returns 0. Otherwise load the account information from the text file into the integer and float arrays and return 1 at the end. The char pointer is the string holding the name of the file to open, int * is the account number array, float * is the amount array, the last int is the size or the number of records. Use fscanf or other library functions to read in the data.
void print_data(int *, float *, int): This function takes integer array , float array and integer size and displays the data on the console stored in these arrays as shown in the sample output below. Use proper output formatting.
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 update_data(char *, int *,float *,int ): This function is similar to load_data function, here you will try to open update.txt. If unable to open it, return 0. Otherwise update the account information from the text file into the integer and float arrays and return 1 at the end. Remember, update the amounts off all those account numbers present in this file. Use fscanf or other library functions to read in the data. You might have to malloc another set of int and float pointers local to this function as it will help you navigate through this file and also update the original integer and floating arrays. Don’t forget to free the ones created here at the end of the function.
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.
void print_stats(int *, float *, int , int, int, float): This function helps you to print the statistics on the accounts. The highest, the lowest and the average amount and which account hold them. This prints on the console, so call it after print_data every time. All the arguments passed to this function remain the same as above except the file name which is not required as we print on the screen.
void sort_data(int *,float *,int): This function takes the int account numbers, float amounts and the size, then you need to sort the fields in the ascending order of the account numbers. Make sure the amounts match after the sorting. After doing this then call the write_data function. Remember, your updated records must be sorted both on the screen as well as on the output.txt file.
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, print_data function, and print_stats function to load and print the account information. Call the update_data function to update the records. Call the highest_amount , lowest_amount and the average_amount functions and print the results as shown in the sample output below, by calling the print_data function to display the updated information(sorted). 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. Show proper error messages as and when necessary.
Flow should be:
Create pointers > Open and load data > then print them along with stats > Update the records correctly > if successful print the data again with stats (but sorted this time)> else just print it in original manner. >Then write the data onto output.txt. > Free the memory allocated for the pointers.
Sample Output
[dddd@bengal-vm1 ~]$ gcc hw3.c -Wall -Werror
[dddd@bengal-vm1 ~]$ ./a.out
Insufficient arguments
[dddd@bengal-vm1 ~]$ ./a.out input.txt 8
Insufficient arguments
[dddd@bengal-vm1 ~]$ ./a.out input.txt 8 update.txt
Insufficient arguments
[dddd@bengal-vm1 ~]$ ./a.out inpu.txt 8 update.txt output.txt
ERROR!!! Unable to open the file inpu.txt. Check again
[dddd@bengal-vm1 ~]$ ./a.out input.txt 8 updte.txt output.txt
ACC NUM AMOUNT
1109 234.55
2371 2011.75
3125 0.00
4981 537.65
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 1797.36
ERROR!!! Unable to open the file updte.txt to perform update operation, please check
Output written to output.txt
Thank you for using our application // here output.txt looks exactly as above (not sorted)
[dddd@bengal-vm1 ~]$ ./a.out input.txt 8 update.txt output.txt
ACC NUM AMOUNT
1109 234.55
2371 2011.75
3125 0.00
4981 537.65
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 1797.36
**** UPDATED WITH NEW VALUES AND SORTED in the ascending order of Account Numbers ***
ACC NUM AMOUNT
1109 734.55 // observe the changes in the value of amount
2229 900.00
2371 2011.75
3125 8779.73
4981 537.65
5410 1745.35
7402 7236.20
9036 2000.80
The highest amount is 8779.73 in the account number 3125
The lowest amount is 537.65 in the account number 4981
The average amount is 2993.25
Output written to output.txt // again output.txt looks exactly as above (sorted with stats)
Thank you for using our application
[dddd@bengal-vm1 ~]$
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int load_data(char *s,int *ptra, float *ptrb,int n)
{
FILE *ip;
ip=fopen(s, "r");
if(ip==null)
{
printf("Unable to open input file");
return 0;
}
else
{
fscanf(ip, "%d", n);
for(i=0;i<n;i++)
{
fscanf(ip, "%d %f", ptra+i,ptrb+i);
}
return 1;
}
}
void print_data(int *ptra,float *ptrb,int n)
{
int i;
printf("Acc Num Amount");
for(i=0;i<n;i++)
printf("%d %f",ptra+i,ptrb+i);
}
int highest_amount( float *ptrb, int n )
{
int i,j;
float max=0;
for(i=0;i<n;i++)
{
if(max<(ptrb+i))
{
max=(ptrb+i);
j=i;
}
}
return j;
}
int lowest_amount( float *ptrb, int n)
{
int i,j;
float min=50000;
for(i=0;i<n;i++)
{
if(min>(ptrb+i))
{
min=(ptrb+i);
j=i;
}
}
return j;
}
float average_amount( float *ptrb, int n )
{
int i;
float avg=0;
for(i=0;i<n;i++)
{
avg+=avg;
}
avg /= n;
return avg;
}
int update_data(char *s, int *ptra,float *ptrb,int n )
{
FILE *ip;
ip=fopen(s, "r");
int acc;
float bal;
if(ip==null)
{
printf("Unable to open update file");
return 0;
}
else
{
while(fscanf(ip, "%d %f",acc ,bal!=EOF)
{
for(i=0;i<n;i++)
{
if((ptra+i)==acc)
{
ptrb+i +=bal;
break;
}
}
}
return 1;
}
}
void write_data(char *s , int *ptra, float *ptrb, int n , int high, int low, float avg)
{
FILE * fp;
int i;
fp = fopen (s, "w+");
for(i=0;i<n;i++)
fprintf(fp, "%d %f ", ptra+i, ptrb+i);
fprintf(fp,"%s %f %d" "The highest amount is",ptrb+high, " in the account number", ptra+high);
fprintf(fp,"%s %f %d" "The lowest amount is",ptrb+low, " in the account number", ptra+low);
fprintf(fp,"%s %f" "The average amount is",avg);
}
void print_stats(int *ptra, float *ptrb, int n , int high, int low, float avg)
{
printf("%s %f %d" "The highest amount is",ptrb+high, " in the account number", ptra+high);
printf("%s %f %d" "The lowest amount is",ptrb+low, " in the account number", ptra+low);
printf("%s %f" "The average amount is",avg);
}
void sort_data(int *ptra,float *ptrb,int n)
{
int i,j,swap;
float balswap;
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < n - j - 1; j++)
{
if ((ptra+j) > (ptra+(j+1))) /* For decreasing order use < */
{
swap = ptra+j;
ptra+j = ptra+j+1;
ptra+j+1 = swap;
balswap = ptrb+j;
ptrb+j = ptrb+j+1;
ptrb+j+1 = balswap;
}
}
}
print_data(ptra,ptrb,n);
}
void main( int argc, char *argv[] ) {
int *ptra,n,ip,high,low;
float *ptrb,avg;
if( argc < 5 ) {
printf("Insufficient orgument");
}
else if( argc == 5 ) {
n=ato(argv[2]);
ptra=(int*)malloc(n*sizeof(int));
ptrf=(float*)malloc(n*sizeof(float));
ip=load_data(argv[1],ptra,ptrb,n);
print_data(ptra,ptrb,n);
high=highest_amount( ptrb, n );
low =lowest_amount(ptrb,n);
avg = average_amount(ptrb,n);
up = update_data(argv[3], ptra,ptrb,n);
write_data(argv[4] , ptra, ptrb, n , high, low, avg);
print_stats(ptra, ptrb, n , high, low, avg);
sort_data(ptra,ptrb,n);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.