Just Click on Below Link to Download This Course:
https://www.devrycoursehelp.com/product/devry-gsp-115-week-3-assignment-latest/
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.
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
DeVry GSP 115 Week 3 Assignment latest
1. DeVry GSP 115 Week 3 Assignment latest
Just Click on Below Link to Download This Course:
https://www.devrycoursehelp.com/product/devry-gsp-115-week-3-assignment-latest/
Or
Email us
help@devrycoursehelp.com
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************
2. #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++);
{
3. array[i] = (rand()%50) + 50;
}
int number;
intmysteryNum;
intarrayNum;
while (!endLoop)
{
mysteryNum = rand()%40;
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.
4. cin.get();
return 0;
}
//–end of main program————-
//———————————-
2. 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************
5. #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;
6. 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”
7. }
}
};
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;
8. }
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;
9. 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————-
//———————————-
3. 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
10. // 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()
11. {
// 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
12. 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———
13. //———————————-
//**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;
}
14. // 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;
15. 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;
}
16. //–end of main program————-
//———————————-
2. 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)
17. 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
19. 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:
20. 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;
21. 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;
22. 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])
23. {
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;
24. 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————-
//———————————-
3. 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
25. 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.
26. 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()
27. {
// 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
28. // 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 3 Assignment
latest, then contact us through call or live chat.