DeVry GSP 115 Week 4 Assignment latest

$28

Description

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 Courses helps in providing the best essay writing service. If you need 100% original papers for DeVry GSP 115 Week 4 Assignment latest, then contact us through call or live chat.

DeVry GSP 115 Week 4 Assignment latest

Best DeVry GSP 115 Week 4 Assignment latest
DeVry GSP 115 Week 4 Assignment latest

Reviews

There are no reviews yet.

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

Add to cart