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

C Programming: here are the requirements: Write a program which expects the name

ID: 3854718 • Letter: C

Question

C Programming:

here are the requirements:

Write a program which expects the name of an input file and an output file to be given by the user. If the user does not input any names, default file names should be used, such as in.txt, and out.txt. The input files have lines which look like this:

North Carolina,Chapel Hill:70
The number after the state/city is a high temperature. Any state/city can be represented any number of times in the input file. Your program will create an output file which contains each city/state followed by the average high temperature to one decimal place such as:

North Carolina,Chapel Hill:76.4
Read data from the first input file into a dynamically allocated array of CITY structures. You may assume that the maximum size of a state/city string is 100. Display the array to the screen. The program should use the insertion sort algorithm to sort the data in state/city order. To demonstrate that the sorting algorithm works, display the array after sorting.

Create your own input file using the data shown below. On the first line in the input file provide the number of state/city lines.

Make sure that your program does not produce memory leaks. Memory leak detection is optional (see last page).

Run the program once and save the output at the end of the source file as a comment. Compress the source file, input and output files and upload the compressed file: 26B_LastName_FirstName_H1.zip
in.txt
27 // number of the line which will need to create a function of

Pennsylvania,Philadelphia:91

California,San Francisco:75

Nevada,Reno:108

Arizona,Flagstaff:81

California,Yreka:101

Arizona,Tucson:107

California,Los Angeles:78

California,Los Angeles:81

Pennsylvania,Pittsburgh:89

Oregon,Salem:90

California,Los Angeles:82

Arizona,Flagstaff:84

California,San Francisco:64

Oregon,Salem:83

California,San Francisco:68

Arizona,Tucson:99

California,Yreka:100

Arizona,Phoenix:109

Oregon,Portland:82

Arizona,Tucson:103

Oregon,Portland:79

Arizona,Phoenix:107

California,Cupertino:88

Oregon,Salem:85

Pennsylvania,Philadelphia:86

California,Los Angeles:97

Nevada,Reno:108

So this is what I have so far and I'm still so confused:

#include <stdio.h>
#include <stdlib.h>

#ifdef _MSC_VER
#include <crtdbg.h> // needed to check for memory leaks (Windows only!)
#endif

int main( void )
{
FILE * fPointer;
fPointer = fopen("in.txt", "r");
char line[150];
while(!feof(fPointer)){
fgets(line, 150, fPointer);
puts(line);
}


fclose(fPointer);

#ifdef _MSC_VER
printf( _CrtDumpMemoryLeaks() ? "Memory Leak " : "No Memory Leak ");
#endif

return 0;
}

Explanation / Answer

//main.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>

#ifdef _MSC_VER
#include <crtdbg.h> // needed to check for memory leaks (Windows only!)
#endif

#define CITY_SIZE 100

/************************************/
/* */
/* state/city structure */
/* */
/************************************/
typedef struct {
char *name;
double temp;
} City;


void parse(char*, int, City *);
void sort(City*, int);
void swap (City*, int );
void makeOut(City* ptr, int length);//, FILE*);

int main( void )
{
char *inFileName;
// char *outFileName;
//
//
// printf("Enter the name of the input file.");
// fgets(inFileName, 11, stdin);
//
// printf("%s", inFileName);
  
  
FILE *myFile;
FILE *outFile;
int c;
int fileSize;
char line[100]; //temorary stack memory for fetchin data from file line by line
  

/*
printf("Enter the name of the input file.");
for (int i = 0 ; i < 10 ; i ++)
scanf("%c" , &inFileName[i]);
  
printf("Enter a name for the output file.");
for (int i = 0 ; i < 10 ; i ++)
scanf("%c" , &outFileName[i]);
*/
// fpData = fopen(inFileName, "r");
  
  
  
// outFile = fopen("myout.txt", "w");
  
  
myFile = fopen("/Users/in.txt", "r");
  
  
if (myFile == NULL)
{

printf("Error opening %s for reading" , inFileName);
exit(101);
}
  
  
//*********************
// *read*
//**********************
fgets(line, sizeof(line), myFile);
  
fileSize = atoi(line);
  
City* dataArr = malloc( fileSize * sizeof(City));
  
  
  
  
/************************************/
/* */
/* make the string */
/* */
/************************************/
int lineNum = 0;
  
while (c == fgetc(myFile) != EOF && lineNum < 23) //?????????
{
ungetc(c, myFile);
fgets(line, sizeof(line), myFile);
parse(line, lineNum, dataArr);
lineNum++;
}
  
  

sort(dataArr, fileSize);
  
printf("************ ");
for (int i = 0; i < fileSize; i++) {
  
printf("%s:%f ", dataArr[i].name, dataArr[i].temp);
}
  
  
makeOut(dataArr, fileSize);//, outFile);
fclose(myFile);
  
  
#ifdef _MSC_VER
printf( _CrtDumpMemoryLeaks() ? "Memory Leak " : "No Memory Leak ");
#endif

return 0;
}

/************************************/
/* */
/* parse */
/* */
/************************************/

void parse (char*line, int lineNum, City* ptr)
{
//find size of state/city string
int counter = 0;
while (line[counter] != ':')
counter++;
  
  
  
ptr[lineNum].name = malloc(counter + 1);
  
  
sscanf(line, "%[^:]%*c%lf", ptr[lineNum].name, &ptr[lineNum].temp);


printf("%s ", ptr[lineNum].name);
printf("%lf ", ptr[lineNum].temp);

}

/************************************/
/* */
/* Insertion Sort */
/* */
/************************************/
void sort (City* ptr, int length)
{
for (int i = 1; i < length ; i++) {
for (int j = i; j > 0; j--) {
if ( strcmp(ptr[j].name, ptr[j - 1].name) > 0 )
swap (ptr, j);
}
}
}


/************************************/
/* */
/* Swap */
/* */
/************************************/
void swap (City* ptr, int j)
{
City temp;
  
temp = ptr[j];
ptr[j] = ptr[j - 1];
ptr[j - 1] = temp;
}

/************************************/
/* */
/* make output file */
/* */
/************************************/
void makeOut(City* ptr, int length)//, FILE* outFile)
{
int i = 0;
int count = 0;
double sum = 0;
double avg;
char outStr[100];

while (i < length)
{int j = i + 1;
sum = ptr[i].temp;
count = 1;
while (j < length && (strcmp(ptr[j].name, ptr[j - 1].name) == 0))
{
sum += ptr[j].temp;
count++; // how many similiar states (to divide for average)
j++;
}
  
avg = sum / count;
printf("name:%s, sum: %f count: %d avg: %f ", ptr[i].name,sum,count ,avg);

count = 0;
i = j;
  
  
sprintf(outStr, "%s:%f", ptr[i].name , avg);
// fprintf(outFile, "%f ", avg);
}

}

===========================================================================