Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note Read the complete document carefully (before starting the assignment) to un

ID: 3859023 • Letter: N

Question

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 tc.rnet directory, 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 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(FILE*, int *, float *): This function takes the input file pointer, integer and float pointers. It opens the input file. If there is any errors with the file or the malloc returning NULL, 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 class/lab). 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

Insufficient arguments

$ ./a.out input.txt update.txt

Insufficient arguments

$ ./a.out inp.txt update.txt output.txt

Unable to open the input file

$ ./a.out input.txt 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

Explanation / Answer


//libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//prototypes
int load_data(char*, char**, int*, float*, int);
void print_data(char**, int*,float*, int);
int check_account(char**, int*,char*,int, int);
int string_compare(char*, char*);
int highest_amount(float*, int);
int lowest_amount(float*, int);
float average_amount(float*, int);
void write_data(char*, char**, int*, float*,int,int,int,float);
// our main program
int main(int argc, char** argv) {
  
  
    if(argc != 4){
   printf("Insufficent Arguments");
    }
    //setting our variables to the things located in the arguments
    int *id = malloc(atoi(argv[2]) * sizeof(int));
    float *amount = malloc(atoi(argv[2]) * sizeof(float));
    int n = atoi(argv[2]);
    char **names = malloc(atoi(argv[2]) * sizeof(char*));
    char *targetname = malloc(atoi(argv[2]) * sizeof(char*));
    int i, d;
    i = 0;
    for(i = 0; i < atoi(argv[2]); i++)
    {
   names[i] = malloc(atoi(argv[2])+1 * sizeof(char));
    }
    // calling check
    int size = load_data(argv[1], names, id, amount, n);
    if(size == 0){
   printf(" Unable to open the input file ");
   return 0;
    }
    print_data(names, id, amount, n);

    printf(" Enter a name: ");
    scanf("%s", targetname);
  
    printf(" Enter a account number: ");
    scanf("%d",&d);

   if(check_account(names, id, targetname, d, n) == -1){
       printf("Unable to find account ");
    }
    int high = highest_amount(amount,7);
    int low = lowest_amount(amount,7);
    float average = average_amount(amount,7);
    print_data(names, id, amount, size);
    printf(" %s, has the highest amount $%.2f in the account number %d ",*(names + high), *(amount + high), *(id + high));
    printf(" %s has the lowest amount $%.2f in the account number %d ", *(names + low), *(amount + low), *(id + low));
    printf(" The average amount is $%.2f in the account number ", average);
    printf(" ");
    printf("Do you want to: ");
    printf(" 1. Withdraw ");
    printf(" 2. Deposit ");
    int choice;
    scanf("%d", &choice);
    printf(" Enter the amount: ");
    int wd;
    scanf("%d", &wd);
    if(choice == 1){
   (*(amount - i) -= wd);
   if( wd > *(amount + i)){
        printf("Not Enough Funds ");
   }
    }
    if(choice == 2){
   (*(amount + i) += wd);
    }

    write_data(targetname, names, id, amount, 7, high, low, average);
    free(amount);
    free(names);
    free(targetname);
    free(id);
    return 0;
}
int load_data(char* fname, char** names, int* id, float* amount, int size)/*this function will open the file -> if unable return 0 /--> otherwise will load the account information from text file*/{
    FILE *cfptr = fopen(fname, "r");
    if( (cfptr) == NULL){
   return 0;
    }
    int i = 0;
    for(i = 0; i< size; i++){
   fscanf(cfptr, "%s %d %f", *(names + i), &(*(id + i)), &(*(amount + i)));
    }
    fclose(cfptr);
    return 1;
}
void print_data(char** names, int* id, float* amount, int size){
    int i;

    for( i = 0; i < size; i++){
   printf("%s %d %.2f ", *(names + i),*(id + i), *(amount+ i));
    }
  
}

int highest_amount(float* amount, int size)/* function takes the float pointer and number of accounts, if finds highest amount and returns the index correspoinding to the highest number*/{
    int i=0;
    float largest;
    int max_index = 0;
    largest = *(amount + 0);
    for(i = 0; i < size; i++){
   if(*(amount+i) > largest){
        largest = *(amount+i);
        max_index = i;
   }
    }
    return max_index;
}
int lowest_amount(float* amount, int size)/* function takes the float pointer and number of accounts, if it finds lowest amount => returns the indez corresponding to the lowest number*/{
    int i=0;
    float smallest;
    int min_index = 0;
    smallest = *(amount + 0);
    for(i = 0; i < size; i++){
   if(*(amount+i) < smallest){
        smallest = *(amount + i);
        min_index = i;
   }
    }
    return min_index;
}
float average_amount(float *amount, int size)/* function takes the float pointer and number of accounts, if it summing them all up => returning the average for all the accounts*/{
    int i=0;
    float average;
    int sum = 0;
    for(i = 0; i < size; i++){
   sum += *(amount+i);
    }
    average = (float)sum / size;
    return average;
}


void write_data(char* fname, char** names, int* id, float* amount, int size, int high, int low, float average)/* this function writes the account information(name, account numbers, and amounts), the highest, the lowest and the average amount information into a text */{
    FILE *cfptr = fopen(fname, "w");
    int i = 0;
    for(i = 0; i< size; i++){
   fprintf(cfptr, "%s %d %.2f", *(names + i), *(id +i), *(amount +i));
   fprintf(cfptr, "the highest amount %.2f in the account number %d", *(amount + i), *(id + i));
   fprintf(cfptr, "the lowest amount %.2f in the account number %d", *(amount + i), *(id + i));
   fprintf(cfptr, "The average amount is %.2f", average);
    }
    fclose(cfptr);
}
int check_account( char** names, int *id, char* targetname, int tar, int size)/* searches for the given account number and name and if it finds then return the index of the name and account number otherwise returns -1 */{
    int i;
    for(i = 0; i < size; i++){
   if(string_compare(targetname, *(names + i)) && *(id + i) == tar)
   {
        return i;
   }

    }
    return -1;

}
int string_compare(char* s1, char* s2){
  
    while(*s1 != ''){
   if(*s1 != *s2){
        return 0;
   }
    s1++;
    s2++;
    }
    return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote