Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need your help with my C++ program to find the Average Computation values and symbol for each city from the txt files and display

I need your help with my C++ program to find the Average Computation values and symbol for each city from the txt files and display them in the output. I managed to display the city name and id. You are not allowed to hard code as the coordinates and values can be changed. You are also not allowed to use vector for this if its possible. I have included the txt files and my codes below, you can edit in function3(). Refer to below for explanation.

readfile.txt

image text in transcribed

// The range of 'horizontal' indices, inclusive // E.g. if the range is 0-4, then the indices are 0, 1, 2, 3, 4 GridX_Range=0-8

// The range of 'vertical' indices, inclusive // E.g. if the range is 0-3, then the indices are 0, 1, 2, 3 GridY_Range=0-8

// [x,y] grid-areas which are occupied by cities city.txt

// each [x,y] grid-area cityValues.txt

--------------------------------------------------------------------------------------------------------------

city.txt

image text in transcribed

[1, 1]-3-Big_City [1, 2]-3-Big_City [1, 3]-3-Big_City [2, 1]-3-Big_City [2, 2]-3-Big_City [2, 3]-3-Big_City [2, 7]-2-Mid_City [2, 8]-2-Mid_City [3, 1]-3-Big_City [3, 2]-3-Big_City [3, 3]-3-Big_City [3, 7]-2-Mid_City [3, 8]-2-Mid_City [7, 7]-1-Small_City -----------------------------------------------------------------

cityValues.txt

[0, 0]-41 [0, 1]-93 [0, 2]-90 [0, 3]-24 [0, 4]-70 [0, 5]-39 [0, 6]-47 [0, 7]-35 [0, 8]-83 [1, 0]-38 [1, 1]-66 [1, 2]-45 [1, 3]-11 [1, 4]-53 [1, 5]-35 [1, 6]-88 [1, 7]-75 [1, 8]-21 [2, 0]-56 [2, 1]-81 [2, 2]-34 [2, 3]-76 [2, 4]-53 [2, 5]-44 [2, 6]-70 [2, 7]-38 [2, 8]-32 [3, 0]-86 [3, 1]-13 [3, 2]-23 [3, 3]-93 [3, 4]-68 [3, 5]-26 [3, 6]-53 [3, 7]-52 [3, 8]-29 [4, 0]-76 [4, 1]-60 [4, 2]-43 [4, 3]-82 [4, 4]-40 [4, 5]-72 [4, 6]-48 [4, 7]-29 [4, 8]-75 [5, 0]-16 [5, 1]-49 [5, 2]-36 [5, 3]-53 [5, 4]-18 [5, 5]-47 [5, 6]-27 [5, 7]-98 [5, 8]-78 [6, 0]-68 [6, 1]-63 [6, 2]-33 [6, 3]-92 [6, 4]-27 [6, 5]-48 [6, 6]-13 [6, 7]-15 [6, 8]-37 [7, 0]-47 [7, 1]-3 [7, 2]-8 [7, 3]-17 [7, 4]-62 [7, 5]-62 [7, 6]-14 [7, 7]-35 [7, 8]-84 [8, 0]-7 [8, 1]-23 [8, 2]-63 [8, 3]-24 [8, 4]-37 [8, 5]-18 [8, 6]-44 [8, 7]-6 [8, 8]-18

------------------------------------------------------------------------------------------

Below is the diagram to aid your understanding.

image text in transcribed

Bright Yellow grid areas indicate the actual grid area occupied by a city.(e.g. [1,1])

Light Yellow grid areas indicate the grid areas surrounding the occupied city. For example above, the coordinate[2,8] has following grid areas surrounding its perimeter: [1,7],[1,8],[2,7],[3,7],[3,8].

The grid areas [1,9],[2,9],[3,9] are not shown as they are beyond the upper limits of the vertical grid range. As a result, they are not included in the Average computation. Refer to below for calculation.

For each coordinate listed in the city.txt file, the Average computation value is derived by the following: AC = SUM(city value + surrounding grid areas)/Total no of grid areas

For example, for the coordinate [2,8]: AC = (32 + (21+75+38+52+29))/6 = 41.17

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Below are my codes:

#include #include #include #include

using namespace std;

//function delcarations void function1(); void function2(); void function3(); void mainMenu();

//global variables string linestring; string position; string GridXRange; int GridXRangeValue; int GridYRangeValue; string GridYRange; string cityFile; //city string cityValuesFile; //cityValues int lineCount; ifstream inData; vector tokenStringVector;

