Problem understandnig...pls help

• 01-18-2006
gtr_s15
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; }```
• 01-18-2006
DougDbug
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.)
• 01-18-2006
gtr_s15
:( 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.
• 01-18-2006
Dae
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.
• 01-19-2006
gtr_s15
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)```
• 01-19-2006
peter_hii
by wat i think for the IPOS, i think is i position...
well that is just wat i guess..

not really sure.... :D
• 01-19-2006
Stuka
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.