Question

In: Computer Science

Create a program that reads a file of 2D coordinates and calculates the bounding box and...

Create a program that reads a file of 2D coordinates and calculates the bounding box and center of the bounding box. The bounding box is defined as the minimum area that fully encompasses all the coordinates. The center of that bounding box is calculated by taking the mean of the bounding box coordinates: ( x1+x2 2 , y1+y2 2 ). • If the input file cannot be opened, warn the user by printing "CANNOT OPEN FILE." to stdout. • Print the resulting bounding box and center to stdout using the format string %.2f,%.2f,%.2f,%.2f,%.2f,%.2f, following the pattern xmin, ymin, xmax, ymax, xcenter, ycenter. • Save your code as prob2.c.

Example Run

$ ./a.out keypoints.txt

0.00,0.00,100.00,100.00,50.00,50.00

Solutions

Expert Solution

CODE -

#include<stdio.h>

int main(int argc, char** argv)

{

    // Open file using file name passed as command line argument

    FILE* infile = fopen(argv[1], "r");

    // Display error message if file can't be opened.

    if(!infile)

        printf("\nCANNOT OPEN FILE.\n");

    else

    {

        float x, y, xmin = 99999, ymin = 99999, xmax = -99999, ymax = -99999, xcenter, ycenter;

        // Read file till end of file is reached

        while(!feof(infile))

        {

            // Read x and y coordinate seperated by space in the file

            fscanf(infile, "%f %f", &x, &y);

            // Determine the coordinates of bounding box

            if (x < xmin)

                xmin = x;

            if(y < ymin)

                ymin = y;

            if (x > xmax)

                xmax = x;

            if(y > ymax)

                ymax = y;

        }

        // Determine the center coordinates of bounding box

        xcenter = (xmin + xmax) / 2;

        ycenter = (ymin + ymax) / 2;

        // Display the coordinates of bounding box and coordinates of its center

        printf("%.2f,%.2f,%.2f,%.2f,%.2f,%.2f", xmin, ymin, xmax, ymax, xcenter, ycenter);

    }

    return 0;

}

SCREENSHOTS -

INPUT TEXT FILE -

CODE -

OUTPUT -

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


Related Solutions

Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
2. Create a java program that reads a file line by line and extract the first...
2. Create a java program that reads a file line by line and extract the first word.        The program will ask for the name of input and output file. Input file: words.txt today tomorrow sam peterson peter small roy pratt Output file: outwords.txt tomorrow peterson small pratt
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
Java: create a program that reads in a piece of DNA sequence from a sequence file...
Java: create a program that reads in a piece of DNA sequence from a sequence file (dna.seq) (alternatively you can use the getRandomSeq(long) method of the RandomSeq class to generate a piece of DNA sequence), and then print out all the codons in three forward reading frames. Design a method called codon() that can be used to find all the codons from three reading frames. The method will take in an argument, the reading frame (1, 2, or 3), and...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
The following program is supposed to read 10 pairs of 2D coordinates, (xi , yi ),...
The following program is supposed to read 10 pairs of 2D coordinates, (xi , yi ), and print the average coordinate. However, it does not work correctly. Fix the program so that it compiles with no errors or warnings and correctly computes the average coordinate. int main() { float coords[9][2] = { 0 }; float sum[2]; while (i < 2) { scanf("%d%d", &coords[i][0], coords[i][1]); sum[0] += coords[i][0]; sum[1] += coords[i][1]; } printf("Average coordinate: (%.2d, %.2d)\n", sum[0], sum[1]); return 0; }...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT