Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here's a link to the problem in question: https://code.google.com/codejam/contest/6224486/dashboard#s=p1
Okay, so I got a bit hung up on this problem. Now that the quals are over, does anyone know why this wouldn't work? I've checked against about 50 different cases and haven't been able to find one that doesn't work. Here's my full code below.
Basically, looking for any cases where my algorithm could break.
//Includes
#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <stdlib.h>
using namespace std;
//Functions
int solve(list<int>);
//Main
int main() {
int minutes = 0;
int numTestCases = 0;
int initNonEmpty = 0;
char tempChar;
int tempInt;
list<int> people;
//Import Data
//Get number of Test cases
ifstream infile;
infile.clear();
infile.open("B-small-attempt5.in");
//get NumTestCases
infile >> numTestCases;
int solution[numTestCases];
//Solve it
for(int i=0; i<numTestCases; i++){
//Reset vars
initNonEmpty=0;
infile >> initNonEmpty;
people.clear();
//Input data
for(int j=0; j<initNonEmpty; j++){
infile >> tempInt;
cout << tempInt;
people.push_back(tempInt);
}
cout << endl;
//Solve the set
people.sort();
people.reverse();
tempInt = solve(people);
cout << "Solve returns: " << tempInt << endl << endl;
solution[i] = tempInt;
}
//Output
ofstream outfile;
outfile.open("RealCase6.out");
for (int i=0; i < numTestCases; i++){
cout << "Case #" << i+1 << ": " << solution[i] << endl;
outfile << "Case #" << i+1 << ": " << solution[i] << endl;
}
}
int solve( list<int> data){
cout << "Starting Solve functions." << endl;
int tempMax;
int max =data.front();
int test=0;
cout << "Max: " << max << endl;
//Test if base case
if(max<=3){
cout << "Reached base case." << endl;
return max;
}
else if (max % 2 == 0 ){
cout << "Max is even" << endl;
tempMax = max/2;
data.pop_front();
data.push_back(tempMax);
data.push_back(tempMax);
data.sort();
data.reverse();
test=solve(data);
test=test+1;
cout << "test is:" << test << endl;
cout << "max is:" << max << endl;
if( test<=max){
return test;
} else {
return max;
}
}
else {
cout << "Max is odd" << endl;
tempMax = max/2;
data.pop_front();
data.push_back(tempMax);
data.push_back(tempMax+1);
data.sort();
data.reverse();
test=solve(data);
test=test+1;
cout << "test is:" << test << endl;
cout << "max is:" << max << endl;
if(test<=max){
return test;
} else {
return max;
}
}
}
And here's my input/output for 6 different cases.
https://www.dropbox.com/sh/55wq52lzuygd82s/AABYxJJ7zaeoMhgCmymJcwnAa?dl=0
I'll remove this if this is too early to start asking about the problems. Sorry about eh formatting, but I was trying to get this done quickly.
Edit1: Added Link to problem.
Edit2: Someone else found the error. I only thought to divide things into groups of 2, which gives a case of one person with 9 pancakes, where dividing it into a group of 6 and 3 and then 3 3 3 works best.
This breaks in cases where it's more efficient to divide into non-equivalent stacks. For example, the case
1
9
where one person has 9 pancakes. Dividing as evenly as possible into groups of 2 gives the solution:
min 1: 9
min 2: 5 4
min 3: 4 3
min 4: 3 2
min 5: 2 1
min 6: 1 0
min 7: 0 0
where as dividing unevenly gives the solution
min 1: 9
min 2: 3 6
min 3: 3 3 3
min 4: 2 2 2
min 5: 1 1 1
min 6: 0 0 0
which is more efficient.
Related
I'm trying to make a Sudoku puzzle generator for a school project, and I have most of the coding and logic worked out for the program, it's just that the random number generator I'm using to assign values to the Sudoku grid are generating numbers far out of the appropriate range, and I can't quite figure out why it's not functioning appropriately.
Here's the code for the number generator:
int Sudoku::RNG(int range, int start){
int randNum;
randNum = (rand()%range+start);
return randNum;
}
I've also seeded with
srand(time(NULL));
at the very start of my main method.
And here's the code for the method which populates the Sudoku grid with numbers from the RNG() function:
void Sudoku::gridPopulate(int grid [9][9]){
Solution s;
int num;
bool safe;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
do{
safe = false;
num= RNG(9,1);
if(s.rowCheck(grid, i, num)&& s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)){
grid[i][j] = num;
cout << "Number entered into grid" << endl;
safe = true;
}
}while(safe);
}
}
Main Method:
int main()
{
//Program Start
srand(time(NULL));
int difficulty;
int choice;
int grid[9][9];
cout << "Welcome to my Sudoku puzzle generator! \nTest your mental muscles and see if you can solve the puzzle!" << endl;
//Puzzle Generator
Sudoku game;
game.gridPopulate(grid);
cout << "board populated"<< endl;
cout << "Successful board made" << endl;
game.displayBoard(grid);
}
Example of erroneous Sudoku board below (formatting is off because of the huge numbers, just ignore the dashes and vertical lines):
1 5013192 4981028 9 |20064 8 5 |1 2 3 |
2 8 5 4 |1995768265 0 2114351727 |28 7670880 7670908 |
3 1 7274056 7 |3 0 7670880 |4 6 7670880 |
-----------------4 6 7 2 |4199040 1 8 |7670912 -1196314433 4 |
5 1953657218 1 0 |5 4 32 |7 3 7274140 |
6 5 8 1953722109 |2 9 7670916 |7274116 4199040 4358512 |
-----------------7 0 1953722297 1 |9 1953787893 3 |7274204 4 8 |
8 1953722109 1953722083 8 |4199040 4199040 0 |9 7274160 2 |
9 3 1953746112 1198757840 |6 7274216 4 |4358512 7274368 4358606 |
-----------------
1 2 3 4 5 6 7 8 9
The number's it's giving me are sometimes very large, sometimes not. It's kinda baffling me at the moment, any help or advice would be greatly appreciated!
EDIT/UPDATE #1:
I've revised my gridPopulate code, and I've added in a few cout statements for troubleshooting. The method will now stay on each individual array element until it is populated, the main issue I'm running into now is that the code will get stuck in the do/while statement where the RNG() is occurring. I'm using the counter to track how many RNG iterations it goes through before passing a safe value into the grid. The problem is that the RNG is going through way too many iterations before happening to generate a safe value, sometimes thousands of iterations before moving on. At some point it will just get stuck endlessly generating random numbers without being able to move on.'
void Sudoku::gridPopulate(int grid [9][9]){
Solution s;
int num;
do{
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
int counter = 0;
do{
srand(time(NULL));
num = RNG(9,1);
counter++;
cout << "RNG attempts: " << counter << endl;
}while(!(s.rowCheck(grid, i, num)&& s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)));
grid[i][j]=num;
cout << grid[i][j] << endl;
cout << "Row #: " << i << endl;
cout << "Col #: " << j << endl;
}//end j loop
displayBoard(grid);
}//end i loop
displayBoard(grid);
}while(!validityCheck(grid));
Here's my displayBoard method too, though I don't think it is correlated to the problem.
void Sudoku::displayBoard(int grid[9][9]){
/**********************************************************/
cout << " ----------------------" << endl;
for(int i=0; i<9;i++){
if(i==0)
cout << "1 | ";
if(i==1)
cout << "2 | ";
if(i==2)
cout << "3 | ";
if(i==3)
cout << "4 | ";
if(i==4)
cout << "5 | ";
if(i==5)
cout << "6 | ";
if(i==6)
cout << "7 | ";
if(i==7)
cout << "8 | ";
if(i==8)
cout << "9 | ";
for(int j=0;j<9;j++){
cout << grid[i][j] << " ";
if(j==2)
cout << "| ";
if(j==5)
cout << "| ";
if(j==8)
cout << "| ";
}
cout << endl;
if(i==2)
cout << " ------------------------" << endl;
if(i==5)
cout << " ------------------------" << endl;
if(i==8)
cout << " ------------------------" << endl;
}
cout << "\n";
cout << " 1 2 3 4 5 6 7 8 9 " << endl;
/**********************************************************/
}
Nothing wrong with the RNG function, all we need is debugging skills
## Try this to see what is the immediate value of the grid[i][j] ##
`void Sudoku::void gridPopulate(int grid[9][9])
{
Solution s;
int num;
bool safe;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
do {
safe = false;
num = RNG(9, 1);
if (s.rowCheck(grid, i, num) && s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)) {
grid[i`enter code here`][j] = num;
//cout << "Number entered into grid" << endl;
safe = true;
}
} while (safe);
cout << grid[i][j] << " ";
}
cout << endl;
}
}`
This question already has an answer here:
C++ - using enums to generate a deck of cards
(1 answer)
Closed 2 years ago.
it's in c++
The idea was to set it up so that it will keep looping through all the cards to keep getting different results and answers until all 52 cards are gone i dont know the exact placmet for it I know its
for(int i = 0; i < 5; i++)
{
cout << i << endl;
}
I wasn't quite sure how to set up the array If I just did it like string {ace, one two} and so on.. but I have the array labeled 52 even typing them all out its only 13 number repeated 4 times in 4 suits so would you class them all separately? Or is there something for that?
#include <iostream>
using namespace std;
class cardGame;
int main()
{
int bet;
int dealer[52] = { 13, 13, 13, 13 };
int player[52] = { 13, 13, 13, 13 };
int dealerCard1;
int dealerCard2;
int playerCard1;
int playerCard2;
int dealerTotal;
int playerTotal;
int shuffle;
cout << "This is My attempt at BlackJack" << endl;
cout << endl;
cin >> bet;
cout << "Player enter amount to bet: $ ";
cout << endl;
cin >> shuffle;
cardGame::playeer(playerCard1 + playerCard2 = playerTotal);
cardsGame.playerCard1 = 0;
cardsGame.playerCard2 = 0;
cardsGame.playerTotal = 0;
cout << "the First card is: " << cardsGame.playerCard1 = 0 << endl;
cout << "The second card is: " << cardsGame.playerCard = 0 << endl;
cout << "Your total is: " << cardsGame.playerTotal = 0 << endl;
cardGame::playeer(playerCard1 + playerCard2 = playerTotal);
if (playerCard1 + playerCard2 == 21)
{
cout << "WINNER WINNER CHICKEN DINNER!" << endl;
};
else (playerCard1 + playerCard2 > 21)
{
cout << "What a dissapointment you are to everyone!" << endl;
};
if (playerCard1 + playerCard2 > dealerTotal)
{
cout << "WINNER WINNER CHICKEN DINNER!" << endl;
};
else (playerCard1 + playerCard2 == dealerTotal)
{
cout << "What a dissapointment you are to everyone!"
}
cardGame::dealer(dealerCard1 + dealerCard2 = dealerTotal);
cardsGame.dealerCard1 = 0;
cardsGame.dealerCard2 = 0;
cardsGame.dealerTotal = 0;
cout << "the First card is: " << cardsGame.dealerCard1 = 0 << endl;
cout << "The second card is: " << cardsGame.dealerCard2 = 0 << endl;
cout << "Your total is: " << cardsGame.dealerTotal = 0 << endl;
cardGame::dealer(dealerCard1 + playerCard2 = playerTotal);
if (dealerCard1 + dealerCard2 == 21)
{
cout << "What a dissapointment you are to everyone!" << endl;
};
else (dealerCard1 + dealerCard2 > 21)
{
cout << "WINNER WINNER CHICKEN DINNER" << endl;
}
if (dealerCard1 + dealerCard2 > playerTotal)
{
cout << "What a dissapointment you are to everyone!" << cout endl:
};
else (dealerCard1 + dealerCard2 < playerTotal)
{
cout << "What a dissapointment you are to everyone!"
}
}
I don't know c++ (or blackjack); but below is an example in Javascript which illustrates some concepts and might push you in the right direction.
In terms of structure you might want something like:
// Set up your 'deck'
deck = {
hearts: [],
spades: [],
diamonds: [],
clubs: []
}
// The object 'deck' has four properties (hearts, spades...), each of which is an empty array.
// Use a loop to add your 'cards'
Object.keys(deck).forEach(suit => {
for (i = 1; i < 14; i++) {
deck[suit].push(i);
}
});
// For each property in the object 'deck', push ascending consecutive integers starting from the number 1 to each array, until the number 14 is reached.
This gives you:
deck = {
hearts: [1,2,3,4,5,6,7,8,9,10,11,12,13],
spades: [1,2,3,4,5,6,7,8,9,10,11,12,13],
diamonds: [1,2,3,4,5,6,7,8,9,10,11,12,13],
clubs: [1,2,3,4,5,6,7,8,9,10,11,12,13]
}
I am a beginner in programming. I was doing some simple applications. I was already done with my programme when I realised that my variable is getting random, even if I set it to 0. I was trying to figure it out why. My goal is to add 1 to the "cor" variable when the answer is matching the random generated number, the "else" section is working as it has to be. Maybe someone more experienced can help me.
`
#include <iostream>
#include <string>
#include <time.h>
#include <Windows.h>
using namespace std;
int number;
int ynumber[5];
int cor = 0;
int falsed = 0;
int main()
{
cout << "Welcome to our lottery!" << endl;
cout << "We start in ..." << endl;
Sleep(2000);
for (int i = 3; i >= 0; i--)
{
system("cls");
cout << i << endl;
Sleep(1000);
}
system("cls");
srand(time(NULL));
for (int i = 0; i < 6; i++)
{
cout << "Type your " << i+1 << " number below" << endl;
cin >> ynumber[i];
number = rand() % 50 + 1;
cout << "The picked number is: " << number << endl;
Sleep(1000);
if (ynumber[i] == number)
{
cout << "Same same!" << endl;
cor = cor + 1;
}
else
{
cout << "Hope for better luck next time ;)" << endl;
falsed = falsed + 1;
}
}
system("cls");
cout << "Thank you for participating!" << endl << "Correct picked numbers: " << cor << endl << "Wrong picked numbers: " << falsed << endl << endl;
system("pause");
return 0;
}
`
int ynumber[5];
The length of your array is 5
for (int i = 0; i < 6; i++)
{
//...
cin >> ynumber[i];
You loop over the indices 0,1,2,3,4,5. Use your fingers to count the number of indices that you use. You'll notice that you access the array at 6 different fingers. 6 is more than 5. As such, we can conclude that you're accessing the array out of bounds. The consequence is that behaviour of your program is undefined.
Solution: Do not access an array out of bounds. The last index of array of length n is n - 1.
More generally: Don't rely on magic numbers. In this case, you could use instead:
for (int i = 0; i < std::size(ynumber); i++)
Attempting to create my first program Expected behaviour:
1. User inputs an integer (lets say 7)
2. User continues to input same integer more times (lets say 4 total)
3. User inputs 8
4. Program then prints ("the count of 7 was 4")
5. User continues to enter 8 (lets say a 8 was entered a total of 11 times)
6. User enters 73
7. Program then prints ("the count of 8 was 11")
After step 4, the program gets stuck and any inputs yeild "7 was entered 0 times"
#include <iostream>
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
if(std::cin >> firstvalue){
++cnt;
while(std::cin >> inputtedvalue){
if(inputtedvalue == firstvalue)
++cnt;
else{
std::cout << "the count of " << firstvalue <<" is " << cnt << std::endl;
inputtedvalue = firstvalue;
cnt= 1;
}
}
}
}
I was looking over your example, and i believe the following might get you back in the right direction.
using namespace std;
int main(){
int inputtedvalue = 0;
int firstvalue = 0;
int cnt = 0;
cout << "Enter Starting value."<< std::endl;
if (cin >> firstvalue){
cout << "New count " << firstvalue <<" of " << cnt << endl;
while(cin >> inputtedvalue)
{
if(inputtedvalue != firstvalue)
{
cout << "It Doesn't Match" << endl;
firstvalue = inputtedvalue;
}
else
{
cout << "It Matches" << endl;
}
}
}
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string.h>
using namespace std;
int main()
{
char pin[6]; //character array to allow for ease of input
int randomNumbers[10]; //holding the randomized numbers that are printed back to the user
int pinStorage[5];
char pinVerify[5];
char pinReenter[6];
int result;
srand(time(0));
cout << "Enter your pin number" << endl;
cin >> pin;
for (int i = 0; i < 5; i++)
{
pinStorage[i] = pin[i] - '0';
}
for (int i = 0; i < 10; i++)
{
randomNumbers[i]= rand() % 3 + 1;
}
cout << "Pin Verify: ";
for (int b = 0; b < 5; b++)
{
for (int d = 0; d <10; d++)
{
if (pinStorage[b]== d)
{
pinVerify[b] = randomNumbers[d];
switch (pinVerify[b])
{
case 1: pinVerify[b] = '1';
break;
case 2: pinVerify[b] = '2';
break;
case 3: pinVerify[b] = '3';
}
cout << pinVerify[b] << " ";
}
}
}
cout << " " << endl;
cout << "Pin : 0 1 2 3 4 5 6 7 8 9" << endl;
cout << "Number: ";
for (int c = 0; c < 10; c++)
{
cout << randomNumbers[c] << " ";
}
cout << " " << endl;
cout << "Renter pin" << endl;
cin >> pinReenter;
for(int h = 0; h < 5; ++h)
{
int digit = pinStorage[h];
pinReenter[h] = randomNumbers[digit] + '0';
}
cout << "PV: " << pinVerify;
cout << "PR: " << pinReenter;
result = (strcmp(pinVerify, pinReenter));
switch(result)
{
case 1: cout << "You did not enter the pin correctly!" << endl;
break;
case 0: cout << "Pin Entered Correctly!" << endl;
break;
case -1: cout << "You did not enter the pin correctly!" << endl;
}
The above is my code. The objective is to be able to enter a normal 5 digit pin such as 12345. The program will then store that pin, and produce a screen like this:
Pin: 0 1 2 3 4 5 6 7 8 9
Num: 3 1 2 3 2 2 3 1 1 3
The program randomized the num part that you see and assigns it to the numbers above. That way the user reenters 12322 and the computer recognizes it as him entering the right code. The next time it where to do it, it would be rerandomized to something else. This whole project is designed to prevent shoulder surfing.
However I seem to be having a problem with my code. Almost everything is working but with the final strcmp function. Even though I have pinVerify set to 5, it still gives me 5 numbers and a ASCII character at the end, something like 21323☻. This makes it impossible to ever have pinVerify and pinReenter as equal, meaning it's impossible for the program to tell you you entered in the challenge correctly, even when you did.
Can anyone help me fix this? I've been looking and tinkering for a while and have been completely unsuccessful.
C-style strings are null-terminated. In your example, it means that, when you want to work with two strings of length 5, you should actually reserve 5+1=6 chars for each of them, and make sure that the 5-th (0-indexed) char contains a character with ASCII code 0. Otherwise, strcmp, and any other C-string function, proceeds past the end of your char[5] array until it finds a byte containing zero.