Question

In: Computer Science

Using C programming I have a file that contains earthquake data that I will copy and...

Using C programming

I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data.

example file to sort:

time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net
2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci
2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci
2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci
2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak

Solutions

Expert Solution

CODE -

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

// Structure to store various contents of the file

struct earthquake

{

    char beg[100];      // To store the content of file before latitude

    float latitude;     // To store the latitude

    char end[200];      // To store the contents of the file after the latitude part

} quakes[100];

int main()

{

    // Opening files

    FILE *infile = fopen("earthquake.txt", "r");

    FILE *outfile = fopen("sorted_quake.txt", "w");

    // Creating a temporary struct of the type earthquake

    struct earthquake temp;

    char ch;

    int i=0;

    char header[100];

    // Reading the header i.e. column names from the file

    fscanf(infile, "%s", header);

    // Writing the header to the output file

    fprintf(outfile, "%s\n", header);

    ch = fgetc(infile);

    char line[200];

    // Loop to run till end of file is reached

    while(ch != EOF)

    {

        // Reading a line of the file

        fscanf(infile, "%s", line);

        

        // Splitting the line into three parts by first and second comma

        // and storing the parts into respective elements of the structure

        strcpy(quakes[i].beg, strtok(line, ","));

        quakes[i].latitude =  atof(strtok(NULL,","));

        strcpy(quakes[i].end, strtok(NULL, ""));

        ch = fgetc(infile);

        i++;

    }

    int size = i;

    // Bubble sort implementation to sort the earthquakes data in ascending order based on latitude.

    for(i=1; i<size; i++)

        for(int j=0; j<size-i; j++)

            if (quakes[j+1].latitude < quakes[j].latitude)

            {

                temp = quakes[j];

                quakes[j] = quakes[j+1];

                quakes[j+1] = temp;

            }

    

    // Writing the sorted data of earthquakes to the output file.

    for(i=0; i<size; i++)

        fprintf(outfile, "%s,%f,%s\n", quakes[i].beg, quakes[i].latitude, quakes[i].end);

    

    return 0;

}

SCREENSHOTS -

INPUT TEXT FILE -

CODE -

OUTPUT TEXT FILE -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

assignment in C I have a file that contains a lot of lines with words separated...
assignment in C I have a file that contains a lot of lines with words separated by spaces ( also contains empty lines as well). I need to read this file line by line and put each word into 2d array. NOTE: i need to skip spaces as well as empty lines. also I need to count each word.
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
Systems Programming - File Operations in Linux using C Preventing copying the same file What does...
Systems Programming - File Operations in Linux using C Preventing copying the same file What does the standard cp do if you try to copy a file onto itself? $ cp file1 file1 cp: 'file1' and 'file1' are the same file Modify cp1.c to handle the situation and include comments. /** * @file cp1.c * @brief Uses read and write with tunable buffer size * usage: cp1 src dest */ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
C programming A small company provided you three files. 1) emp.txt : this file contains list...
C programming A small company provided you three files. 1) emp.txt : this file contains list of employees where each line represents data of an employee. An employee has an id (String max length 20), last name (string max length 100), and salary (int). See the example emp.txt file. 2) dept.txt: This text file contains list of departments of the employees. Each line of the file contains an employee id (string max length 20) and department name for that employee...
Using C Programming. Put all of these 4 things in one source file and attach to...
Using C Programming. Put all of these 4 things in one source file and attach to this question. Put #1 in main. Put all the others into separate function functions but in the same file.   1)   Put this code in main. You are writing a program for Bowl Me Over, a local bowling alley. The program will allow staff to enter any number of bowling scores. Scores in a standard game range between 0 to 300, with being a perfect...
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
a) Submit a copy of your dataset along with a file that contains your answers to...
a) Submit a copy of your dataset along with a file that contains your answers to all of the following questions. b) What the mean and Standard Deviation (SD) of the Close column in your data set? c) If a person bought 1 share of Google stock within the last year, what is the probability that the stock on that day closed at less than the mean for that year? Hint: You do not want to calculate the mean to...
Systems Programming - File Operations Create your version of the tail command in Linux using C...
Systems Programming - File Operations Create your version of the tail command in Linux using C The lseek system call allows you to move the current position anywhere in a file. The call lseek(fd, 0, SEEK_END) moves the current position to the end of the file. The tail command displays the last ten liens of a file. Try it. tail has to move, not to the end of the file, but to a spot ten lines before the end of...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled library file during linking? Where are the C++ header files and compiled C++ library files on your computer?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT