Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help fixing errors. //**begin #include files************ #include // provides access to cin and cout #include // provides access to time() for srand() //--end of

Need help fixing errors.

//**begin #include files************ #include // provides access to cin and cout #include // provides access to time() for srand() //--end of #include files----------- //----------------------------------

using namespace std; //----------------------------------

//**begin function prototypes******* double func1( int[]); bool func2(int); //--end of function prototypes------ //----------------------------------

//**begin global constants********** const int ArraySize = 10; //--end of global constants--------- //----------------------------------

//**begin main program************** int main() { // seed random number generator srand(time(NULL)); // create and initialize variables string myString; int myInt; bool myBool; int myIntArray[ArraySize]; // initialize the array with random numbers 1-100 for (int &i:myIntArray) { i = (rand()%100) + 1; } // calling the functions for (int i = 0; i < 4; i++) { myInt = func1( myIntArray); cout << "Value returned by func1 was " << myInt << endl; } cout << "Flipping a coin. The result is " << func2()?"Heads.":"Tails."; << endl; for (int i = 0; i < 4; i++) { int ranNum = rand()%3; cout << "The number " << ranNum << " is " << func3(ranNum) << endl; } // Wait for user input to close program when debugging. cin.get(); return 0; } //--end of main program------------- //----------------------------------

//**begin function definitions****** int func1( int []) { return int [rand()%ArraySize]; }

bool func2() { return rand()%2?true:false; }

string func3( int anInt) { switch (anInt) { case 0: return "ZERO."; break; case 1: return "ONE."; break; case 2: return "TWO." break; default: break; } return "Not Found."; } //--end of function definitions------ //---------------------------------- Here is the error list. Issue Type and number Description File Line Project Error 3 error C2660: func2 : function does not take 0 arguments C:\YOUR PATH\ week5 assignment-1\main.cpp 46 Week5 Assignment-1 1 Error 4 error C2143: syntax error : missing ; before << C:\YOUR PATH\ week5 assignment-1\main.cpp 46 Week5 Assignment-1 1 Error 5 error C3861: func3: identifier not found C:\YOUR PATH\ week5 assignment-1\main.cpp 50 Week5 Assignment-1 1 Error 6 error C2556: int func1(int []) : overloaded function differs only by return type from double func1(int []) C:\YOUR PATH\ week5 assignment-1\main.cpp 61 Week5 Assignment-1 1 Error 7 error C2371: func1 : redefinition; different basic types C:\YOUR PATH\ week5 assignment-1\main.cpp 61 Week5 Assignment-1 1 Error 8 error C2062: type int unexpected C:\YOUR PATH\ week5 assignment-1\main.cpp 62 Week5 Assignment-1 1 Error 9 error C2143: syntax error : missing ; before break C:\YOUR PATH\ week5 assignment-1\main.cpp 82 Week5 Assignment-1 1 IntelliSense Error 10 IntelliSense: cannot overload functions distinguished by return type alone C:\YOUR PATH\ week5 assignment-1\main.cpp 15 Week5 Assignment-1 8 IntelliSense Error 11 IntelliSense: expected an expression C:\YOUR PATH\ week5 assignment-1\main.cpp 46 Week5 Assignment-1 74 IntelliSense Error 12 IntelliSense: no operator << matches these operands (operand types are: std::basic_ostream> << std::string) C:\YOUR PATH\ week5 assignment-1\main.cpp 50 Week5 Assignment-1 45 IntelliSense Error 13 IntelliSense: type name is not allowed C:\YOUR PATH\ week5 assignment-1\main.cpp 62 Week5 Assignment-1 9 IntelliSense Error 14 IntelliSense: expected a ; C:\YOUR PATH\ week5 assignment-1\main.cpp 82 Week5 Assignment-1 3

2. Run-Time Bugs The following program is an upgrade to the Week 3 snowball fight code. Weve changed the movement algorithm and made the code easier to modify by using two functions. Weve also changed what it reports. It has four lines of code that need to be changed but only two real bugs. Youll understand when you find the bugs. The assignment is to find and fix the two bugs. One bug is subtle but does exhibit improper behavior; you just need to be looking closely. Heres a big hint. When a target is hit, it should no longer move or report hits or misses. The other bug is insidious. This is the kind of bug that gets shipped when you dont thoroughly test your code. It has no outward signs of failure but affects the game significantly. Were going to provide some guidance for this bug to help improve your code testing skills. Build the code and then follow these steps. Set a breakpoint at line158, switch (ranNum). The quickest way is to left click to the left of the line number. If a red dot doesnt appear, move the mouse a little farther left. Now start debugging (either hit F5 or use the DEBUG -> Start Debugging menu). Enter in the hit column and row data. You will get one or more target hit or miss reports, and then the program will stop at the breakpoint. (It is possible that the random number will be 4 and the breakpoint wont be triggered for any of the targets in this turn. If that happens, just keep entering column and row data until the breakpoint is triggered.) Youll see a yellow arrow in the red dot. The yellow arrow tells you where the program has stopped. Now look in the Locals window below your source code. (Note: The Locals window is configurable and can be placed anywhere or hidden. The default location is below the source code.) If the Locals window is hidden, try the Alt+4 key combination. The Locals window can also be accessed through the DEBUG->Windows->Locals menu. Left click the + to the left of Target. This should expand the target so you can see all the values, although you can also see the values in the Value column without clicking on the +. Note the value of Target.position (.x and .y). Hit F10. This will cause the program to advance one line of code (single step). The yellow arrow will move and will be pointing at the next line of code to be executed. This should be the first line of one of the case statements. Hit F10 again. If youre lucky, you will be on a line of code that looks like Target.position. . .;. If not, hit F5, which will take you back to Step 3. Cycle through Steps 3, 4, and 5 until you are on a line of code like that shown above. Hit F10 again and note the values of Target.positon. The text in the value line after Target should have turned red, and either the .x or .y value should have changed. Hit F10 three more times and you should be back in the main code at the if (!T.hit) moveTarget(T); line of code. Look at the Locals window. Notice the variables being shown have changed. The local variables in the function are no longer available (they are out of scope), and we are looking at the local variables in Main. Look at the T.position values. Are they the same as the Target.position values? If not, then we havent really moved the Target. Can you figure out why? Remember our discussion of pass by value and pass by reference, especially for structs and std::arrays. You might want to do a little research on the other windows, such as the Autos window and especially the Watch window, which can be very useful.

