C++ Homework. Please help :-)
I've been acing my class up to this far however the teacher has given
an homework assignment that I just cannot seem to come up with an
algorithm to do this. I've put about 5 hours within the past day or
say pondering an algorithm that conforms to these rules:
1) Cannot use function(s), array(s)
2) Must ONLY use loop structures (for, do...while)
I have all the source setup BUT the algorithm. Anyways here is the
source (the question in included). Also note that I am not asking for
the answer; I just want a 'push' into find the correct answer.
Thanks :-)
//*****************************************************************
//
// Author:
// Date Created: 2/24/2009
// Revisions: None.
//
// Purpose:
//
// Input: Locker Number
// Output: Number of lockers that are open
//
//*****************************************************************
/*
A high school has 1000 students and 1000 lockers, one locker for each
student. On the first day of school, the principle plays the
following game:
She asks the first student to go and open all the lockers. She then
asks the
second student to go and close all the even-numbered lockers. The
third
student is asked to check every third locker. If it is open, the
student closes
it; if it is closed, the student opens it. The fourth student is
asked to check
every fourth locker. If it is open, the student closes it; if it is
closed, the
student opens it. The remaining students continue this game. In
general, the
nth student checks every nth locker. If the locker is open, the
student closes it;
if it is closed, the student opens it. After all the students have
taken their
turn, some of the lockers are open and some are closed. Write a
program that
prompts the user to enter the number of lockers in a school. After
the game is
over, the program outputs the number of lockers that are opened. Test
run
your program for the following inputs: 1000, 5000, 10000. Do you see
a pattern
developing?
(Hint: Consider locker number 100. This locker is visited by student
numbers 1, 2, 4, 5, 10, 20, 25, 50, and 100. These are the positive
divisors
of 100. Similarily, locker number 30 is visited by student numbers 1,
2, 3, 5,
6, 10, 15, and 30. Notice that if the number of positive divisors of
a locker
number is odd, then at the end of the game the locker is opened. If
the number
of positive divisors of a locker number is even, then at the end of
the game the
locker is closed.)
**** In addition to the requirements presented in the text, write
your program
so that it may be run repetitively until a sentinel has been
presented.
*/
#include <iostream>
using namespace std;
int main()
{
//declare the input variable for amount of lockers and amount of
lockers open
int amountOfLockers = 0;
int openedLockers = 0;
const int SENTINEL = 001;
int closedLockers = 0;
cout << "This program will loop through the amount of lockers\n"
<< "in your input and ouput the ones that remain open.\n" << endl;
cout << "To exit the program at any time push 001" << endl << endl;
cout << "Please input the amount of lockers you wish to work with:
";
cin >> amountOfLockers;
while (amountOfLockers != SENTINEL)
{
for (int counter = 2; counter <= amountOfLockers; counter++)
{
}
cout << "To exit the program at any time push 001" << endl << endl;
cout << "Please input the amount of lockers you wish to work with:
";
cin >> amountOfLockers;
}
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]