Converting A Basic Program to A C++ Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bsmath
    New Member
    • Mar 2010
    • 7

    Converting A Basic Program to A C++ Program

    I wrote the following program in Basic. It works in Basic, but I cannot get it
    converted to C++. Any help in that direction will be appreciated. The values for
    a, S, and R can be changed.

    a=3
    for S=1 to 5 step 2
    R=2
    N=((2^R)*S)+1
    if int(((a^S)-1)/((2^R)*S+1))=(( a^S)-1)/N then print "yes"; "S="; S; "R="; R; "a="; a else print "no"
    next S
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    If you know the algirithm, reimplement it in C. If A^B is raisong to power, use pow(a,b) function for it.

    Comment

    • randysimes
      New Member
      • Oct 2009
      • 54

      #3
      Code:
      int a(3), S, R;
      for (S=1; S <=5; S+=2)
      {
      R=2
      N=((2^R)*S)+1
      if (int(((a^S)-1)/((2^R)*S+1))==((a^S)-1)/N)
       std::cout<< "yes\n"
                    << "S= " << S
                    << "\nR= " << R
                     << "\na= " << a;
       else 
         std::cout << "no";
      }

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        You could write something like the following:
        Code:
        #include<iostream>
        #include<cmath>
        using namespace std;
        
        int main()
        {
            double a=3.0,R=2;
            for(int S=1;S<11;S+=2)
            {
                double N=pow(2,R)+1;
                double X=pow(a,S)-1;
                double Y=pow(2,R)*S+1;
                if(X/Y==X/N)cout<<"yes";
                else cout<<"no";
              cout<<"\tS: "<<S<<"\tR: "<<R<<"\ta: "<<a<<endl;      
              }
        char q;
        cout<<"press any key to continue...";
        cin.get(q);
        return 0;
        }
        //output
        /*yes S: 1 R: 2 a: 3
        no S: 3 R: 2 a: 3
        no S: 5 R: 2 a: 3
        no S: 7 R: 2 a: 3
        no S: 9 R: 2 a: 3
        press any key to continue...*/

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Note: The above code emulates the BASIC sphagetti style which would not attract the lowest level of approbation from a C++ professional. In this case two functions should have been used; one a bool type to test the expression and one called to print the yes/no and R,S and a values

          Comment

          • bsmath
            New Member
            • Mar 2010
            • 7

            #6
            Thank you for your answer.

            Comment

            Working...