Here is the code. // Week 5 Assignment-2 // Description: Snowball fight version 2 //----------------------------------

//**begin #include files************ #include // provides access to cin and cout #include // provides access to std::array #include // provides access to time() for srand() //--end of #include files----------- //----------------------------------

using namespace std; //----------------------------------

//**begin global constants********** // define coordinate structure struct Coords { int x; int y; };

// define a struct of the target struct MyStruct { int ID; // -- Identification number of the target Coords position; // -- position of target int dist; // -- distance between target and snowball hit bool hit; // -- flag indicating target has been hit };

const int gridSize = 5; // const grid size (i.e. 5x5 grid constant is 5) const int turns = 20; // const number of turns const int targetCount = 3; // number of targets

//--end of global constants--------- //----------------------------------

//**begin function prototypes******* int throwSnowball(Coords p, MyStruct Target); void moveTarget( MyStruct Target); //--end of function prototypes------ //----------------------------------

//**begin main program************** int main() { // initialization srand(time(NULL)); bool allHit = false; int hitCount = 0; // number of hits. int dist = 0; // distance of miss Coords snowballPos; // position of snowball hit array Targets; // Initialize targets int idNum = 0; for (auto &T: Targets) { T.ID = idNum++; // set identification number // set target at random location T.position.x = rand()%gridSize; T.position.y = rand()%gridSize; T.hit = false; // set target hit flag to default: false } // loop for the specified number of turns for (int i = 0; i < turns; i++) { // get x and y position for the snowball from player cout << "column? "; cin >> snowballPos.x; cout << "row? "; cin >> snowballPos.y; // throw snow ball (see instructions for details) for(auto &T: Targets) { if (!T.hit) { // check for hit or miss dist = throwSnowball(snowballPos, T); // report results switch (dist) { case 0: cout << "***SPLAT*** You hit target " << T.ID << "!" << endl; hitCount++; break; case 1: cout << "target " << T.ID << ": Way too close!" << endl; break; case 2: cout << "target " << T.ID << ": I heard it hit." << endl; break; default: cout << "target " << T.ID << ": Missed by a mile." << endl; break; } // target moves (see instruction for details if (!T.hit) moveTarget(T); } } if (hitCount == 3) { allHit = true; break; } cout << "---Next Turn---" << endl; } // end of loop // report score (number of hits vs turns) if (allHit) cout << "All targets have been hit! Great job!" << endl; else cout << "You had " << hitCount << " hits out of " << turns << " throws." << endl; cin.get(); // Wait for user input to close program when debugging. cin.get(); return 0; } //--end of main program------------- //----------------------------------

//**begin function definitions****** // Determine hit or distance int throwSnowball(Coords p, MyStruct Target) { int aDistance; // compare to the target's position if ( p.x == Target.position.x) { if (p.y == Target.position.y) { Target.hit = true; return 0; } else { return abs(p.y - Target.position.y); } } else { aDistance = abs(p.x -Target.position.x); if (aDistance < abs(p.y - Target.position.y)) aDistance = abs(p.y -Target.position.y); return aDistance; } }

// Move the target void moveTarget( MyStruct Target) { enum MyEnum { North, East, South, West, Stay }; bool moveNotFound = true; MyEnum ranNum = MyEnum(rand()%5); if (ranNum == Stay) return; while (moveNotFound) { switch (ranNum) { case North: if (Target.position.y == 0) break; // can't move North Target.position.y--; cout << Target.ID << " moving North." << endl; return; case East: if (Target.position.x == gridSize-1) break; // can't move East Target.position.x++; cout << Target.ID << " moving East." << endl; return; case South: if (Target.position.y == gridSize -1) break; // can't move South Target.position.y++; cout << Target.ID << " moving South." << endl; return; case West: if (Target.position.x == 0) break; // can't move West Target.position.x--; cout << Target.ID << " moving West." << endl; return; default: break; } ranNum = MyEnum(int(ranNum+1)%4); } } //--end of function definitions------ //-----------------------------------

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