//String tokenization function vector tokenizeString (string input, string delimiter) { size_t pos = 0; string token; vector result;

while((pos = input.find(delimiter)) != string::npos) { token = input.substr(0, pos);

result.push_back (token);

input.erase(0, pos + delimiter.length()); } result.push_back (input); return (result); }

//main int main() { mainMenu(); return 0; }

//function 1 void function1() { string configFile;

cout

cin>>configFile;

//open input file inData.open(configFile);

while (!inData) //while filename does not exist { cout

cin >> configFile;

inData.open(configFile); }

//removes empty lines and comments and pushes all other information in the result vector string line; vector result;

while (getline(inData, line)) { if(line[0] == '/' && line[1] == '/') continue; // Ignore comment lines

if(line.size()

result.push_back(line); // Push all other lines to the vector }

// Now we extract the values from the result array GridXRange = result[0] ; GridYRange = result[1]; cityFile = result[2]; cityValuesFile = result[3];

inData.close();

//we tokenize the line of strings in the text file to get only the specific data tokenStringVector = tokenizeString (GridXRange, "=");//GridX_IdxRange=0-8

GridXRange = tokenStringVector[1];//0-8

cout

tokenStringVector = tokenizeString (GridXRange, "-");

GridXRangeValue = stoi(tokenStringVector[1]);//8

cout

tokenStringVector = tokenizeString (GridYRange, "=");//GridY_IdxRange=0-8

GridYRange = tokenStringVector[1];//0-8

cout

tokenStringVector = tokenizeString (GridYRange, "-");

GridYRangeValue = stoi(tokenStringVector[1]);//8

cout

cout

cout

mainMenu(); }

void function2() { //open input file "city.txt" inData.open(cityFile);

lineCount = 0;

while(getline(inData, linestring)) { lineCount++; } inData.close();

string cities[lineCount]; int cityIDs[lineCount];

inData.open(cityFile);//open the file again int j = 1; for (int i = 0; i

tokenStringVector = tokenizeString (linestring, "-");

cityIDs[i] = stoi(tokenStringVector[1]);//3

cities[i] = tokenStringVector[2];//Big_City

//repeat this output for each city cout

} inData.close(); cout to go back to main menu"; cin.ignore(); cin.get(); }

void function3() { cout

void mainMenu() { int num_choice;

cout

cout>num_choice;

switch(num_choice) { case 1: function1(); break;

case 2:

function2(); mainMenu(); break;

case 3: function3(); break;

default: cout

------------------------------------------------------------------------------------------------------------------------------------------

The L, M or H symbol for the Average Computation as below:

image text in transcribed

------------------------------------------------------------------------------------------------------------------------------------------------

Desired output when user enter option 2: (Repeat this for the rest of the cities in the city.txt file)

image text in transcribed

.......

readfile - Notepad File Edit Format View Help // The range of 'horizontal' indices, inclusive // E.g. if the range is 0-4, then the indices are 0, 1, 2, 3, GridX_Range=0-8 4 // The range of 'vertical' indices, inclusive // E.g. if the range is 0-3, then the indices are 0, 1, 2, 3 GridY_Range=0-8 // [x,y] grid-areas which are occupied by cities city.txt // each [x,y] grid-area cityValues.txt city - Notepad File Edit Format View Help [1, 1]-3-Big_City [1, 2]-3-Big_City [1, 3]-3-Big_City [2, 1]-3-Big_City [2, 2]-3-Big_City [2, 3]-3-Big_City [2, 7)-2-Mid_City [2, 8] -2-Mid_City [3, 1)-3-Big_City [3, 2]-3-Big_City [3, 3] -3-Big_City [3, 7]-2-Mid_City [3, 8]-2-Mid_City [7, 7]-1-Small_City 8 83 21 132 29 75 78 37 84 18 Coordinate (2,8] is: 32 7 35 75 38 52 29 98 15 35 6 Coordinate (7,7]: 35 6 47 88 70 53 48 27 13 14 44 5 39 35 44 26 72 47 48 62 18 4 70 53 53 68 40 18 27 62 37 3 24 11 76 93 82 53 92 17 24 2 90 45 34 23 43 36 33 8 63 1 93 66 81 13 60 49 63 3 23 0 41 38 56 86 76 16 68 47 7 0 1 2 3 4 5 6 7 8 Coordinate [1,1] is: 66 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How does a pension plan differ from a profit-sharing plan?

Answered: 1 week ago

Question

Show the properties and structure of allotropes of carbon.

Answered: 1 week ago