Question
I need help finishing this assignment i've started. I need to implement a .h file, and i need to open and pass a file to
I need help finishing this assignment i've started. I need to implement a .h file, and i need to open and pass a file to my program which then prints out in .txt file. If you could add comments and explain the things that you put into my code to help me that'd be awesome!
The assignment:
Objectives
Use pointers and arrays
Pass pointers through parameters
Dynamically allocate memory
Read/write to files
Hand-in Requirements
Save your source files in a folder named lastname-firstname-assignment2-4 replacing lastname with your last name and firstname with your first name (all lower-cased). You will have the following code files in your folder:
analyze-real-estate-data.c
analyze-real-estate-data.h
To submit, zip the file and send the zip file with the same naming convention (all lower-cased): lastname-firstname-assignment2-4.tar.gz
Background Information
Your program will read in this file and output the following summary statistics to an output file named analysis.txt
Number of homes sold
Average sales price of homes sold
Number of Residential, Condo, and Multi-Family homes
Average number of bedrooms OR number of bathrooms OR square footage
Process
At the command line, the user will execute the program and pass in the csv file's name:
./analyze-real-estate-data sacramentorealestatetransactions.csv
Make sure the CSV file is in the same folder as your executable. If the file does not exist or the argument is not given, just end the program. After the user hits enter, the summary statistics will appear in both standard output and printed to analysis.txt. The numbers have been removed.
Assignment 2-4 by Firstname Lastname
Number of Sales: XX
Average price of Sales: $XXXXXX.XX
Number of Residential: XX
Number of Condos: XX
Number of Multi Family: XX
Tasks
The program you will write for this assignment has two files: a C source file (.c) and a header file (.h). In the C source file,
Write a main function that processes one command line argument, which is the name of the input CSV file. Only the main function will have printf and fprintf statements.
Write a function that parses the CSV file and provides the counts and averages. This function takes in as parameters: the file pointer and variable pointers which will eventually hold the counts and averages. This function may not have have any printf nor fprintf statements. This function returns nothing.
IMPORTANT: The first row of the CSV file is a header. You will need to skip this row and not include it any counts.
There are definitely better ways in which you can write this program, especially in terms of modularity and reuse. Having all these functionalities in the same function allows us to practice with passing pointers through parameters.
Input Validation
For this assignment, you will need to validate the arguments:
If the first argument command line argument is not available or is not a file that can be opened, end the program. Print out a relevant error message.
My code:
#include
void parseCSV(FILE *fp, int *n, double *av, int *nr, int *nc, int *nf){
char line[100]; *n = 0; double sum = 0; *nc = 0; *nr = 0; *nf = 0; fgets((char *)&line, 100,fp); while(fgets((char *)&line, 100,fp) != NULL){ *n++; char *ch = strtok(line,","); int count = 0; while (ch != NULL){ if (count == 9){ sum = sum + atof(ch); } if (count == 7){ if (strcmp(ch,"Residential") == 0) *nr++; if (strcmp(ch,"Condo") == 0) *nc++; if (strcmp(ch,"Multi Family") == 0) *nf++; } } } *av = sum/(*n); }
int main(int argc , char *argv[]){
FILE *fp; int n; double av; int nr; int nc; int nf; if (argc != 2){ printf("Usage: ./analyze-real-estate-data filename.csv "); } fp = fopen(argv[1],"r"); if (fp == NULL){ printf("Errpr opening file "); return 0; } parseCSV(fp,&n,&av,&nr,&nc,&nf); printf("Number of Sales: %d ",n); printf("Average price of Sales: %.2f ",av); printf("Number of Residential: %.2f ",nr); printf("Number of Condos: %.2f ",nc); printf("Number of Multi Family: %.2f ",nf); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started