0

I think I may have bitten off more than I can chew with this. I'm writing a program that will figure out the last position standing by continuously counting the positions using prime numbers till only one is left. http://acm.uva.es/p/v100/10015.html is a link to the original problem.

the code I have so far is below it's not very good and has a lot of extra crap. If any of you has an idea on how to get it working I'd really appreciate it!

```
#include <iostream>
using namespace std;
//-------------------------
// Constants
//-------------------------
int MAX_PERSONNEL = 100; // capacity of the array
//-------------------------
// Global Variables
//-------------------------
//-------------------------
// Function Declarations
//-------------------------
// getNumOfPersonnel:
// 1. read an integer from user as # of personnel and save in "size"
bool getNumOfPersonnel(int& size/*OUT*/);
// Pre: None
// Post: Return false if size is bigger than MAX_PERSONNEL;
// otherwise: size got a value in range [1, MAX_PERSONNEL];
// return true.
// numberPersonnel:
// 1. Place a numbers in the array for use in counting
void numberPersonnel(int arr[]/*OUT*/, int& size/*IN*/);
// Pre: getNumOfPersonnel must be valid.
// Post: Array will be filled in and ready for exicutions.
// primNum
// 1. Determine all prime numbers
void primeNum(int& size/*in*/);
// Pre: GetNumOfPersonnel must be filled in.
// Post: Will show all prime numbers for given number of peronnel.
// exicutePersonnel:
// 1. Place a numbers in the array for use in counting
int exicutePersonnel(int& size/*IN*/);
// Pre: numberPersonnel array must be filled in.
// Post: All personnel will be counted to the nth prime number untill only one remains.
// printArray: display content of array on screen
void printArray(const int arr[]/*IN*/, int size/*IN*/);
// Pre: User will have input values into the array
// Post: Will print user input array values
//-------------------------
// main
//-------------------------
int main()
{
cout << "This program is designed to determine which position will save you incase you\n";
cout << "are caught and about to be exicuted by Joseph's Cousin!!\n" << endl;
cout << "Joseph's Cousin likes to exicute every nth person in a group of people\n";
cout << "that have been grouped into a circle, starting with the person at the\n";
cout << "12 o'clock possition and moving counter clockwise...\n" << endl;
cout << "He continues counting after the first time around never resetting\n";
cout << "the count to zero untill there is only one person remaining.\n" << endl;
cout << "the nth person to be killed is determined by counting to a prime number\n";
cout << "and killing that person then continuing on to the next person in line!\n" << endl;
cout << "This program will determine which possition you need to be standing in order\n";
cout << "to survive the encounter and live to program another day!!\n";
cout << endl << endl << "GOOD LUCK!!!!!\n";
cout << "Please enter how many personnel are in your group\n";
// variables
int perArray[MAX_PERSONNEL]; // store numbers read in from keyboard
int size; // # of numbers actually read in
//int maxPrime = 4000; // Max number for determining prime numbers.
// read in size and fill array
if ( !getNumOfPersonnel(size) ) // if failed
{
cout << endl << "Can't fill the array. Program aborted." << endl;
exit(1);
}
primeNum(size);
numberPersonnel(perArray, size);
cout << exicutePersonnel(size);
//cout << exicutePersonnel(perArray, size);
// Print array
cout << endl << "After user input: " << endl;
printArray(perArray, size);
//cout << exicutePersonnel;
return 0;
}
//-------------------------
// Function Definitions
//-------------------------
bool getNumOfPersonnel(int& size/*OUT*/)
{
// read in size
cout << "Enter # of numbers you have (1 ~ " << MAX_PERSONNEL << "): ";
cin >> size;
// check if valid size
if (size > MAX_PERSONNEL || size <=0)
{ // invalid
cout << endl << "Invalid size. Valid range is 1 to " << MAX_PERSONNEL << ". " << endl;
return false;
}
return true;
}
void numberPersonnel(int arr[]/*OUT*/, int& size/*IN*/)
{
// fill in the array
for (int i=0; i<size; i++)
{
arr[i] = 0;
arr[i] += 1;
}
return;
}
void primeNum(int& size/*in*/)
{
bool isPrime=true;
for ( int i = 0; i <= size -1; i++)
{
for ( int j = 2; j <= size - 1; j++)
{
if ( i!=j && i % j == 0 )
{
isPrime=false;
break;
}
else if ( i==1 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true;
}
}
int exicutePersonnel(int& size/*IN*/)
{
int i, s;
for (s = 0, i = 1; i <= size; i++)
s = (s + (size - i)) % i;
return (s - 1);
}
void printArray(const int arr[]/*IN*/, int size/*IN*/)
{
// loop through the array and print each element
for (int i=0; i<size; i++) {
// print one number
// cout << " [" << i << "]: " << arr[i] << endl;
}
cout << endl;
return;
}
// end of program
```

*Edited 6 Years Ago by Nick Evan*: Added code-tags