Translation from C to C++

closed account (oy721hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

I have debugged this code and it works perfectly fine in C, but I'd like it to work in C++.  If anyone can tell me how to translate it, please let me know.

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


int main()
{

    bool Prime(int A)
    {
        int k;
        if (A == 2)
        {
            return true;
        }
        for (k = 0; k < (int)sqrt(A); k++)
        {
            if ( A % (k+2) == 0)
            {
                return false;
            }
        }
                return true;
    }


    int start, end, max, j;
    start = 101;
    end   = 150;
    if (start < 2) { start = 2; }
    max = end - (start - 1);
    for(j = 0; j < max; j++)
    {
        if( Prime(j+start))
        {
        printf("\t\t\t\t    %4d\n",j+start);
        }
    }

}
 
Last edited on
Most C programs are valid C++ programs.

Local functions? That's not legal C; some GNU extension that no one should know about I think.

C has no bool type, you must use int instead.

Now you have legal C that is also C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int Prime(int A)
{
  int k;
  int mx = (int)sqrt(A);

  if (A == 2)
  {
    return 1;
  }
  for (k = 0; k < mx; k++)
  {
    if (A % (k+2) == 0)
    {
      return 0;
    }
  }
  return 1;
}

int main()
{
  int start, end, max, j;

  start = 101;
  end   = 150;
  if (start < 2)
  {
    start = 2;
  }
  max = end - (start - 1);
  for(j = 0; j < max; j++)
  {
    if( Prime(j+start))
    {
      printf("\t\t\t\t    %4d\n",j+start);
    }
  }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream> // #include <stdio.h>
//#include <stdlib.h>
#include <cmath> // #include <math.h>
// #include <stdbool.h>
#include <iomanip>

bool is_prime( int n )
{
    if( n < 2 ) return false ;
    if( n == 2 ) return true ;
    if( n%2 == 0 ) return false ;

    const int iroot = std::lround( std::sqrt(n) ) + 1 ;

    for( int k = 3 ; k < iroot ; k += 2 ) if( n%k == 0 ) return false ;

    return true ;
}


int main()
{
    /*  (this is not even C)
    bool Prime(int A)
    {
        int k;
        if (A == 2)
        {
            return true;
        }
        for (k = 0; k < (int)sqrt(A); k++)
        {
            if ( A % (k+2) == 0)
            {
                return false;
            }
        }
                return true;
    }
    */


    //int start, end, max, j;
    // start = 101;
    // end   = 150;
    // if (start < 2) { start = 2; }
    // max = end - (start - 1);
    const int start = 101 ;
    const int end = 150 ;

    // for(j = 0; j < max; j++)
    //{
      //  if( Prime(j+start))
      //  {
      //  printf("\t\t\t\t    %4d\n",j+start);
      //  }
    // }
    std::cout << "prime numbers in [" << start << ',' << end << ")\n" ;
    for( int j = start ; j < end ; ++j )
        if( is_prime(j) ) std::cout << std::setw(16) << j << '\n' ;
}

http://coliru.stacked-crooked.com/a/a8539369e9efa1c4
What problems were you having when translating it to C++?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream> //C++ equivalent of C's stdio.h
#include <cmath> //C Math Library - instead of math.h
//All the imports you need in C++ for this program

bool Prime(int A) //Define the function outside main
{
	if(A == 2)
		return true;

	for(int k = 0; k <= static_cast<int>(sqrt(static_cast<float>(A))); k++) //You can create the counter k inside the for brackets
	{
		if(A % (k+2) == 0)
			return false;
	}
	return true;
}

int main() //The main function
{
    int start, end, max;
    start = 101;
    end = 150;

    if(start < 2)
    	start = 2;

    max = end - (start-1);

    for(int j = 0; j < max; j++)
    {
    	if(Prime(j+start))
    		std::cout << "\t\t\t\t    " << j+start << std::endl; //std::cout prints the things, std::endl adds a new line.
    }

    return 0;
}
Last edited on
closed account (oy721hU5)
Thanks for the great replies. You guys are awesome. (:
kbw wrote:
C has no bool type, you must use int instead.

Huh? C has bool, which the OP used correctly.
When someone told me I can include the stdio.h which is the C language standard library into C++ code and use it. I tried and it actually works using GNU C++ Compiler...
So that means you don't need to convert the code at all.

Just as kbw said, most C programs are valid C++ programs.
Last edited on
Actually I guess C does have bool type (well the recent versions of it does):

http://stackoverflow.com/a/1608350/2089675
Topic archived. No new replies allowed.