Solved

Help with algorithm

Posted on 2000-02-13
2
205 Views
Last Modified: 2006-11-17
I got this algorithm for a function that solves Josephus algorithm. and I will like to translate it to c++.
In C langiage:

#include <stdio.h>
#include <stdlib.h>

int josef(int num, int skip)
{
char *vec;
int i;
int curPtr;
int numLive = num;

vec = (char *)malloc(num+1);
for(i=1 ; i<=num ; i++)
   vec[i] = 1;

curPtr = 1;
while(numLive > 1)
{
   i = 1;
   while(i<skip)
   {
      curPtr++;
      if(curPtr > num)
         curPtr = 1;

      if(vec[curPtr])
         i++;
   }

   vec[curPtr] = 0;  /* KILL him*/

   curPtr++;
   if(curPtr > num)
      curPtr = 1;


   numLive--;
}

/* Find the one LIVE */
for(i=1 ; i<=num ; i++)
   if(vec[i])
      return(i);

return(-1);
}

What I have done so far in C++:

Jos(n,m)
for i=0 to n
  {a[i]=i+1;}
while n>i  do
 for i=0 to n
 {
  if( a[i]!=0)
  {
   count=count+1;
   if (count==m)
   {
     a[i]=0;
     count=0;
    }
   }
  }

I'm I doing it okay?
what else do I need?
0
Comment
Question by:milalik
2 Comments
 

Author Comment

by:milalik
ID: 2517044
josephus problem is related to the following. Begining with n people arranged in a circle. Starting with the first person, count continuosly around the circle removing every m-th person until only one remains.Example n=7 and m=3
         1,2,3,4,5,6,7
          1,2,4,5,6,7
           1,2,4,5,7
            1,4,5,7
             1,4,5
              1,4
               4
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2517076
The C code _IS_ C++ code.  So you can just use that.  however it can be made a little more "C++ish"  (Also it has a memory leak that this fixes.)

#include <stdio.h>
#include <stdlib.h>

int josef(int num, int skip)
{
   char *vec = new char[num + 1];
    int numLive = num;

   for(int i=1 ; i<=num ; i++)
      vec[i] = 1;

   int curPtr = 1;
   while(numLive > 1)
   {
      int i = 1;
      while(i<skip)
      {
         curPtr++;
         if(curPtr > num)
            curPtr = 1;
         if(vec[curPtr])
            i++;
       }

      vec[curPtr] = 0;  /* KILL him*/
      curPtr++;
      if(curPtr > num)
         curPtr = 1;
      numLive--;
   }

   int RetVal = -1;

   /* Find the one LIVE */
   for(int i=1 ; i<=num ; i++)
      if(vec[i])
      {
          RetVal = i;
         break;
      }

    delete [] vec;
    return (RetVal);
}
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now