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

c++ You are an intern at an engineering firm and having heard you are taking CS

ID: 645308 • Letter: C

Question

c++

You are an intern at an engineering firm and having heard you are taking CS 20A, your boss has come to ask for your help with a task.

The firm engineers often need to compare the output of sensors measuring the location of points on a 3D grid to determine if any of these locations have shifted. She needs you to write a program that takes in a file containing point records as measured by a sensor and compare it to another file containing point records as measured by that sensor at a later date. The program outputs all the points in the first file that have a different location in the second file, sorted alphabetically by the point tag. The point tag uniquely identifies a point. Note that not every point in the first file has to be in the second file. Also, the second file may have points that are not in the first.

This sounds like a very simple task and you immediately agree to work on it. She warns you that the files are pretty large - a few hundred thousand point records. You inform her that with today's computers having processors of a few GHz this task is a piece of cake and can be computed in an instant.

Part 1: You think about the problem for a minute and you come up with a plan. - You will create a Point class representing point records. It will have the data members below corresponding to the data contained in the sensor output. The header file has been provided to you and cannot be modified.
string tag; int x; int y; int z;

- You will implement operator = = to consider two point objects as equal if they have the same tag.

- You will implement operator < to allow sorting of point objects alphabetically by tag.

- You will implement the toString member function to print the fields as they appear in the records file.

- You will implement the equalCoordinates member function to detect whether a point has moved

The idea is that for every point you read from the file, you will create a Point object. You will store these records on 2 arrays. Parts 2 and 3 operate on these arrays.

Part 2: Your data structures are the two arrays from Part 1, each containing the points from each sensor output. For each point in the first array you will do a linear search on the second array to find that point. If the point is found and the point's location has changed, you will add that point to the moved points array. Once you have the array of the moved points, you sort it by point tag and print it to a file. We will use the C++ Standard Template Library (STL) sort for this (already done for you in main()). Note that the sort will be alphabetical and not numerical, eg Point1, Point10, Point100, ...Point2, Point200,

Explanation / Answer

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

/**

Processes a pixel by forming the negative.

@param blue the blue value of the pixel

@param green the green value of the pixel

@param red the red value of the pixel

*/

void process(int& blue, int& green, int& red)

{

blue = 255 - blue;

green = 255 - green;

red = 255 - red;

}

/**

Gets an integer from a binary stream.

@param stream the stream

@param offset the offset at which to read the integer

@return the integer starting at the given offset

*/

int get_int(fstream& stream, int offset)

{

stream.seekg(offset);

int result = 0;

int base = 1;

for (int i = 0; i < 4; i++)

{

result = result + stream.get() * base;

base = base * 256;

}

return result;

}

int main()

{

cout << "Please enter the file name: ";

string filename;

cin >> filename;

fstream stream;

// Open as a binary file

stream.open(filename.c_str(), ios::in | ios::out | ios::binary);

int file_size = get_int(stream, 2); // Get the image dimensions

int start = get_int(stream, 10);

int width = get_int(stream, 18);

int height = get_int(stream, 22);

// Scan lines must occupy multiples of four bytes

int scanline_size = width * 3;

int padding = 0;

if (scanline_size % 4 != 0)

{

padding = 4 - scanline_size % 4;

}

if (file_size != start + (scanline_size + padding) * height)

{

cout << "Not a 24-bit true color image file." << endl;

return 1;

}

stream.seekg(start); // Go to the start of the pixels

for (int i = 0; i < height; i++) // For each scan line

{

for (int j = 0; j < width; j++) // For each pixel

{

int pos = stream.tellg(); // Go to the start of the pixel

int blue = stream.get(); // Read the pixel

int green = stream.get();

int red = stream.get();

process(blue, green, red); // Process the pixel

stream.seekp(pos); // Go back to the start of the pixel

stream.put(blue); // Write the pixel

stream.put(green);

stream.put(red);

}

stream.seekg(padding, ios::cur); // Skip the padding

}

return 0;

}

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