Thread: Problem understandnig...pls help

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Problem understandnig...pls help

    hi everyone, can someone help me explain no what is this code means?

    Code:
    bool FindNumber(int iPos)
    {
        int iNumberTwo, iTotal;
    
        if(iPos < 1)
        
        return false;
        if (iPos == number)
        {
            iTotal = array[iPos-1] + array[0];
            if (IsPrime(iTotal))
            { 
                for(int j=0; j< number; j++)
                {
                    printf("%d ", array[j]);
                }
                printf("\n");
                return true;
            }
            else
            return false;
        }
        
        iNumberTwo = GetNumber(iPos, 0);
        while(iNumberTwo != 0)
        {
            array[iPos] = iNumberTwo;
            iTotal = array[iPos]+array[iPos-1];
            if (IsPrime(iTotal))
            {
    //   printf("Now in %d, try %d, and %d is prime\n", iPos, ring[iPos], iTotal);
    //   for(int i=0; iĦq =iPos; i++)
    //   printf("%d ", ring[i]);
    //   printf("\n\n");
                if (FindNumber(iPos+1) == true)
                {
    // iPos --
                }      
            }
            iNumberTwo = GetNumber(iPos, iNumberTwo);
        }
        return false;
    }

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You don't understand ANY of it???? Take a look at the tutorials, especially the tutorials covering if-statments, loops, and functions.

    I could comment every line... but I don't have the time right now. And, it's going to be hard to understand if you don't already know what an array is, or what a function is.

    It's a single function, not a complete program. It calls at least two other functions which are not defined here: IsPrime() and GetNumber().

    Also, printf() is unusual in C++. (It's a C function.)

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    ok thank you for replying, i understand those. and this is the whole program...

    Code:
    // Problem A: Prime Ring
    
    #include <stdio.h>
    #include <iostream>
    #include <cstdlib>
    #include <math.h>
    using namespace std;
    
    bool NotInRing(int iPos, int iNum);
    int GetNumber(int iPos, int iNumber);
    bool IsPrime(int iNum);
    bool FindNumber(int iPos);
    
    int ring[16];
    int m;
    
    int main()
    {
        int  i, j, k;
    //  insert the 1 value into first position which is ring[0]
        ring[0] = 1;
    
        while (scanf("%d", &m))
        {
            if (m == 0)
            break;
    
    //  printf("Get %d\n", m);
            if (FindNumber(1) == true)
            {
                for(j=0; j<m; j++)
                {
                    printf("%d ", ring[j]);
                }
                printf("\n");
            }
        }// end while.
        system("pause");
        return 0;
    }
    
    bool NotInRing(int iPos, int iNum)
    {
        for(int i=0; i<iPos; i++)
        {
            if (iNum == ring[i])
            {
                return false;
            }
        }
        return true;
    }
    
    int GetNumber(int iPos, int iNumber)
    {
        for(int i=iNumber+1; i<=m; i++)
        {
            if (NotInRing(iPos, i))
            return i;
        }
        return 0;  
    }
    
    bool IsPrime(int iNum)
    {
        for (int i=2; i<=sqrt((double)iNum); i++) //sqr/sqrt 
        {
            if (iNum == ((int)iNum/i)*i)		// (inum%i == 0)
            return false;
        }
        return true;
    }
    
    
    bool FindNumber(int iPos)
    {
        int iNumberTwo, iTotal;
        
        if(iPos < 1)
        
        return false;
    
        if (iPos == m)
        {
            iTotal = ring[iPos-1] + ring[0];
            if (IsPrime(iTotal))
            {
                for(int j=0; j< m; j++)
                {
                    printf("%d ", ring[j]);
                }
                printf("\n");
                return true;
            }
            else
            return false;
        }
        
        iNumberTwo = GetNumber(iPos, 0);
        while(iNumberTwo != 0)
        {
            ring[iPos] = iNumberTwo;
            iTotal = ring[iPos]+ring[iPos-1];
            if (IsPrime(iTotal))
            {
    //   printf("Now in %d, try %d, and %d is prime\n", iPos, ring[iPos], iTotal);
    //   for(int i=0; iĦq =iPos; i++)
    //   printf("%d ", ring[i]);
    //   printf("\n\n");
                if (FindNumber(iPos+1) == true)
                {
    // iPos --;
                }      
            }
            iNumberTwo = GetNumber(iPos, iNumberTwo);
        }
        return false;
    }
    this program works perfect for me , and this is exactly what i want but wat do you mean printf() is for c?

    if it is for c, and what is in c++?

    thank you.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    printf for C, cout for C++. scanf for C, cin for C++. math.h for C, cmath for C++. Like said, check out the tutorials on this site.

    As for the program, I'm not sure exactly what it does, and I'm not sure I'd spend the time doing so. Glancing over it I'd guess it allows you to add numbers to a global array, search the array for numbers, and check if its a prime number. See this is what commenting on the programs use, and functions use, etc. is good for.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    actually i already work it out. but still need to thank you for all who participate and helps.

    what is this code mean which is in the FindNumber function?

    Code:
     if(iPos < 1)
        
        return false;
        if (iPos == number)

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    by wat i think for the IPOS, i think is i position...
    well that is just wat i guess..

    not really sure....

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    That's a check for valid input. The function expects iPos (passed in by the function's caller) to be >= 1. If it's not, the function bails out rather than running with bad data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Spot the problem..a test question, pls help!
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2002, 01:40 AM