open close locker problem
I keep getting the output I want up till pass 10 then I get the error
bash: line 12: 22449 Segmentation fault $file.o $args
not sure how to fix this problem
any pointers/advice on what I did wrong so I can fix it and not make the same mistake would be appreciative
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
using namespace std;
enum LockerStatus {
CLOSED = 0,
OPEN,
};
const int NUMLOCKERS = 100;
const int MAX = 10;
const int Delaytime1 = 8000;
const int Delaytime2 = 500;
const int Delaytime3 = 4000;
void show_lockers();
void printline();
void toggle_lockers(int pass);
void count_lockers(int pass);
int lockers[NUMLOCKERS + 1];
int position[MAX + 1][MAX + 1];
main()
{
unsigned pass;
cout << "At the start :" << endl << endl;
for (int i = 1; i <= NUMLOCKERS; i++)
lockers[i] = OPEN;
show_lockers();
std::this_thread::sleep_for(chrono::milliseconds(Delaytime1));
for (pass = 2; pass < 10; pass++ )
{
cout << "pass = " << pass << ": Toggling lockers ..." << endl;
toggle_lockers(pass);
show_lockers();
std::this_thread::sleep_for(chrono::milliseconds(Delaytime1));
}
for (; pass < 90; pass++ ) {
cout << "pass = " << pass << ": Toggling lockers ..." << endl;
toggle_lockers(pass);
std::this_thread::sleep_for(chrono::milliseconds(Delaytime2));
}
for (; pass <= 100; pass++ ) {
cout << "pass = " << pass << ": Toggling lockers ..." << endl;
toggle_lockers(pass);
show_lockers();
std::this_thread::sleep_for(chrono::milliseconds(Delaytime3));
}
show_lockers();
count_lockers(--pass);
return 0;
}
void show_lockers(void)
{
{
for (int i = 100; i > 0; i--)
if (lockers[i] == 1)
cout<< "O";
else
cout<<"C";
}
cout<<""<<endl;
}
// Print the hrozontal lines
// used in the ASCII diagram
void
printline()
{
}
void toggle_lockers(int pass)
{ for (int i = 1; i*pass<= 100;i++ ) //fixed the problem with this
{
if (lockers[i*pass] == 0)
lockers[i*pass] = 1;
else
lockers[i*pass] = 0;
}
}
void count_lockers(int pass)
{
int open = 0, closed = 0;
cout << endl << endl
<< "At the end of " << pass << " passes, " << endl
<< open << " lockers remain OPEN and "
<< closed << " lockers remain CLOSED."
<< endl << endl;
return;
}
|
Last edited on
1 2
|
const int NUMLOCKERS = 100;
int lockers[NUMLOCKERS + 1];
|
The elements of the array are in positions 0, 1, ... 100. The last valid position is
NUMLOCKERS (100)
In the loop,
1 2 3 4 5
|
for( int i = 1; i <= 100; i++ )
{
if (lockers[i*pass] == 0)
// ...
}
|
lockers[i*pass] results in out of bounds access to the array, when the value of
pass is greater than one.
perfect wow cant believe I missed that
helped me a lot thank you
I spent too much time overlooking that
changed it to that and it works now
for( int i = 1; i*pass <= 100; i++ )
{
if (lockers[i*pass] == 0)
// ...
}
Topic archived. No new replies allowed.