Thread: Comparing Pascal, C, C++ and Java thru Code.

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    3

    Comparing Pascal, C, C++ and Java thru Code.

    Hello all, I'm just beginning with programming. Started with a bit of PASCAL and now just browsing thru C,C++ and Java. Basically, I'm comparing them through code. I have this piece of code in PASCAL:
    -------------------------------------------
    Code:
    Program A;
    var
    sum,x,i:integer;
    
    Procedure AddX(x:integer);
    begin
       sum:=sum+x;
    end;
    
    Begin
       i:=0;
       repeat
         read(x);
         AddX(x);
         i:=i+1;
       until i=10;
    write(x);
    write(sum);
    End.
    -----------------------------------
    I'm converting them to C, C++ and Java.. to understand the differences. Here are my C and C++ samples:
    -----------------------------------
    Code:
    #include <stdio.h>
    
    int main()
    {
       int i,x,sum;
       i=0;
       sum=0;
          do
             {
             scanf("%d", &x); 
             sum=sum+x;
             i=i+1;
             }
          while (i!=10);
       printf("%d\n", x);
       printf("%d\n", sum);
    
    system("PAUSE");
    return 0;
    }
    -----------------------------------------
    In C++:
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
       int i,x,sum;
       i=0;
       sum=0;
          do
          {
             cin >> x;
             sum=sum+x;
             i++;
           }
           while (i!=10);
    cout << x;
    cout << sum;
    
    system("PAUSE");
    return 0;
    }
    ---------------------------------------------------
    I'd like to find out, from the experts out there, are my codes correct? Console would return the last value input and the sum. However, I'm figuring out, do I need to make a function for AddX?

    I haven't started with the Java code yet. (Because I'm just browsing through Java now) But if you guys have an idea how I can do this, I'd appreciat e it very much.
    Last edited by jozzua; 07-04-2004 at 10:34 PM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    After a quick read-through, the only thing I can see is that your call to pause the system at the end, should have a capital S. It's a minor thing but on most compilers I don't believe it'll work.

    A few pointers, however:

    System("PAUSE"); is a bad idea. You can accomplish the same thing using the getch(); function in conio.h. On the outside, it has the same characteristics and behavior, but it doesn't depend on outside programs. System("PAUSE") tells the OS to run a program called Pause.exe - and a lot of systems won't accept this. I think only Win9x and DOS.

    sum=sum+x can be replaced with sum+=x. The same contraction can be used for subtraction, multiplication, and also division.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I don't think it would be fair to compare that way, only because in your pascal version you set up a procedure, which is like setting up a function. So in the others, a function should be made for that operation. It's a very small tidbit, but if you're comparing syntax and such, everything should be on the same level.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    3
    Many thanks to your comments. Here is my revised C/C++ code for the pascal sample. In your opinion, was I able to tranlate the code properly?
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    int i,x,sum;
    
    using namespace std;
    
    int AddX(int x)
        {
        sum+=x;
        return sum;
        }
    
    int main()
    {   
        i=0;
        sum=0;
        do
            {
                cin >> x;
                AddX(x);
                i++; 
            }
        while (i!=10);
        cout << x << '\n';
        cout << sum;
    
    return 0;
    }
    Code:
    #include <stdio.h>
    int i,x,sum;
    
    int AddX(int x)
        {
        sum=sum+x;
        return sum;
        }
    
    
    int main()
    {
        i=0;
        sum=0;
         do
            {
                scanf("%d", &x);
                AddX(x);
                i=i+1;
            }
         while (i!=10);
            printf("%d\n", x);
            printf("%d\n", sum);
    
        return 0;
    }
    Last edited by jozzua; 07-05-2004 at 01:55 AM.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    yes I do.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by sean_mackrory
    After a quick read-through, the only thing I can see is that your call to pause the system at the end, should have a capital S.

    the lower-case 's' is correct for a system call. I think in win32 you can use capital 'S'. Agreed that it shouldent be used in this situation though

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>int AddX(int x)
    I believe this should be declared as void, if you want it to be exactly like the Pascal version; and if you do that, then the return statement at the end of the function shouldn't be there.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Hunter2
    >>int AddX(int x)
    I believe this should be declared as void, if you want it to be exactly like the Pascal version; and if you do that, then the return statement at the end of the function shouldn't be there.
    Good catch. Proceedures return nothing in Pascal.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Quote Originally Posted by sean_mackrory

    System("PAUSE"); is a bad idea. You can accomplish the same thing using the getch(); function in conio.h. On the outside, it has the same characteristics and behavior, but it doesn't depend on outside programs. System("PAUSE") tells the OS to run a program called Pause.exe - and a lot of systems won't accept this. I think only Win9x and DOS.

    For a more compliant approach, however, one could put

    Code:
    if(cin.get()) return 0;
    at the end of your document, it does the same thing as getch();, and it not borland, it's ANSI.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>if(cin.get()) return 0;
    You wouldn't need the if. Actually, the if is probably a bad thing, because if cin.get() returns false, then main() won't return a value.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    1. Yes, the pascal procedure comment was correct:

    procedures in pascal are basically equivalent to void functions in C/C++

    Your pascal procedure should also send back the value of sum variable instead of value parameter x right? Since you are using a procedure

    value = in
    variable/reference= in/out, out

    Because you want to send the value back to main through the parameter.

    So pascal
    (Try to remember..long ago..)


    Code:
    Procedure AddX( x:integer; var sum :integer)
    
    begin
      
       sum:=sum+x;
    end;
    I also think it is bad style to just hava read statement. I would suggest having prompt with it like

    Enter a value sum
    read x

    You get the idea..


    2. Modify the pascal procedure to C/C++

    3. Then try it in C#as well as Java

    Overall good try
    Last edited by Mister C; 07-06-2004 at 04:13 PM.
    Mr. C: Author and Instructor

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    (*snip!*)

    **EDIT**
    Ooops, you edited There goes a lot of typing for nothing... But anyway, if you do that then the procedure call would need to be changed too, and the C/C++ samples need to be updated too. But truthfully, I don't see anything wrong with the original code; it was just used to illustrate his meaning.
    Last edited by Hunter2; 07-06-2004 at 04:27 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Hunter

    The := in the C/C++ was a typo.

    Could not remember the scope rules for pascal.

    Just curious if he actually did run and compile that pascal program.

    Mr. C: Author and Instructor

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Quote Originally Posted by Hunter2
    >>if(cin.get()) return 0;
    You wouldn't need the if. Actually, the if is probably a bad thing, because if cin.get() returns false, then main() won't return a value.

    Very good point. One must make up for that then, I suppose.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    3
    Quote Originally Posted by Hunter2
    >>int AddX(int x)
    I believe this should be declared as void, if you want it to be exactly like the Pascal version; and if you do that, then the return statement at the end of the function shouldn't be there.
    Wow. Thanks for the info. Let me redo the code again...

Popular pages Recent additions subscribe to a feed