Jump to content

theADOLESCENT

Members
  • Posts

    9
  • Joined

  • Last visited

Profile Information

  • Favorite Area of Science
    Computer Science

theADOLESCENT's Achievements

Lepton

Lepton (1/13)

2

Reputation

  1. Hey everyone. It's been about 3 months since I've programmed and at that time I had just finished a semester of Software Design in Java. Now I'm in a C++ class and was asked to create this program. Mind you, we haven't been taught anything yet, first day was full of introductions. So we were told to read a few chapters and do this program. So between being rusty with my programming and never really programming in C++ before, I'm having a little trouble. On Wednesday he assigned 180 pages of reading along with 2 programming assignments which are due tonight. Just started the program this morning since I've been reading a lot so please don't think I was being lazy haha. The part I'm having trouble with is programming the "smart" computer. Here's what the assignment says. In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1 - that is, 3, 7, 15, 31, or 63 since the max pile size will be 100. That is always a legal move, except if the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move. I've just kind of been messing around trying to figure out something for the smart computer but have not been able to get it. I'm trying to code "start at pile size and move down each iteration, test to see if 2 raised to a number equals the pile size, then do (2^number)-1". I'm having trouble with figuring out the "number". This also may be way off, too. Any help is appreciated. Here it is: #include <iostream> using namespace std; int main() { int pileSize = 10 + rand() % 91; // generates a random pile size between 10 and 100 int turn = rand() % 2; // generates a 0 or 1 to determine who goes first, computer or player int marbleGrab = 0; // how many marbles are being taken out int smartOrStupid = rand() % 2; //0 for smart, 1 for stupid bool smartComputer; // setting level of "intelligence" of computer if(smartOrStupid == 0) { smartComputer = true; std::cout << "Computer is smart.\n"; } else { smartComputer = false; std::cout << "Computer is not so smart.\n"; } pileSize = 100; //take out before turn in std::cout << "Pile size is " << pileSize << "\n"; while(pileSize > 1) { if(turn == 0) // computer turn { std::cout << "Computer turn.\n"; if(!smartComputer) { marbleGrab = 1 + (rand() % pileSize)/2;; } else { for(int i = pileSize; i >= pileSize/2; i--) { for(int x = 10; x > 0; x--) { if(pow(2, x) == pileSize); marbleGrab = pow(2,x) - 1; } } } pileSize -= marbleGrab; std::cout << "The computer took " << marbleGrab << " marbles.\nThere are " << pileSize << " marbles left.\n"; } else // player turn { std::cout << "Player turn\nHow many marbles would you like to take?\n"; cin >> marbleGrab; while(!(marbleGrab <= pileSize/2 && marbleGrab > 0)) { std::cout << "You are trying to take too many or too little marbles.\nHow many marbles would you like to take?\n"; cin >> marbleGrab; } pileSize -= marbleGrab; std::cout << "The player took " << marbleGrab << " marbles.\nThere are " << pileSize << " marbles left.\n"; } turn = (turn + 1) % 2; } std::cout << "Pile size is " << pileSize << "\n"; if (turn == 0) cout << "Computer won.\n"; else cout << "Player won.\n"; return 0; }
  2. hahah I forgot the do in there for my do while loop. Thanks, it exits on a space.
  3. Hey guys. I'm almost finished with my program but I can't figure out what's not working. It works when I fill in the entries in the code but when I try to use the Scanner class to have a user input the characters, and then enter a space to stop, it doesn't work. I'm guessing there's something wrong with my loop since when I debug, I can enter a letter once, it sets the first entry to that letter, but cannot keep going after that in debugger. import java.util.Scanner; public class CombinationLockTester { /** * @param args */ public static void main(String[] args) { CombinationLock lock = new CombinationLock("ABC"); Scanner scan = new Scanner(System.in); String entryString = null; char entryChar = 0; { entryString = scan.nextLine(); entryChar = entryString.charAt(0); lock.setEntry(entryChar); } while(entryChar != ' '); /* lock.setEntry('A'); lock.setEntry('B'); lock.setEntry('C'); lock.setEntry('F'); lock.setEntry('A'); lock.setEntry('B'); lock.setEntry('C'); lock.setEntry('B'); lock.setEntry('C'); lock.setEntry('D'); */ //if space entered, try to open lock boolean didOpen = lock.openLock(); if(didOpen) System.out.println("The lock opened."); else System.out.println("The lock did not open."); lock.closeLock(); } } /** A lock like object that opens when the correct 3 letter combination is entered. * @author roseka27 * */ public class CombinationLock { private final String correctCombination; private char entry1; // most recently entered letter private char entry2; private char entry3; // oldest entry /** A constructor that creates the combination lock. * @param correctCombination The 3-letter combination to open lock. */ public CombinationLock(String correctCombination) { this.correctCombination = correctCombination; } /**Set a letter as the next piece of the combination being entered. * The last three calls to this method determine whether or not the lock is unlocked. * If this is called more than three times, only the last three calls matter. * @param entry Current letter being entered for entry. */ public void setEntry(char entry) { entry3 = entry2; entry2 = entry1; entry1 = entry; } /**Tries to open the lock. Will fail if the last three entries are not equal to the lock's built in combination. * @return True if the lock successfully opens, false if it doesn't open, such as with an incorrect combination. */ public boolean openLock() { if(correctCombination.charAt(0) == entry3 && correctCombination.charAt(1) == entry2 && correctCombination.charAt(2) == entry1) return true; else return false; } /**Resets the lock to an unset state where no entries have been entered. * */ public void closeLock() { entry1 = 0; entry2 = 0; entry3 = 0; } }
  4. Hey guys, this is my last homework assignment of the semester and it's on counting. I'm having a little trouble doing it. 1) A bridge hand consists of 13 cards from a standard 52 card deck. How many bridge hands contain cards from exactly two suits? I said step 1) pick suits (4 over 2) as in pick 2 suits from 4 suits step 2)pick cards (26 over 13) as in pick 13 cards from the 26 cards of the two suits you picked in step 1. Answer: (4 over 2)*(26 over 13) This one I feel pretty good about. 2)A club has 25 members: 10 men and 15 women. In how many ways can a committee of 5 be formed. Answer: (25 over 5) as in of all 25 people pick 5 for the committee. This one i feel pretty good about as well How many of those committees contain at least one woman? I did (10 over 5) to find out how many committees could be created of all men and then subtracted it from the total number of committees possible. Answer: (25 over 5)-(10 over 5) 3)We have a matrix algebra expression involving the multiplication of 12 matrices. In how many ways can we completely parenthesize the expression? This one I'm not really sure how to start. 4)We have three different children, and 10 different books. In how many ways can we distribute the books to the children (assume we are allowed to leave children empty-handed). Answer: 4^10 because you have 10 choices, one for each book; for each book decide if it goes to child #1, 2, 3 or none. This is if we are allowed to keep some of the books for our self and not give some or all away. I'm not sure how to do it for if all of the books have to be given away. Thanks for any help you guys can provide, theADOLESCENT
  5. Thanks a lot. I don't really get the stuff you're talking about near the end up looking ahead in the notes it looks like we could be learning something along those lines. theADOLESCENT
  6. Thanks, I understand how my #3 doesn't account for those, it's just the figuring out where to go from there haha. Would I do something like (5)(9)(1)(1)(5) + (5)(1)(9)(1)(5) + (5)(1)(1)(9)(5) = 675? My redone #2 is (5)(8)(7)(6)(4) with doing it your way for a product of 6,720.
  7. Hey guys, we just started learning "counting" and I have a few questions about my homework. 1) How many 5-digit integers start and end with an odd digit? I figure 99999 is the highest numbers so it would go like this. (5)(9)(9)(9)(5) because for the first number you have 5 choices (1,3,5,7,9), the 2nd,3rd,4th numbers have 10 choices (0,1,2,3,4,5,6,7,8,9) and the last has the same amount of choices as the first number. So I got 25,000 as my final answer. That one wasn't hard. 2)How many 5-digit integers begin with an odd digit, end with an odd digit, and consist of 5 different integers? Again, 99999 would be highest number possible if they didn't have to be all different. Here's what I get: (5)(9)(8)(7)(4 or 1?) because for first choice you have 5 choices (1,3,5,7,9), second you have (0,1,2,3,4,5,6,7,8,9)-your first choice so 9 choices, next you have minus first and 2nd choice so 8 choices, 3rd you have minus 1st,2nd, and 3rd so that's 7 choices, but I'm confused for the last one. If the first 4 choices are all odd, then you're left with 1 choice for the last number, if the first number is odd, but the 2nd, 3rd, and 4th are even, then you have 4 choices. I'm confused about that one... 3) How many 5-digit integers start with an odd digit, end with an odd digit, and contain exactly two zeroes. I did: (5)(9)(1)(1)(5) and got 225. I'm not 100% sure about this one... Any help is appreciated. Thanks, theADOLESCENT
  8. Hey guys I'm Kent. I'll mostly be on here to see if anyone wants to test out my programs or help me out; if I have time I'll try to do the same for anyone else ( college student/father here, so I can only do so much). I'm in my Junior year of college and will be graduating with a BS in Computer Science within the next few years. I hope to become either a programmer for a game developer or level designer. My other interests include video games(duh), like LoL, Mass Effect, Halo, BF3, as well as my family.
  9. Hey guys, I just finished my basic calculator in HTML/Javascript and I was wondering if anyone would be able to test it out. The coding isn't going to be 100% perfect since I'm fairly new at it and the teacher hasn't taught us all the ways to do things. So right now, I'm mostly looking to see if it works. Valid entries include #operator#, then hit =. Please don't enter like -4*+8. Both numbers can be negative, as well as decimals, but with one operator in between or in the case of a negative (-#*-#) is okay. I attached a notepad file of the code. Thanks! theADOLESCENT Edit: I can't find where you guys would see the attached file so I'll use the code snippet tool. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <!-- ************************************************* * * * Assignment 6 - Exercise 2 * * * Author: * * Course: COM SCI 201 * * Created On: 11/16/2012 * Last Modified On: 11/16/2012 * * ************************************************* --> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Assignment 6 - Exercise 2</title> </head> <body ><center><h1>Assignment 6 - Exercise 2</h1></center><hr style="width:90%"/> <script type="text/javascript"> var dblNum1, dblNum2, dblResult, intOp, intNum1Over, intY; intNum1Over = 0; intY = 0; intOp = 0; dblNum1 = 0; dblNum2 = 0; var intNum1Used; var strNum1, strNum2, strPre; intNum1Used = -1; strNum1 = ""; strNum2 = ""; function plus() { with(document.frmCalculator) { intOp = 1; intNum1Used = 0; } } function minus() { if(intOp != 0) { strNum2 = "-"; } if ((intNum1Over == 1) && (intOp == 0)) { //intnum1 = done intOp = 2; intNum1Used = 0; } else { if(intOp == 0) { strNum1 = "-"; } else { } } } function times() { intOp = 3; intNum1Used = 0; } function divide() { intOp = 4; intNum1Used = 0; } function percent() { intOp = 5; intNum1Used = 0; } function result() { with (document.frmCalculator) { //txaResult.value = strNum1 + "," + intOp + "," + strNum2; dblNum1 = parseFloat(strNum1); dblNum2 = parseFloat(strNum2); if(intOp == 1) { dblResult = dblNum1 + dblNum2; dblResult = dblResult.toFixed(5); txaResult.value = dblResult; } else { if(intOp == 2) { dblResult = dblNum1 - dblNum2; dblResult = dblResult.toFixed(5); txaResult.value = dblResult; } else { if(intOp == 3) { dblResult = dblNum1 * dblNum2; dblResult = dblResult.toFixed(5); txaResult.value = dblResult; } else { if(intOp == 4) { dblResult = dblNum1 / dblNum2; dblResult = dblResult.toFixed(5); txaResult.value = dblResult; } else { if(intOp == 5) { dblResult = (dblNum1/dblNum2)*100; dblResult = dblResult.toFixed(5); txaResult.value = dblResult + "%"; } } } } // end if op = 2 } // end if op = 1 } // end with } // end result function zero() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "0"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "0"; txaResult.value = strNum1; intNum1Over = 1; } } } function one() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "1"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "1"; txaResult.value = strNum1; intNum1Over = 1; } } } function two() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "2"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "2"; txaResult.value = strNum1; intNum1Over = 1; } } } function three() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "3"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "3"; txaResult.value = strNum1; intNum1Over = 1; } } } function four() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "4"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "4"; txaResult.value = strNum1; intNum1Over = 1; } } } function five() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "5"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "5"; txaResult.value = strNum1; intNum1Over = 1; } } } function six() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "6"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "6"; txaResult.value = strNum1; intNum1Over = 1; } } } function seven() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "7"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "7"; txaResult.value = strNum1; intNum1Over = 1; } } } function eight() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "8"; txaResult.value = strNum2; } else { strNum1 =strNum1 + "8"; txaResult.value = strNum1; intNum1Over = 1; } } } function nine() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 = strNum2 + "9"; txaResult.value = strNum2; } else { strNum1 = strNum1 + "9"; txaResult.value = strNum1; intNum1Over = 1; } } } function point() { with(document.frmCalculator) { if(intNum1Used == 0) { strNum2 =strNum2 + "."; txaResult.value = strNum2; } else { strNum1 = strNum1 + "."; txaResult.value = strNum1; } } } function reset1() { with(document.frmCalculator) { strNum1 = ""; strNum2 = ""; strPre = ""; dblNum1 = 0; dblNum2 = 0; intNum1Used = 1; dblResult = 0; intNum1Over = 0; intOp = 0; } } </script> <form name ="frmCalculator" id="frmCalculator" onsubmit=""> <table border="2"> <tr><td align="center"><textarea name="txaResult" rows="1" cols="45"></textarea></td></tr> <tr><td colspan="1"><input type="reset" onclick="reset1()" value=" Clear "/></td></tr> <tr> <td> <input type="button" value=" 7 " onclick="seven()"/> <input type="button" value=" 8 " onclick="eight()"/> <input type="button" value=" 9 " onclick="nine()"/> <input type="button" value=" + " onclick="plus()"/> </td> </tr> <tr> <td> <input type="button" value=" 4 " onclick="four()"/> <input type="button" value=" 5 " onclick="five()"/> <input type="button" value=" 6 " onclick="six()"/> <input type="button" value=" - " onclick="minus()"/> </td> </tr> <tr> <td> <input type="button" value=" 1 " onclick="one()"/> <input type="button" value=" 2 " onclick="two()"/> <input type="button" value=" 3 " onclick="three()"/> <input type="button" value=" * " onclick="times()"/> </td> </tr> <tr> <td> <input type="button" value=" 0 " onclick="zero()"/> <input type="button" value=" . " onclick="point()"/> <input type="button" value=" = " onclick="result()"/> <input type="button" value=" / " onclick="divide()"/> <input type="button" value=" % " onclick="percent()"/> </td> </tr> </table> </form> </body> </html>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.