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.



LinkBack URL
About LinkBacks



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.