DeVry GSP 115 All Assignments latest

$120

Description

DeVry GSP 115 All Assignments latest

DeVry GSP 115 Week 1 Assignment latest

Week 1: Simple Data Types

Instructions

Complete the following assignments. Cut and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document. If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W1_Assignments.docx. Submit it to the Week 1 assignment Dropbox.

  1. 1.Compile Time Bugs (TCO 4)

Find and fix the four (possibly five) compile time bugs in the code at the end of this section. Compile time bugs shown as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here . You will actually see more than five errors, but they are all caused by just four or five bugs. There is a case where you have either two bugs or one, depending on what you think the error is. In one case, it can be fixed with a single change. In the other case, it will take two changes to fix the problems.

Here is the code.

// Week 1 Assignment-1

// Description: Compile time errors with simple variables

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

//–end of #include files———–

//———————————-

//**begin global constants**********

// NO GLOBALS

//–end of global constants———

//———————————-

usingnamespacestd;

//———————————-

//**begin main program**************

int main()

{

// Initialize variables

int 1IntVar;

int x = “Four”;

char y = “a”;

float z1 = 4.756f

z2 = 7.45f;

// Print out the values

1IntVar = x;

cout<<“1IntVar = “<< 1IntVar <<endl;

cout<<“x = “<< x <<endl;

cout<<“y = “<< y <<endl;

cout<<“z1 = “<< z1 <<endl;

cout<<“z2 = “<< z2 <<endl;

ductTape;

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 2.Numbers and Conversions (TCO 4)

The following has one compile time bug. This bug only generates a warning in some compilers and would cause an exception during runtime. Visual Studio 2012 actually gives us an error at compile time. This is better, because you should never do the thing that causes this error. Fix the error and run the program. Compare the output to the code and answer the questions found after the code.

Here is the code.

// Week 1 Assignment-2

// Description: Numbers and conversions with simple variables

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<iomanip>

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin main program**************

int main()

{

// create and initialize variables

int x, y;

float f1 = 1.6767676f;

float f2 = 2.2f;

float diff;

// demonstrate math and number output

x = y;

x = f1; // What happens when you store a float into an int?

y = f2; // Does it round or truncate?

// display ints

cout<<“—>using default cout settings: “<<endl;

cout<<“x=”<< x <<” y=”<< y <<endl;

cout<<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—>using scientific settings: “<<endl;

cout<< scientific <<“x=”<< x <<” y=”<< y <<endl;

cout<< scientific <<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—>setting precision to 4″<<setprecision(4) <<endl;

cout<< scientific <<“x=”<< x <<” y=”<< y <<endl;

cout<< scientific <<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—>using fixed settings: “<<endl;

cout<< fixed <<“x=”<< x <<” y=”<< y <<endl;

cout<< fixed <<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—>setting precision to 12″<<setprecision(12) <<endl;

cout<< fixed <<“x=”<< x <<” y=”<< y <<endl;

cout<< fixed <<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—>restoring float (default) settings: “<<endl;

cout<<defaultfloat<<“x=”<< x <<” y=”<< y <<endl;

cout<<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—>setting precision to (default) 6″<<setprecision(6) <<endl;

cout<<“x=”<< x <<” y=”<< y <<endl;

cout<<“f1=”<< f1 <<” f2=”<< f2 <<endl;

cout<<“—–> Showing results of unusual and unexpected math operations “<<endl;

f2 = sqrt(f1);// take the square root of f1

cout<<“The square root of “<< f1 <<” is “<< f2 <<endl;

f2 = f2*f2; // square the square root of f1

cout<<“Squaring f2 should give us the same value as f1. f1 = “<< f1 <<” f2 = “<< f2 <<endl;

diff = f1 – f2; // This should be zero–is it?

cout<<“diff should be zero. diff = “<< diff <<endl;

cout<<“Oops. Let’s take another look at f1 and f2 in higher precision.”<<endl;

cout<<“—>setting precision to 12 places. f1 = “<<setprecision(12) << f1 <<” f2 = “<< f2 <<endl;

f2 = f2/diff; // What happens when we divide a floating point by something close to zero?

cout<<“Division by a very small number. f2 = “<< f2 <<endl;

f1 = f1/0.0; // What happens when we actually divide by zero?

cout<<“Division by zero. f1 = “<< f1 <<endl;

f1 = f1 * diff;

cout<<“Multiply a small negative number by infinity. f1 = “<< f1 <<endl;

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

Questions

  1. 1.When storing a floating point number into an integer variable, does it truncate or round?
  2. 2.When the floating point numbers were printed out at higher precision, they were not the same value as entered.Research this issue and explain why.
  3. 3.Thesetprecision(12) command either sets the precision for 12 places after the decimal point or 12 digits total; select the right option for each case below.
  4. a.fixed [total/after the decimal point]
  5. b.scientific [total/after the decimal point]
  6. c.float [total/after the decimal point]
  7. 4.Based on the results of taking the square root and then squaring again, what would you say about the accuracy of floating point arithmetic in C ?
  8. 5.Dividing by zero in earlier compilers generates an error and an exception.What does Visual Studio 2012 produce as the result of dividing by zero?
  9. 3.Create a Program FromPseudocode (TCO 1)

This exercise will be to use pseudocode (in the form of comments) to write a program that creates and initializes the variables for a computer role-playing game character generator. This will only setup the variables. It is slightly different from our iLab assignment.You can make up your own variable names.

Here is the pseudocode.

// Week 1 Assignment-3

// Description: Setup Variables for CRPG Character Generator

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

//–end of #include files———–

//———————————-

//**begin global constants**********

//–end of global constants———

//———————————-

usingnamespacestd;

//———————————-

//**begin main program**************

int main()

{

//**Enter code staring here

// Create Variables

// character ID [integer] initialized to 0

// strength [integer] initialized to 80

// armor [integer] initialized to 70

// health [float] initialized to 1.0f

// flag to indicate if the character is dead or alive [Boolean] initialized to true

// Print out each of the variables.

//**End of your code

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

 

 

 

DeVry GSP 115 Week 2 Assignment latest

Week 2: ComplexData Types

Instructions

Complete the following assignments. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document.If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W2_Assignments.docx. Submit it to the Week 2 assignment Dropbox.

  1. 1.Compile Time Bugs (TCOs 2, 4, 5, and 6)

Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here.jpg”>. Be aware that not every line that is marked as an error is actually an error. For example, if something causes one of your variables to not be created, such as forgetting to add the data type when you create it, everywhere in your code that references that variable will show an error.Fix the problem so your variable is created and the errors where the variable is referenced go away. You should work on bugs starting at the top of your code (that’s the order the compiler presents them to you). Fix the first line with a bug and then recompile.Often, this will clear additional errors. You may have to make some assumptions about what was actually intended. That’s OK. When the code compiles, run it and get a screen capture of the output.

Here is the code.

// Week 2 Assignment-1

// Description: Compile time errors in C Strings, Arrays, std:string,

// struct, enum, pointers, and std::array.

//———————————-

//**begin #include files************

#include <iostream> // provides access to cin and cout

#include <> // provides access to std:array

#include <string> // required for getline

//–end of #include files———–

//———————————-

using namespace std;

//———————————-

//**begin global constants**********

constintarraySize = 4; // **there is a subtle bug here (needs “const”)

enumMyEnum // Needs to be before the struct that uses it

{

Dog, Cat, Fish, Squirrel

};

structMyStruct

{

int a;

float b;

string c;

MyEnum d;

};

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

// Initialization

charmyCString[arraySize] = {0};

charmyOtherCString[] = {‘Yet another string’};

intmyInt[3] = {27, 39, 0, 42};

stringmyString;

MyStructaStruct = { 4,3.5,Dog ,”Dogs”};

int x;

int * pX = x;

array<MyStruct, arraySize> Animals;

// Storing values in uninitialized variables

myCString[0] = “A”;

myString = “A third string”;

x = 4;

for (inti = 0; i<arraySize; i )

{

Animals[i].a = rand();

Animals[i].b = rand()0/100.0;

Animals[i].c = MyEnum(rand()%4);

cout<< “Enter a name: “;

getline(cin, Animals[i].d);

}

// Display the data

cout<< “myCString = ” <<myCString<<endl;

cout<< “myOtherCString = ” <<myOtherCString<<endl;

for (inti = 0; i< 4; i )

{

cout<< “myInt[” <<i<< “] = ” <<myInt[i] <<endl;

}

cout<< “aStruct data: a = ” <<aStruct.a<< ” b = ” <<aStruct.b<< ” c = ” <<aStruct.c<< ” d = ” <<aStruct.d<<endl;

cout<< “x = ” << x <<endl;

cout<< “value pointed to by pX = ” << *pX<<endl;

for (inti = 0; i<arraySize;i )

{

cout<< “Animals[” <<i<< “].a = ” << Animals[i].a <<endl;

cout<< “Animals[” <<i<< “].b = ” << Animals[i].b <<endl;

cout<< “Animals[” <<i<< “].c = ” << Animals[i].c <<endl;

cout<< “Animals[” <<i<< “].d = ” << Animals[i].d <<endl;

}

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 2.Problems With Pointers and New/Delete(TCOs 2, 4, 5, and 6)

The following code creates a pointer to an array of ints and allocates space for fiveints. It then loads the array with the square of values 0 through 4 and displays the results. Because this is a dynamic array allocation, we can reallocate a larger array. This time we allocate space for sevenints and load and display the squares. In the process, our code has created a memory leak, because we failed to follow the hard and fast rule of for every new there must be a delete.When you run this program (before you fix the memory leak), the output should look something like the following.

.jpg”>

Figure 1: Memory Leak Output

Here’s a printout of what the values should be. The actual addresses, of course, will be different, but they should follow the same pattern.

.jpg”>

Figure 2: Desired Output

Notice in Figure 1 that the address of nArray never changes and is the same for nArray[0] in both loops.

For this assignment, fix the memory leak. The output should look similar to Figure 2.

Here is the code.

// Week 2 Assignment-2

// Description: Problems with pointers and new/delete

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

constintarraySize = 5;

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

cout<<endl<<endl;

int* nArray =newint[arraySize];

cout<<” —>After creating and allocating memory for nArray.”<<endl;

cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl;

for (inti = 0; i<arraySize; i )

{

nArray[i] = i*i;

}

cout<<” —>After initializing nArray.”<<endl;

cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl<<endl;

for (inti = 0; i<arraySize; i )

{

cout<<” nArray[“<<i<<“] = “<<nArray[i] <<” at address <“<<nArray i<<“>”<<endl;

}

// You’ll need a command here to fix the memory leak

cout<<endl<<” —>Before reallocating memory for nArray.”<<endl;

cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<endl;

nArray =newint[arraySize 2];

cout<<dec<<” —>After reallocating memory for nArray.”<<endl;

cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl;

for (inti = 0; i< arraySize 2; i )

{

nArray[i] = i*i;

}

cout<<endl<<” —>After reinitializing nArray.”<<endl;

cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl<<endl;

for (inti = 0; i< arraySize 2; i )

{

cout<<” nArray[“<<i<<“] = “<<nArray[i] <<” at address <“<<nArray i<<“>”<<endl;

}

// . . . and also here.

cout<<endl<<” —>Getting ready to close down the program.”<<endl;

cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl;

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 3.Create a Program FromPseudocode (TCOs 1, 2, 4, 5, and 6)

This exercise will be to use pseudocode (in the form of comments) to write a program that creates and initializes the variables for a computer role-playing game character generator. This will only setup the variables. The difference from last week is that you must put your character attributes in a structure. You can make up your own variable names.

Here is the pseudocode.

// Week 2 Assignment-3

// Description: CRPG Character attributes using a struct

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

//–end of #include files———–

//———————————-

//**begin global constants**********

//–end of global constants———

//———————————-

usingnamespacestd;

//———————————-

//**begin main program**************

int main()

{

//**Enter code staring here

// Create structure

// initialize structure data members

// character ID [integer] initialized to 0

// strength [integer] initialized to 80

// armor [integer] initialized to 70

// health [float] initialized to 1.0f

// flag to indicate if the character is dead or alive [boolean] initialized to true

// Print out each of the data members in the structure.

//**End of your code

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

 

 

DeVry GSP 115 Week 3 Assignment latest

Week 3: Loops and Branching

Instructions

Complete the following assignments. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document. If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W3_Assignments.docx.Submit it to the Week 3 assignment Dropbox.

  1. 1.Compile Time Bugs

Find and fix the fivecompile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here.jpg”>. You will actually see more than five errors, but they are all caused by just five bugs (four in just one line). Remember, start with the first error in your code and work down.

Here is the code.

// Week 1 Assignment-1

// Description: Compile time errors with loops and branching

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<time.h>// to allow seeding the random number generator using time

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

constintarraySize = 5;

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

// seed random number generator

srand(time(NULL));

// create a flag to end the while loop

boolendLoop =false;

// create an int array

int array[arraySize];

// initialize the int array

for (i = 0; i<arraysize: i );

{

array[i] = (rand()P) 50;

}

int number;

intmysteryNum;

intarrayNum;

while (!endLoop)

{

mysteryNum = rand()@;

arrayNum = array[rand()%arraySize];

cout<<” What number do you need to subtract from “<<arrayNum<<” to get “<<mysteryNum<<“? “;

cin>> number;

if (mysteryNum == arrayNum – number)

{

cout<<“You got the number!”<<endl;

else

{

cout<<“That’s not the right number.”<<endl;

endLoop =true;

}

}

cin.get();

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 2.Run-Time Errors in Loops and Branching

Loops and branching are hot spots for simple but difficult-to-spot bugs. This code is filled with some very annoying and common bugs. The code will compile fine, but it won’t work right and may even crash.There are two bugs that are crash bugs. There is one logic error that causes the program to quit before it should and one typo that causes incorrect behavior. This last one will be the hardest to find because it only happens when you win, and it is an error that is notoriously hard to spot until you’ve been caught by it enough to expect it.

This is a simple slot machine. Here is how it is supposed to work.

  1. 1.When you hit the return key, the wheel spins (you won’t actually see any spinning, just the results of the spin).
  2. 2.You automatically bet $1 on each spin.
  3. 3.If you get three of the same symbols, you win according to the following table.
  4. a.Lemons—you keep your bet.
  5. b.Cherries—you win $5.
  6. c.Oranges—you win $10.
  7. d.Bells—you win $15.
  8. e.Jackpot—you win $1,000 and the game is over.
  9. 4.When your pot is gone (pot == 0), you lose and the game is over.

Here is the code.

// Week 3 Assignment- 2

// Description: Run-time errors in loops and branching

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<iomanip>

#include<array>

#include<vector>

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin main program**************

int main()

{

// seed random number generator

srand(time(NULL));

// create enum for symbols

enumsymbol

{

Lemon,Cherry,Orange,Bell,Jackpot

};

// create a struct for slot machine wheel

structWheel

{

array<string, 10> symbols;

array<symbol, 10>eSymbols;

int position;

string selected;

};

//create an array of three slot machine wheels

array<Wheel, 3>slotMachine =

{

{

{

{“Cherry”,”Orange”,”Lemon”,”Orange”,”Bell”,”Orange”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Cherry,Orange,Lemon,Orange,Bell,Orange,Lemon,Cherry,Jackpot,Bell},

0,”Cherry”

},

{

{“Cherry”,”Bell”,”Lemon”,”Orange”,”Bell”,”Jackpot”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Cherry,Bell,Lemon,Orange,Bell,Jackpot,Lemon,Cherry,Jackpot,Bell},

1,”Bell”

},

{

{“Cherry”,”Orange”,”Lemon”,”Orange”,”Lemon”,”Orange”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Cherry,Orange,Lemon,Orange,Lemon,Orange,Lemon,Cherry,Jackpot,Bell},

2,”Lemon”

}

}

};

boolgameOn =true;

bool winner =false;

intthePot = 100;

int bet = 1;

vector<int> combo;

while (gameOn)

{

for (inti = 1; i< 4; i )

{

slotMachine[i].position =(slotMachine[i].position rand());

slotMachine[i].selected = slotMachine[i].symbols[slotMachine[i].position];

cout<<setw(10) << left <<slotMachine[i].selected.c_str() ;

combo.push_back(slotMachine[i].eSymbols[slotMachine[i].position]);

}

if ((combo[0] == combo[1]) && (combo[1] == combo[2]))

{

if (combo[0] ==Lemon)

{

cout<<“You keep your bet.”<<endl;

}

elseif(combo[0] =Jackpot)

{

cout<<“**** You hit $1000 Jackpot!!! ****”<<endl;

thePot = 1000;

winner =true;

gameOn =false;

}

else

{

cout<<“WINNER! You win $”<<combo[0]*5 <<endl;

thePot = combo[0]*5;

}

}

else

{

thePot -= bet;

if (thePot> 0 ) gameOn=false;

}

cout<<“You now have $”<<thePot<<endl;

combo.clear();

cout<<endl;

cin.get();

}

if (winner) cout<<“You walk away a winner.”<<endl;

elsecout<<“You have lost all your money.”<<endl;

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 3.Create a Program FromPseudocode

This exercise will be to use pseudocode (in the form of comments) to write a program that simulates a one-sided snowball fight. You select the row and column you want the snowball to hit; if the target is at that location, you get a point; otherwise, you get the distance (but not the direction) of the target from the snowball hit point. The distance is calculated by taking the absolute value of the difference between the x position of the target and the snowball hit and the y position and the snowball hit. Report the larger of the two if they are different.

After each throw of the snowball, the target moves using the following algorithm.

Generate a random number (0–2). The target doesn’t move if the random number is 0. If the random number is 1, the target changes its x position. If the random number is 2, it changes its y position. Randomly choose whether to increment by one or decrement by one. If the target is on the boundary of the grid, move away from the boundary by one.

The grid size should be 5.

The number of turns should be 10.

Here is the pseudocode.

// Week 3 Assignment-3

// Description: Snowball fight – version 1

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

// define a struct of the target

// — xPos is the x position (the column)

// — yPos is the y position (the row)

// — xHit is x position of the hit(the column)

// — yHit is y position of the hit(the row)

// — distance between target and snowball hit

// — hits is how many times the target has been hit

// const grid size (i.e. 5×5 grid constant is 5)

// const number of turns

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

// initialization

// seed the random number generator

// create the target struct

// set target at random location

// set hits to zero

// loop for the specified number of turns

// get x and y position for the snowball from player

// compare to the target’s position

// report hit, or distance of miss (see instructions for details)

// target moves (see instruction for details)

// end of loop

// report score (number of hits vs turns)

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

 

DeVry GSP 115 Week 4 Assignment latest

Week 4: More Loops and Branching

Instructions

Complete the following assignments. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document. If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W4_Assignments.docx.Submit it to the Week 4 assignment Dropbox.

  1. 1.Compile Time Bugs

Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here.jpg”>. This assignment is meant to test your attention to detail and strengthen your debugging skills.

Here is the code.

// Week 4 Assignment-1

// Description: Compile time errors

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<fstream>// provides access to file commands

#include<string>// provides access to string commands

#include<vectors>// provides access to std::vector

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

// create and initialize variables

stringmyTextString;

stringmyFilename;

vector<string>myStrVector;

ifstreaminFile;

ofstreamoutFile;

cout<<“enter a file name (without an extension): “<<endl;

getline(cin, myFilename);

myFilename =”.txt”;

// open an output file

outfile.open(myFilename);

// write to a file

for(inti = 0; i< 3;i )

{

cout>>”enter a line of text: “;

getline(cin, myTextString);

outFile<<myTextString<<endl;

}

// close the file

outFile.close();

// open an input file

inFile.open(myFilename);

// read from the file

while (getline(inFile,myTextString))

{

myStrVector.push_back(myTextString);

}

inFile.close();

// use a range-based for loop with a switch statement

for(auto s: myStrVector)

{

cout<< s <<endl;

charswitchFlag = s[0];

switch (switchFlag)

{

case”a”:

cout<<“Hey, a vowel. The ‘a’ vowel actually.”<<endl;

break;

case’e’:

cout<<“See vowel. See vowel run. run vowel, run. The ‘e’ vowel.”<<endl;

break;

case’i’:

cout<<“I know. It’s a vowel. The ‘i’ vowel.”<<endl;

break;

case’o’:

cout<<“Oh! Don’t you know, it’s the ‘o’ vowel.”<<endl;

break;

case’u’:

cout<<“Whew! We got a you. Actually, the ‘u’ vowel.”<<endl;

break;

case’s’:

cout<<“Oh great! I love ‘s’s. More ‘s’s, please.”<<endl;

break;

default

cout<<“Nothing interesting here. I would really like to see an ‘s’.”<<endl;

break;

}

}

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 2.Run-Time Errors

We revisit the slot machine from last week, this time using a range-based For loop and the Switch statement. The Switch gives us some flexibility in coding the payouts. The code has one compile time bug and two run-time bugs. One of the run-time errors is a typo and the other is a logic error.Fix the compile error and run the program to find the run-time errors. Neither run-time bug will show up right away. This is to give you experience with testing your program to make sure it works. Often, although a program seems to be OK, it isn’t. Hint: Look for occasional erroneous wins of $1 when the first symbol is Bar.

Here is the code.

// Week 4 Assignment- 2

// Description: Run-time errors in more loops and branching

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<iomanip>

#include<array>

#include<vector>

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

// number of positions on a reel (11)

constintreelPositions = 11;

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

// seed random number generator

srand(time(NULL));

// create enum for symbols

enum symbol

{

Lemon, Cherry, Orange, Bell, Bar, Jackpot

};

// create a struct for slot machine wheel

struct Wheel

{

array<string, reelPositions> symbols;

array<symbol, 1reelPositions1>eSymbols;

int position;

string selected;

};

//create an array of three slot machine wheels

array<Wheel, 3>slotMachine =

{

{

{

{“Bar”,”Orange”,”Lemon”,”Orange”,”Bell”,”Orange”,”Bar”,”Lemon”,”Bell”,”Jackpot”,”Bell”},

{Bar, Orange, Lemon, Orange, Bell, Orange, Lemon, Bell, Jackpot, Bell},

0,”Cherry”

},

{

{“Lemon”,”Bell”,”Lemon”,”Orange”,”Bell”,”Jackpot”,”Bar”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Lemon, Bell, Lemon, Orange, Bell, Jackpot, Bar, Lemon, Cherry, Jackpot, Bell},

1,”Bell”

},

{

{“Cherry”,”Orange”,”Bar”,”Lemon”,”Orange”,”Lemon”,”Orange”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Cherry, Orange, Bar, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell},

2,”Lemon”

}

}

};

boolgameOn =true;

intthePot = 100;

int bet = 1;

bool winner =false;

int winnings = 0;

charcheckKey =’n’;

vector<int> combo;

cout<<“Hit return to bet, space and return to quit.”<<endl;

while (gameOn)

{

for (auto s: slotMachine)

{

s.position=(s.position rand()%reelPositions)%reelPositions;

s.selected = s.symbols[s.position];

cout<<setw(10) << left <<s.selected.c_str() ;

combo.push_back(s.eSymbols[s.position]);

}

winnings = -bet;

switch (combo[0]);

{

case Lemon:

switch (combo[1])

{

case Lemon:

if (combo[2] == Lemon) winnings = 1;

elseif (combo[2] == Cherry) winnings = 1;

break;

case Cherry:

winnings = 1;

if (combo[2] == Cherry) winnings = rand()%4 2;

break;

default:

if (combo[2] == Cherry) winnings = 1;

break;

}

break;

case Cherry:

winnings = 1;

if ((combo[1] == Cherry) && (combo[2] == Cherry)) winnings = 10;

elseif ((combo[1] == Cherry) || (combo[2] == Cherry)) winnings = rand()%4 2;

break;

case Orange:

switch (combo[1])

{

case Orange:

if (combo[2] == Orange) winnings = 15;

elseif (combo[2] == Cherry) winnings = 1;

break;

case Cherry:

winnings = 1;

if (combo[2] == Cherry) winnings = rand()%4 2;

break;

default:

if (combo[2] == Cherry) winnings = 1;

break;

}

break;

case Bell:

switch (combo[1])

{

case Bell:

if (combo[2] == Bell) winnings = 20;

elseif (combo[2] == Cherry) winnings = 1;

break;

case Cherry:

winnings = 1;

if (combo[2] == Cherry) winnings = rand()%4 2;

break;

default:

if (combo[2] = Cherry) winnings = 1;

break;

}

break;

case Bar:

switch (combo[1])

{

case Bar:

if (combo[2] == Bar) winnings = 40;

elseif (combo[2] == Cherry) winnings = 1;

break;

case Cherry:

winnings = 1;

if (combo[2] == Cherry) winnings = rand()%4 2;

break;

default:

if (combo[2] == Cherry) winnings = 1;

break;

}

break; case Jackpot:

switch (combo[1])

{

case Jackpot:

if (combo[2] == Jackpot)

{

winnings = 1000;

winner =true;

gameOn =false;

cout<<“You hit the Jackpot!!!”<<endl;

}

elseif (combo[2] == Cherry) winnings = 1;

break;

case Cherry:

winnings = 1;

if (combo[2] == Cherry) winnings = rand()%4 2;

break;

default:

if (combo[2] == Cherry) winnings = 1;

break;

}

break;

}

if (winnings > 0) cout<<“You win “<< winnings <<endl;

thePot = winnings;

cout<<“You now have $”<<thePot<<endl;

combo.clear();

cout<<endl;

cin.get(checkKey);

if (checkKey !=’n’) gameOn =false;

}

while (!cin.get()){};

if (winner) cout<<“You walk away a winner.”<<endl;

elseif (thePot< 0) cout<<“Good bye.”<<endl;

elsecout<<“You have lost all your money.”<<endl;

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

  1. 3.Create a Program FromPseudocode

This exercise will be to use pseudocode (in the form of comments) to write a program to simulate a horse race. You will need the following variables.

  • structHorse—>member variables

o string name: the name of the horse

o intdistance: how far the horse has traveled

o intoffset: used to track penalties and bonuses from events

o intID: the number of the horse and index for horses

  • global constant inthorseCount = 6: the number of horses in the race
  • global constant inttrackLength = 100: the length of the race track
  • std::array <Horse, horseCount> horses
  • intdie1, die2 for die rolls (These are optional—the sum can be generated without them.)
  • intsumis used to store the sum of the die rolls.
  • intwinneris the winning horse’s ID.
  • intleadis usedto track the distance of the leading horse.
  • intbet is the amount bet on the race.
  • intcash is the amount of money the player has.
  • boolracing is a flag used in the Do-Whileracing loop.While it is true, the race continues.
  • boolplaying is a flag used in the Do-Whilegame loop.While it is true, the game continues.
  • inthNum is the number (ID) of the horse the player bet on.

Use a range-based For loop to initialize the horse’s data. Ask the player for names or hard code them—your choice.

At the start of each race, ask the player to make a bet and pick a horse.

Each turn during the race, each horse will move forward based on the sum of the roll of two six-sided dice (1–6). This value is augmented by an offset. The offset can be negative or positive. Add the offset to the sum of the dice. (Note:If the offset is negative, adding to the sum will reduce the sum’s value.) If the sum is negative, store the negative value in offset and leave the horse’s distance value unchanged. If the sum is positive, add it to the horse’s distance and set the offset to zero. Once any horse has gone more than the length of the track, the race is over and the horse with the greatest distance value wins. Add six times the bet to cash if the player wins, or subtract the bet from cash if the player loses. The game is over when the player has run out of money. You also quit when the player bets $0 or less.

The offset value is calculated using a Switch statement and a random number between 0 and 15. If the number is 0 or 1, the horse breaks stride and the offset is negative 1 or 2 (randomly selected). If the random number is 2 or 3, the horse finds his stride and the offset is a positive 1 or 2 (randomly selected). If the random number is 4, the horse stumbles and the offset is a negative 4–6 (randomly selected). If the random number is 5, the horse has a burst of energy and the offset is a positive 4–6 (randomly selected).If the random number is 6–15, nothing happens.

Reset the values of the horses distance and offset using a range-based For loop before each new race.

Here is the pseudocode.

// Week 4 Assignment-3

// Description: Horserace

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<array>// provides access to std::array

#include<time.h>// provides access to time()

#include<string>// provides access to getline()

#include<iomanip>

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

// struct for horse (name, distance, eventOffset, ID)

// number of horses (6)

// length of track (100)

//–end of global constants———

//———————————-

//**begin main program**************

int main()

{

// seed random number generator

srand(time(NULL));

// create and initialize variables

// create horses

// have user name horses (range-based for loop)

// two 1-6 dice

// sum of dice roll

// number of winning horse

// distance of leading horse

// bet

// cash on hand

// flag for ending race

// flag for ending game

// horse number bet on

// start of game loop

// ask for bet amount

//if bet is less than or equal to $0–quit

// else get the number of the horse being bet on

// start of race loop

// for each horse–use range-based for

// roll one 16 sided die to check on event

// use switch statement to handle event

//(see instructions for event)

// roll dice and,using offset, adjust horse’s distance

// check for race over

// report horse position

// check if this is leading horse and change lead and winner values if it is

//continue race loop until race is over

//end race loop

// report winner and settle bet

// if player has cash, get new bet, new horse, and start race

// else quit

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

 

DeVry GSP 115 Week 7 Assignment latest

Week 7: Scope and Classes

Instructions

Complete the following assignments. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document. If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W7_Assignments.docx. Submit to the Week 7 assignment Dropbox.

  1. 1.Debugging Challenge

You should be busy designing and writing your expansion to the Course Project, so this week’s assignment will be a debugging challenge. We have revised the slot machine code to make the payouts easier to change, but a lot of bugs were created in the process—both compile time and run-time bugs. Your job is to get the code running correctly. As before, the code may look like it’s working but could still have the occasional bug that only rarely shows up. You will only be held responsible for bugs that cause a problem that can be detected. All other bugs will be declared as features or somebody else’s problemin honor of an age-old computer game development tradition.

Here are some clues.

  • You shouldn’t see any blank symbols.
  • There are only three bugs.

This version of the slot machine gets its payout table from a text file. The text file is a series of four numbers, such as 2, 2, 2, and 10.The first three are symbol numbers, and they match the enum numbers. In this case, 2 is Orange. The last number is the payout, so the set of numbers can be interpreted as Orange, Orange, Orange pays out 10. As a result, you can change the pay out by changing the table. However, to save space, payouts for the Cherry are hard coded in to the check4Win function. This was a short cut, but the Cherry payouts could have been set in the text file. In case you haven’t already figured it out, you will need to create the text file and put it in the same folder as your main.cpp file. Here is a copy of the text in the text file this program was tested with.

0 0 0 1 1 1 1 10 2 2 2 15 3 3 3 20 4 4 4 40 5 5 5 200 4 4 2 25 4 4 3 30

Here is the code.

// Week 7 Assignment-1

// Description:

//———————————-

//**begin #include files************

#include<iostream>// provides access to cin and cout

#include<iomanip>

#include<array>

#include<vector>

#include<sstream>

#include<fstream>

//–end of #include files———–

//———————————-

usingnamespacestd;

//———————————-

//**begin global constants**********

// number of positions on a reel (10)

constintreelPositions = 11;

// create enum for symbols

enum symbol

{

Lemon, Cherry, Orange, Bell, Bar, Jackpot

};

// define a struct for slot machine wheel

struct Wheel

{

array<string, reelPositions> symbols;

array<symbol, reelPositions>eSymbols;

int position;

string selected;

};//–end of global constants———

//———————————-

//**begin function prototypes*******

voidloadWinSheet(vector <array<int,4>>&);

int check4Win(vector <int>, vector <array<int,4>>&,int);

//void createSlotMachine(array <Wheel, 3>&);

//–end of function prototypes——

//———————————-

//**begin main program**************

int main()

{

// seed random number generator

srand(time(NULL));

// create the payout table

// define a vector for the payout table

vector<array<int,4>>winSheet;

loadWinSheet(winSheet);

//create an array of three slot machine wheels

array<Wheel, 3>slotMachine =

{

{

{

{“Orange”,”Cherry”,”Orange”,”Lemon”,”Orange”,”Bar”,”Lemon”,”Bell”,”Jackpot”,”Bell”},

{Orange, Cherry, Orange, Lemon, Orange, Bar, Lemon, Bell, Jackpot, Bell},

0,”Orange”

},

{

{“Bell”,”Lemon”,”Orange”,”Bar”,”Jackpot”,”Bar”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Bell, Lemon, Orange, Bar, Jackpot, Bar, Lemon, Cherry, Jackpot, Bell},

1,”Lemon”

},

{

{“Cherry”,”Lemon”,”Bar”,”Lemon”,”Orange”,”Orange”,”Lemon”,”Cherry”,”Jackpot”,”Bell”},

{Cherry, Orange, Bar, Lemon, Orange, Orange, Lemon, Cherry, Jackpot, Bell},

3,”Bar”

}

}

};

boolgameOn =true;

intthePot = 100;

bet = 1;

bool winner =false;

int winnings = 0;

charcheckKey =’n’;

vector<int> combo;

cout<<“Hit ‘enter’ to bet. Hit ‘space’ and ‘enter’ to quit.”<<endl;

while (gameOn)

{

for (auto&s: slotMachine)

{

s.position=(s.position rand()%reelPositions)%reelPositions;

s.selected = s.symbols[s.position];

cout<<setw(10) << left <<s.selected.c_str() ;

combo.push_back(s.eSymbols[s.position]);

}

winnings = check4Win( combo, winSheet, -bet);

if (winnings > 0) cout<<“You win “<< winnings <<“! “;

thePot = winnings;

cout<<“You now have $”<<thePot<<endl;

combo.clear();

cout<<endl;

if (thePot<= 0) gameOn =false;

cin.get(checkKey);

if (checkKey !=’n’) gameOn =false;

}

while (!cin.get()){};

if (winner) cout<<“You walk away a winner.”<<endl;

elseif (thePot> 0) cout<<“Good bye.”<<endl;

elsecout<<“You have lost all your money.”<<endl;

// Wait for user input to close program when debugging.

cin.get();

return 0;

}

//–end of main program————-

//———————————-

//**begin function definitions******

// loads the pattern payout table from a text file

voidloadWinSheet(vector <array<int,4>>&pT)

{

stringstreammyStream;

ifstreaminFile;

stringmyString;

array<int, 4> combo;

int pay;

inFile.open(“paytable.txt”);

if (inFile.is_open())

{

while ( getline (inFile,myString) )

{

myStream<<myString;

}

inFile.close();

}

while (!myStream.eof())

{

myStream>> combo[0] >> combo[1] >> combo[2] >> combo[3];

pT.push_back(combo);

}

return;

}

// Check for winning patterns

int check4Win(vector <int> pattern, vector <array<int,4>>&pT,inttheBet)

{

for(auto p: pT)

{

if (((pattern[0] == p[0]) && (pattern[1] == p[1]) ) && (pattern[2] == p[2]))return p[3];

if ((pattern[1] == Cherry) && (pattern[2] == Cherry))return 3;

if (pattern[2] == Cherry)return 1;

}

return -theBet;

}

//–end of function definitions——

//———————————-

 

DeVry Courses helps in providing the best essay writing service. If you need 100% original papers for DeVry GSP 115 All Assignments latest, then contact us through call or live chat.

DeVry GSP 115 All Assignments latest

Best DeVry GSP 115 All Assignments latest
DeVry GSP 115 All Assignments latest

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Add to cart