In: Computer Science
For C++ Use arrays and or vectors, no classes.
Visualize and consider 100 lockers all lined up horizontally in
a row
Each locker is numbered from 1 to 100 in sequential order
Every locker can be fully closed (open state = 0.00)
Every locker can be fully opened (open = 1.00)
Every locker can be partially open with any possible value between
0.00 and 1.00 inclusive on both ends
A locker cannot ever be more closed than fully closed (open cannot
be less than 0.00)
A locker cannot ever be more open than fully opened (open cannot be
greater than 1.00)
All 100 lockers start in the fully closed state
100 students will be walking by the lockers and opening/closing
them in the following manner:
The 1st student will OPEN EVERY LOCKER 1/2 of the way (ADD 0.50 TO LOCKERS 1,2,3,4...)
The 2nd student will CLOSE EVERY SECOND LOCKER 1/3 of the way (SUBTRACT 0.333333333333 FROM LOCKERS 2,4,6,8...)
The 3rd student will OPEN EVERY THIRD LOCKER 1/4 of the way (ADD 0.25 TO LOCKERS 3,6,9,12...)
The 4th student will CLOSE EVERY FOURTH LOCKER 1/5 of the way (SUBTRACT 0.20 FROM LOCKERS 4,8,12,16...)
The 5th student will OPEN EVERY FIFTH LOCKER 1/6 of the way (ADD 0.166666666666 TO LOCKERS 5,10,15,20...)
The 6th student will CLOSE EVERY SIXTH LOCKER 1/7 of the way (SUBTRACT 0.142857142857 FROM LOCKERS 6,12,18,24...)
The 99th student will OPEN EVERY 99TH LOCKER 1/100 of the way (ADD 0.01 TO LOCKER 99)
The 100th student will CLOSE EVERY 100TH LOCKER 1/101 of the way (SUBTRACT 0.009900990099 FROM LOCKER 100)
NOTE: Remember that the locker open state must always stay within 0.00 <= value <= 1.00
1) Develop C++ code that will generate the open state values for
all 100 lockers
2) Also develop C++ code that will output answers to the following
questions:
Which lockers are left fully closed (open state == 0.00)?
Which lockers are left fully open (open state == 1.00)?
Which locker is the one opened the least and what is its value (open state closest to 0.00)?
Which locker is the one closed the least and what is its value (open state closest to 1.00)?
#include<iostream>
using namespace std;
double perform(int n) //method to calculate how much
open/close to perform
{
double value = (double)1.0/(n+1);
if(n%2 == 0) //if # of person is even, locker
should be closed
value = value * (-1); //so multiply
-1
return value;
}
int main()
{
double locker[100],howmuch;
int i,j;
for(i=0;i<100;i++)
locker[i] = 0.0; //initialising value of
locker
for(i=1;i<=100;i++)
{
howmuch = perform(i);
//calculate how much to open/close
for(j=(i-1);j<100;j=j+i)
{
locker[j] =
locker[j] + howmuch; //open close the locker
if(locker[j]<0) //see if value<0 or
>1
locker[j] =
0;
else
if(locker[j]>1)
locker[j] =
1;
}
}
//un-comment the next part to display
status of every locker
/*
for(i=1;i<=100;i++)
{
cout << i << "\t"
<< locker[i-1] << "\n";
}
*/
double min=1,max=0; //set minimum and
maximum
int openpos,closepos;
cout << "\nFully closed lockers:\t";
for(i=1;i<=100;i++)
{
if(locker[i-1]==0) //if
locker is fully closed, print
cout << i << ",";
}
cout << "\nFully open lockers:\t";
for(i=1;i<=100;i++)
{
if(locker[i-1]==1) //if
locker is fully open, print
cout << i << ",";
}
for(i=1;i<=100;i++)
{
if(locker[i-1]>0 &&
locker[i-1]<min) //if locker is more closed than min but
not 0, make this min
{
openpos =
i;
min =
locker[i-1];
}
if(locker[i-1]<1 &&
locker[i-1]>max) //if locker is more open than max but
not 1, make this max
{
closepos =
i;
max =
locker[i-1];
}
}
cout << "\n\nLocker " << openpos <<
" is least opened with value " << min;
cout << "\nLocker " << closepos << "
is least closed with value " << max;
}
Sample execution:
NOTE: Un-comment the specified part to get status of every locker.
THANK YOU!! PLEASE VOTE