Levels Tought:
Elementary,Middle School,High School,College,University,PHD
Teaching Since: | Jul 2017 |
Last Sign in: | 213 Weeks Ago, 4 Days Ago |
Questions Answered: | 15833 |
Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Hi there, I have two programs, one I cant figure out the problem in the program so it wont run. The second program I have, the command window wont stay up long enough to get a screen shot.
Â
Can you help me?
Â
FIRST PROBLEM
Â
// Week Assignment-
// Description:
//----------------------------------
Â
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <time.h> // provides access to time() for srand()
//--end of #include files-----------
//----------------------------------
Â
using namespace std;
//----------------------------------
Â
//**begin function prototypes*******
int func1(int[]);
bool func2();
string func3(int anInt);
//--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;
    int myIntArray[ArraySize];
    // initialize the array with random numbers 1-100
    for (int i = 0; i < ArraySize; i++) {
          myInt = (rand() % 100) + 1;
          myIntArray[i] = i;
    }
    // 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 myArray[]) {
    return myArray[(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------
//----------------------------------
Â
Â
Â
SECOND PROBLEM
Â
// Description: Horserace - version 2
//----------------------------------
Â
//**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-----------
//----------------------------------
Â
using namespace std;
//----------------------------------
Â
//**begin global constants**********
// struct for horse (name, distance, eventOffset, ID)
struct Horse {
      string name;
      int distance;
      int offset;
      int ID;
};
const int horseCount = 6;
const int trackLength = 100;
//--end of global constants---------
//----------------------------------
Â
//**begin function prototypes*******
// get horse name
void getName(Horse &);
// Racing events
int checkEvents(Horse &);
// Update positions
int updatePos(Horse &);
// Report positions
Â
void reportPos(array<Horse, horseCount>);
// Clear racing data
void clearData(array<Horse, horseCount> &);
//--end of function prototypes------
//----------------------------------
Â
//**begin main program**************
int main() {
      // seed random number generator
      srand(time(NULL));
      // CREATE AND INITIALIZE VARIABLES
      std::array<Horse, horseCount> Horses;
      int die1, die2;
      // using a range-based for loop, initialize data for each horse
      for (Horse &h : Horses) {
             getName(h);
             h.distance = 0;
             h.offset = 0;
             h.ID += 1;
      }
      // have user name horses
      // clear distance and offset.
      // create ID. (you will probably want to create a index variable external to the loop to do this)
Â
      // number of winning horse (init to 0)
      int winner = 0;
      // distance of leading horse (init to 0)
      int lead = 0;
      // bet
      // cash on hand (init to 100--or whatever value you chose)
      int bet;
      int cash = 100;
      // flag to indicate the race is active in race do-while loop (init to true)
      bool racing = true;
      // flag to indicate the game is active in game do-while loop (init to true)
      bool playing = true;
      // horse number bet on
      int hNum;
      // START OF GAME LOOP
      while (!playing) {
             // ask for bet amount and get the number of the horse being bet on
             cout << "You have $" << cash << ". How much do you want to bet? ";
             cin >> bet;
             if (bet <= 0) {
                    playing = false;
             }
             else {
                    cout << "Which horse number (from 1-6) do you want to bet on? ";
                    cin >> hNum;
                    cout << endl;
                    racing = true;
                    bet = (cash - bet);
                    cin.sync();
             }
             // if the bet is negative or $0, quit.
             while (racing) {
                    // START OF RACE LOOP
                    // for each horse--use range-based for
                    for (Horse &h : Horses) {
                          h.distance += checkEvents(h);
                          h.distance += updatePos(h);
                    }
                    // check for events using a function and return horse's offset
                    // roll dice and, using offset, adjust horse's distance using a function
                    // check for race over
                    if (Horses[0].distance < trackLength
                          && Horses[1].distance < trackLength
                          && Horses[2].distance < trackLength
                          && Horses[3].distance < trackLength
                          && Horses[4].distance < trackLength
                          && Horses[5].distance < trackLength) {
                          racing = true;
                    }
                    else {
                          racing = false;
                    }
                    // check if this is leading horse and change lead and winner values if it is
                    reportPos(Horses);
Â
             }
             for (Horse &h : Horses) {
                    if (h.distance > lead) {
                          lead = h.distance;
                    }
             }
             cout << "The winning distance was " << lead << " units!" << endl;
             int index = 0;
             for (Horse &h : Horses) {
                    if (lead == h.distance) {
                          break;
                    }
                    index++;
             }
             if (hNum == winner) {
                    cout << "Your horse won!" << endl;
                    cash = (cash + (bet * 6));
                    racing = false;
             }
             else {
                    cout << "Your horse didn't win!" << endl;
                    racing = false;
             }
             cin.get();
             if (cash <= 0) {
                    cout << "Uh-oh! You ran out of money!" << endl;
                    system("pause");
                    playing = false;
             }
             clearData(Horses);
             // END OF GAME LOOP
      }
      // Wait for user input to close program when debugging.
      cin.get();
      return 0;
      system("pause");
}
//--end of main program-------------
//----------------------------------
Â
//**begin function definitions******
// Get horse names
void getName(Horse &Nag) {
      // ask for horse's name
      cout << "Enter a horse's name: ";
      cin >> Nag.name;
}
// Racing events
int checkEvents(Horse &Nag) {
      int die1 = rand() % 6 + 1;
      int die2 = rand() % 6 + 1;
      int sum;            // sum of dice roll
      sum = (die1 + die2);
      // roll one 16 sided die to check on event (see instructions for event)
      // use switch statement to handle event
      // case 0, 1: horse breaks stride lose 1-2
      // case 2, 3: horse find its stride gain 1-2
      // case 4: horse stumbles lose 4-6
      // case 5: horse has energy burst gain 4-6
      switch (sum) {
      case 0 || 1:
             sum = (sum - ((rand() % 2) + 1));
             cout << "----" << Nag.name << " breaks stride!" << endl;
             break;
Â
      case 2:
             sum = (sum + ((rand() % 2) + 1));
             cout << "----" << Nag.name << " found stride!" << endl;
             break;
Â
      case 3:
             sum = (sum + ((rand() % 2) + 1));
             cout << "----" << Nag.name << " found stride!" << endl;
             break;
Â
      case 4:
             sum = (sum - ((rand() % 3) + 4));
             cout << "----" << Nag.name << " stumbles" << endl;
             break;
      case 5:
             sum = (sum + ((rand() % 3) + 4));
             cout << "----" << Nag.name << " gained a burst of speed!" << endl;
             break;
      default:
             cout << "No changes in the offset for " << Nag.name << endl;
             sum = sum + 0;
             break;
      }
Â
      return sum;
Â
}
Â
// Update positions
int updatePos(Horse &Nag) {
      // simulate rolling two dice using rand()%6+1
      // sum up the total of the two dice
      // add the horse's offset to the sum
      // if the sum is negative make it the new offset
      // else add the sum to the distance and clear offset
      // return the distance
Â
      int die1 = rand() % 6 + 1;
      int die2 = rand() % 6 + 1;
      int sum;            // sum of dice roll
      sum = (die1 + die2);
Â
      sum += Nag.offset;
Â
      if (sum < 0) {
             Nag.offset = sum;
      }
      else {
             Nag.distance += sum;
             Nag.offset = 0;
      }
      return Nag.distance;
}
// Report positions
void reportPos(array<Horse, horseCount> HArray) {
      // use range-based for loop
      // print horse position
Â
      for (Horse &h : HArray) {
             cout << h.name << " at marker " << h.distance << "." << endl;
      }
}
// Clear racing data
void clearData(array<Horse, horseCount> &HArray) {
      // use range-base for loop
      // clear distance
      // clear offset
Â
      for (Horse &h : HArray) {
             h.offset = 0;
             h.distance = 0;
      }
}
//--end of function definitions------
//----------------------------------
Â
Both of these are in C++ in microsoft visual studios
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly