Question

Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of gray for each cell in the map Produce the output file in the specified format (PPM) Use an online free tool to convert your PPM file into a JPG file to visually check your results Step 1 - Read the data into a 2D array Your first goal is to read the values into a 2D array (i.e., a matrix, elevations in the start code) of ints from a data file. This data file contains, in plain text, data representing the average elevations of patches of land (roughly 700x700 meters) in the US. The user of your program will provide as input three values from standard in: The number of rows in the map; (The height of the image to be produced.) The number of columns in the map; (The width of the image to be produced.) The name of the file containing the map data. You should validate the inputs. If there is an error, output the error as formatted below and exit the program immediately. Error: Problem reading in rows and columns. Error: Unable to open file . After reading these data items from the user, you should implement a function to output the elevation data to check that you read the values correctly. We provide several sample data files containing elevation data for most of the state of Colorado (mountains!). Other input files can be obtained from the National Oceanic and Atmospheric Administration (http://maps.ngdc.noaa.gov/viewers/wcs-client/). A data file comes as one large, space-separated list of integers. A file representing a 480-row by 844-col grid contains 405,120 integers (480*844). Each integer is the average elevation in meters of each cell in the grid. The data is listed in row-major order, i.e., first the 844 numbers for row 0, then the 844 numbers for row 1, etc. You should validate that you got the correct number of values from the file. If there is an error, output the error as formatted below and exit the program immediately. Error: Read a non-integer value. Error: Problem reading the file. Error: End of file reached prior to getting all the required data. Error: Too many data points. Hints/Warnings: The data is given as a continuous stream of numbers - there may be no line breaks in the file. You may want to make sure that you are reading the data correctly before you move to the next step of this project. Implement a function to print your map data to standard out and try out your code with a file containing a small map first (you can create an input file corresponding to 4 rows and 4 columns, for example.) Then check what happens when you read the sample map data files: (100 by 100, 480 by 480, 844 by 480). Note that failbit and eofbit can be on at the same time. Just because eofbit is set, does not mean that the previous read did not succeed. Elevations can be negative if they are below sea level. You can download the example map data files (shown below) packaged together (in zip format) via: map-inputs.zip Step 2 - Find the min and max values In order to draw the map with a grayscale that indicates the elevation, you will need to determine which scale to use, i.e., which will be shown as a dark area (with low elevation) and which ones will be shown as bright areas (high elevation). This means you need to find the min and max values in the map first, so that you know which values to associate with the brightest and darkest colors. You can compute the min and max elevation values by writing the findMaxMin function that scans the entire map data and keeps track of the smallest and largest values found so far. Test this functionality to make sure you’re getting the correct values for min and max before you proceed to implement the rest of the program. You may want to check with a friend or colleagues in the Piazza forum to see if other students are getting the same min and max values for the provided input files. Step 3 - Compute the color for each part of the map and store For each value in the array of elevation data, you will calculate the shade of grey that corresponds to that elevation point using the scaleValue function that uses the following equation: Use the min and max elevations calculated from Step 2. Also, round the value you get to the nearest whole number since RGB values are integers. Implement the loadGreyscale function to go through all of the elevations and put the greyscale value into the parallel image array. Each element of the Pixel struct (red, green, and blue) should be assigned the same value since using the same value for R, G and B will result in grey. The structure of the array should mirror the array with the elevation data. This structure is required in the next part of the homework. If you use an alternative structure, then you will have to redo that part in the next homework. Search the zyBook for more information about parallel arrays searching for “multiple arrays”. Unfortunately, the zyBook uses this more ambiguous term. Step 4 - Produce the output file in the PPM format PPM (portable pixel map) format is a specification for representing images using the RGB color model. PPM is not used widely because it is very inefficient (for example, it does not apply any data compression to reduce the space required to represent an image.) But PPM is very simple, and there are programs available for Windows, Mac, and Linux that can be used to view ppm images. Even more conveniently, you can use an online tool with your browser to convert a PPM file into a widely used format such as JPG. We will be using Plain PPM, which has the information in text format (readable as decimal numbers instead of binary.) You will implement the outputImage function to write the PPM data to the output file. In Step 3, you computed the RGB values for the shades of gray. All you will need to do to get a PPM image for these RGB values is to write a preamble before writing the RGB numbers into a file. We will follow the convention that if the input file is named input.dat, then your program will generate a file named input.dat.ppm A PPM file has the following format: First line: string “P3” Second line: width (number of columns) and height (number of rows) Third line: max color value (for us, 255) Rest of the file: list of RGB values for the image, expressed as a raster of rows, from top to bottom. Each row contains the RGB values (i.e., three values) for each column. Example: Note: We have added colors showing that every three numbers represent a single pixel. P3 4 4 255 0 0 0 255 0 0 0 0 0 0 255 0 255 255 255 255 0 255 0 0 0 0 255 0 255 255 0 0 0 255 125 0 255 255 0 125 0 0 255 255 255 0 125 125 125 239 239 239 //functions file #include #include #include #include "functions.h" using namespace std; void findMaxMin(const int elevations[MAX_ROWS][MAX_COLS], int rows, int cols, int& max, int& min) { } void loadData(int elevations[MAX_ROWS][MAX_COLS], int rows, int cols, istream& inData) { } int scaleValue(int value, int max, int min) { return 255; } void loadGreyscale(Pixel image[MAX_ROWS][MAX_COLS], const int elevations[MAX_ROWS][MAX_COLS], int rows, int cols, int max, int min) } void outputImage(const Pixel image[MAX_ROWS][MAX_COLS], int rows, int cols, ostream& outData) { } //main file #include #include #include "functions.h" using namespace std; // Normally you should not use global variables. However, the stack is // limited in size and will not allow arrays of this size. We'll learn // later how to put these arrays on the heap instead. Regardless, for now // we'll use these and treat them as if they were declared in main. So // when used in functions, we'll pass them as we would any other array. static int elevations[MAX_ROWS][MAX_COLS]; static Pixel image[MAX_ROWS][MAX_COLS]; int main() { }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <vector>
#include <fstream>
#include <limits.h>

int main()
{
int r,c;
int min_val = INT_MAX, max_val = INT_MIN;

std::string fileName;
// Accepting 3 input from the user as mentioned
std::cout << "Enter number of rows: ";
std::cin >> r;
std::cout << "Enter number of columns: ";
std::cin >> c;
std::cout << "Enter name of file: ";
std::cin >> fileName;

// Naming output file as mentioned
std::string outFileName = fileName+".ppm";

std::vector<int> v;
std::vector<std::vector<int> > data;
std::ifstream f(fileName.c_str());

// Checking if file is available
if(!f.is_open())
{
std::cerr << "Error: Could not open file." << std::endl;
return -1;
}

int n;
while(f >> n)
{
if(n < min_val)
min_val = n;
if(n > max_val)
max_val = n;

v.push_back(n);
if(v.size() == c)
{
data.push_back(v);
v.clear();
}
}

// Checking if exactly the required amount of data is available in file
if(data.size() != r && v.size() != 0)
{
std::cerr << "Error: Recieved lesser or more data values than expected." << std::endl;
return -1;
}

std::vector<std::vector<int> > R(r);
std::vector<std::vector<int> > G(r);
std::vector<std::vector<int> > B(r);

double val;

for(int i=0; i<data.size(); i++)
{
for(int j=0; j<data.at(i).size(); j++)
{
// Calculating grayscale values
if(max_val == min_val)
val=0;
else
val = (data.at(i).at(j) - min_val)*255.0/(max_val - min_val);

R.at(i).push_back((int)val);
G.at(i).push_back((int)val);
B.at(i).push_back((int)val);
}
}

std::ofstream of(outFileName.c_str());
// Checking if output file is made
if(!of.is_open())
{
std::cerr << "Error: Could not open output file for writing." << std::endl;
return -1;
}

// Output in PPM format
of<<"P3"<<std::endl;
of<<r<<" "<<c<<std::endl;
of<<255<<std::endl;

for(int i=0; i<data.size(); i++)
{
for(int j=0; j<data.at(i).size(); j++)
of<<R.at(i).at(j)<<" "<<G.at(i).at(j)<<" "<<B.at(i).at(j)<<" ";
of << std::endl;
}
}

output:-

Sample input:
Enter number of rows: 4
Enter number of columns: 4
Enter name of file: data.dat

Contents of data.dat
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Add a comment
Know the answer?
Add Answer to:
Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...

    Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of loops and conditional statements Description The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 – 9 exactly The sum of each row, each column and each diagonal all add up to the same number. s is shown below: Write a program that...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • C Programming, getting data from a file and computing them into 5 parallel arrays so that...

    C Programming, getting data from a file and computing them into 5 parallel arrays so that they can be used for later calculation. Also please avoid using structures at all costs for this code as they have not yet been covered. # Melbourne Central Daily Pedestrian Counts day 2 2009 2009 2009 06 06 06 01 02 03 daycount 22663 22960 23618 4 2018 2018 02 02 27 28 33722 33164 4 There will always be two heading lines in...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have...

    The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have been placed above a conveyer belt to enables parts on the belt to be photographed and analyzed. You are to augment the system that has been put in place by writing C code to detect the number of parts on the belt, and the positions of each object. The process by which you will do this is called Connected Component Labeling (CCL). These positions...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT