Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with a C++ problem! Tasks: Yourgoal is to take the broken c++ code, provided,and repair it. The line numbers listed here are for

Need help with a C++ problem!

Tasks: Yourgoal is to take the broken c++ code, provided,and repair it. The line numbers listed here are for the unaltered original busted code. There are a couple of questions embedded in the code that you will answer with comments embedded in your repaired code at the point in the code where the questions are asked. Those questions are found at lines 194 and 247. There is a simple formula to be reverse engineered at line 142. Another is missing at line 158. Two simple equations with missing formulas are at lines 173 and 174. You will need to derive the math equation needed to change 2D x,y coordinates into the 1D string coordinate system at line 188. you will need to fill in the missing parameters located at lines 143, 144, 145, 161, 162 and 163. there are more missing parameters at line 177. You will recreate the missing control logic of the for loops located at lines 159, 176, 218, 225, 232 and 240. You will add the following gridable functions to the capabilities: cosine, tangent,reciprocal of x,parabola and the ellipse. The final required item is, once your program is running, to successfully change the size of the grid through the provided menu system. Strings: These are the only string manipulators used in this project. Powerful member functions they are. create a string, g, that is int long filled with char characters string g(int,char) replace in the string g starting at position int a, take out int b number of chars, replace with int c number of characters, that are char characters. a b c g.replace(int,int,int,char) select a substring from g starting at position int a that is int b characters long. a b g.substr(int,int) Hint: Ifwewereyou,we wouldbuildadriverprogramtoindependentlyrepairthefunctionsgrid_newandgrid_show.What this means is that you would create a completely different program (main) in a completely different source file. Then cut and paste from the busted code the two busted functions back into your brand new driver program and go from there.

***This is the "skeleton" code provided*** *** line numbers from instructions may be slightly off from what is pasted here, but missing code should have the word "missing" where our input is needed***

//******************************************************** // Name: // ID: XXXXXX // Section: // Instructor: // Assignment: //******************************************************** // Program Summary // // this program maps functions, x and y coordinates, into // a string. the string can be displayed like a grid or graph // // you will deal with functions and call by reference parameters // demonstrate your ability to trace a program // you will show that you can work with For loops // you will reverse engineer missing items to demonstrate // your understanding of this grid program // you will add the ability to grid map Parabola // you will add the ability to grid map Cosine // you will add the ability to grid map 1/X, Reciprocal // you will add the ability to grid map Tangent // you will attempt the ability to grid map Ellipse

#include #include #include

using namespace std;

// menu headers int menu_c(string&); int menu_e(int&, string&); int menu_g(string&);

// function headers string grid_new(); void set_vars(int, int); void set_grid(string&); void grid_map(string&, int, int, char); void grid_map(string&, int, double, char); void grid_set_00 (string&, char); void grid_set_xax(string&, char); void grid_set_yax(string&, char); void grid_show(string&); void grid_res(string&);

// gridable function headers // add additional gridable equation functionality here void eq_init(int, string&, bool, bool); void line_init(); void line_g(string&); void sin_init(); void sin_g(string&);

// two sets of // global variables

// for the grid int xmin= 0; int xmax= 0; int ymin= 0; int ymax= 0;

// for the equations double a=1.0; double b=1.0; double c=1.0; string eq="";

// working portion of our project 2 starts here // don't mess with main() // // // int main() { set_vars(78, 24); // an X width by Y height grid string agrid = grid_new(); int doeq = 0; // active equation int doitem = 0; // flow control cout << " press q to exit at either menu "; while (doitem!=9) { switch (doitem) { case 0:doitem=2; eq_init(doeq, agrid, true, true); break; case 1:doitem=2; eq_init(doeq, agrid, false, true); break; case 2:doitem=3; grid_show(agrid); break; case 3:doitem=menu_c(agrid); break; case 4:doitem=menu_e(doeq, agrid); break; case 5:doitem=menu_g(agrid); break; default:; // just in case }} cout << " thanks for using project 2.0 "; return 0; }

// put an equation into the grid // if init is true then initialize the global eq variables // add new gridable equation functionality here void eq_init(int eq, string& g, bool init, bool run) { switch(eq) { case(0):if (init) line_init(); // global variables if (run) line_g(g); break; // show the equation case(1):if (init) sin_init(); if (run) sin_g(g); break; default:; } }

// menu for selection of the active equation // add new gridable equation functionality here int menu_e(int& doeq, string& g) { int doitem = 2; bool loop = true; char menu_pic = ' '; while(loop) { cout << "Select a function to put in the grid "; cout << "0\tLine "; cout << "1\tSine "; cin >> menu_pic; switch(menu_pic) { case '0':doeq=0; eq_init(doeq,g,true,false); loop=false; break; case '1':doeq=1; eq_init(doeq,g,true,false); loop=false; break; case '2':doeq=2; eq_init(doeq,g,true,false); loop=false; break; case 'q':doitem=9 ; loop=false; break; default: cout << "what? "; }} return doitem; }

// make a fresh grid // what is the missing formula? // what are the missing parameters? string grid_new() { string g(missing formula, ' '); grid_set_xax(missing parameters); // x axis grid_set_yax(missing parameters); // y axis grid_set_00 (missing parameters); // x=0,y=0 return g; }

// reset the grid // equation chars in the grid are replaced with ' ' // restore the horizontal axis // restore the vertical axis // restore the origin // reverse engineer the missing formula // reverse engineer the missing for loop items void grid_res(string& g) { int z=(missing formula); for(missing items){ g.replace(i, 1, 1, ' ');} grid_set_xax(missing parameters); // x axis grid_set_yax(missing parameters); // y axis grid_set_00 (missing parameters); // x=0,y=0 }

// put the grid on the screen // a "row" of the grid is couted to the screen followed by a " " // reverse engineer the missing formulas // reverse engineer the missing parameters // reverse engineer the missing for loop items void grid_show(string& g) { int y=(missing formula); // how tall? int x=(missing formula); // how wide? cout << " "; for(missing items){ cout << g.substr(missing parameters) << " ";} cout << " "; }

// put x,y data points into the grid // reverse engineer the missing equation void grid_map(string& g, int x, int y, char c) { if (y < ymin) ; // out of bounds check else if (y > ymax) ; // out of bounds check else{ missing equation; g.replace(gcoor, 1, 1, c); }}

// grid mapping with double to int type casting // we are function overloading here // is this necessary? or are we messing with you? // explain either way void grid_map(string& g, int x, double y, char c) { grid_map(g,x,static_cast(round(y)),c); }

// equation specific global variable initialization // add additional equation functionality here void line_init() {eq="A*x +B" ; a = -1.00 ; b = -1.00; c = 0.00;} void sin_init() {eq="A*Sin(B*x)" ; a = 12.00 ; b = 0.06; c = 0.00;}

// put the + at the center of the grid // don't mess with this void grid_set_00(string& g, char c) { grid_map(g, 0, 0, c); }

// fill in the x axis void grid_set_xax(string& g, char c) { for(missing items){ grid_map(g, x, 0, c); }}

// fill in the y axis void grid_set_yax(string& g, char c) { for(missing items){ grid_map(g, 0, y, c); }}

// map the line function into the grid void line_g(string& g) { for(missing items){ double y = (a * x) + b; grid_map(g, x, y, '.'); }}

// map the sin function into the grid void sin_g(string& g) { for(missing items){ double y = a * sin(b * x); grid_map(g, x, y, 's'); }}

// define the limits of your grid // don't mess with this // what is the purpose of the lines with %2 ? void set_vars(int xsiz,int ysiz) { // out of bounds tests for x axis (max 100) if (xsiz%2!= 0) xsiz=xsiz+1; if (xsiz >100) xsiz= 100; // out of bounds tests for y axis (max 100) if (ysiz%2!= 0) ysiz=ysiz+1; if (ysiz >100) ysiz= 100; // align x=0,y=0 to the center xmin = -(xsiz/2); ymin = -(ysiz/2); xmax = +(xsiz/2); ymax = +(ysiz/2); } // menu for adjusting the equation coefficients // don't mess with this int menu_c(string& g) { int doitem = 1; bool loop = true; char menu_pic = ' '; while(loop) { cout << "Current Eq: " << eq << " "; cout << "Change coefficients? "; cout << "A=" << a << " B=" << b << " C=" << c << " "; cout << "a\tchange A "; cout << "b\tchange B "; cout << "c\tchange C "; cout << "e\tequation selection "; cout << "g\tgrid size selection "; cout << "r\trun as is "; cout << "w\twipe the grid "; cin >> menu_pic; switch(menu_pic) { case 'a':cout <<"A=> "; cin >> a; break; case 'b':cout <<"B=> "; cin >> b; break; case 'c':cout <<"C=> "; cin >> c; break; case 'e':doitem=4; loop=false; break; case 'g':doitem=5; loop=false; break; case 'r': loop=false; break; case 'q':doitem=9; loop=false; break; case 'w':grid_res(g); grid_show(g); break; default: cout << "what? "; }} return doitem; } // menu for adjusting the grid size // don't mess with this int menu_g(string& g) { int doitem = 1; bool loop = true; char menu_pic = ' '; int xsiz, ysiz; while(loop) { cout << "Change Width and Height of grid "; cout << "x\tchange width currently:" << xmax-xmin <<" "; cout << "y\tchange height currently:" << ymax-ymin <<" "; cout << "r\trun with the changes "; cin >> menu_pic; switch(menu_pic) { case 'x':cout << "new width? > "; cin >> xsiz; break; case 'y':cout << "new height? > "; cin >> ysiz; break; case 'r':set_vars(xsiz,ysiz); g=grid_new(); doitem=1; loop=false; break; case 'q':doitem=9; loop=false; break; default: cout << "what? "; }} return doitem; }

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

Introduction To Database And Knowledge Base Systems

Authors: S Krishna

1st Edition

9810206208, 978-9810206208

More Books

Students also viewed these Databases questions