6 Replies - 5968 Views - Last Post: 23 September 2010 - 08:28 PM Rate Topic: -----

#1 kozo_gurl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-March 09

Incorporating an ADA code into C++

Posted 21 September 2010 - 09:08 PM

I was wondering how you would incorporate this ADA code into C++ to find for example the GCD of two numbers:


procedure gcd(u,v:in integer; x: out integer) is     
     y, t, z; integer; 
begin 
     z:= u; 
     y:=v; 
     loop      
      exit when y=0;       
      t:=y;      
      y:=z mod y      
      z:=t; 
     end loop; 
      x:=z; 
end gcd; 


Is This A Good Question/Topic? 0
  • +

Replies To: Incorporating an ADA code into C++

#2 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Incorporating an ADA code into C++

Posted 22 September 2010 - 04:13 AM

Because ADA is high level, there is no way to "incorporate" it into your program. However, you can translate it if you understand ADA and C++ enough.

Let me help get you started (Note that I don't understand ADA very much, I've never used it):

begin Transalte this to int main()

Translate the procedurep to a function (I believe that returns int

Translate the [il]loop
to a while loop. E.g. [il]while (y != 0)


Good luck figuring out the rest. If you run into any more bumps, I'll be happy to help

P.S. - Remember to incorporate curly brackets, I do not show them in the examples of translations.

This post has been edited by Splatocaster: 22 September 2010 - 04:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 kozo_gurl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-March 09

Re: Incorporating an ADA code into C++

Posted 23 September 2010 - 07:52 PM

I went back and attempted to write the program in C++ and it ran for a few seconds before shut down automatically. On the console it displayed "gcd = 2 and gcd = 0". It got the right gcd but it displayed "gcd = 0" as well and I don't know how to fix it.


#include <iostream>
using namespace std;

int main()
{
	int t;
	int u = 8;
	int v = 18;
	int z = u;
    int y = v; 
    
 while (y !=0)
  {
  	t = y;     
    y = z % y;      
    z = t;
      cout << " gcd is:  " << z % y << endl;
  }
  
  return 0;

}



Was This Post Helpful? 0
  • +
  • -

#4 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Incorporating an ADA code into C++

Posted 23 September 2010 - 08:14 PM

The program is actually doing the right thing - it's doing all the code, and than exiting. You never specified that you wanted to be able to see the output! >.<

Try adding one of the following to the end of the program, right before return 0; This will instruct the program to wait for the user before closing.
The first is the "best", the last is the "worst".

cin.ignore (numeric_limits<streamsize>::max(), '\n' );
cin.get()


while (true)
   ;


system("PAUSE");



If your not satisfied with any of those, check out http://www.dreaminco...on-window-open/

This post has been edited by Splatocaster: 23 September 2010 - 08:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Incorporating an ADA code into C++

Posted 23 September 2010 - 08:17 PM

Well, first thing does your while() loop look like the Ada one?

No, because the Ada one does not do any IO. So why are you displaying the GCD inside the loop?

Second, the Ada procedure returns an integer value when it's loop exits. It assigns z to x. That's when you should display the GCD.

Like this:

...
    /*
    procedure gcd(u,v:in integer; x: out integer) is     
    y, t, z; integer; 
    begin 
    z:= u; 
    y:=v; 
    loop      
    exit when y=0;       
    t:=y;      
    y:=z mod y      
    z:=t; 
    end loop; 
    x:=z; 
    end gcd; 
    */
    int u = 8;
    int v = 18;

    int y = v; 
    int t;
    int z = u;

    while (y != 0)
    {
        t = y;     
        y = z % y;      
        z = t;
    }

    cout << " gcd is:  " << z << endl;
...


This post has been edited by n8wxs: 23 September 2010 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

#6 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Incorporating an ADA code into C++

Posted 23 September 2010 - 08:22 PM

It should print 2 and 0

It prints the zero because the last operation is 8 % 2

8%2 = 4 Remainder 0, therefore you are returned 0.

<Edit> Too late

This post has been edited by eker676: 23 September 2010 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Incorporating an ADA code into C++

Posted 23 September 2010 - 08:28 PM

Here's a C++ 'Procedure' version:

/*
 * procedure gcd(u,v:in integer; x: out integer) is     
 * y, t, z; integer; 
 * begin 
 * z:= u; 
 * y:=v; 
 * loop      
 * exit when y=0;       
 * t:=y;      
 * y:=z mod y      
 * z:=t; 
 * end loop; 
 * x:=z; 
 * end gcd; 
*/
int GCD(int u, int v) {
    int y, t, z;

    z = u;
    y = v;

    while (y != 0) {
        t = y;     
        y = z % y;      
        z = t;
    }

    return z; // x:=z; 
}



It can be called like this:

using namespace std;

int GCD(int, int);

int _tmain(int argc, _TCHAR* argv[])
{
    cout << " gcd is:  " << GCD(8, 18) << endl;

    cin.clear();
    cin.sync();
    cout << endl << "Hit ENTER to continue..." << endl;
    cin.get();

    return 0;
}



Output:

Quote

gcd is: 2

Hit ENTER to continue...

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1