how I transform this algorithm from pascal to c ++

0

Hello everyone I am a first semester student of lic. in computer science and in my university they sent us to learn the languages in a self-taught way, until now we have seen pascal but they ordered us to learn c / c ++, we are currently working with modules and fixes, my problem is that I have had great problems implementing this last learned ac / c ++ then I will place a code with which I have had these difficulties:

    program sumamatrices;
    const
       x =4;
    type
      tiparre= array [1..x,1..x] of integer;
    var
    a,b,c : tiparre;
    Fi,Co: integer;
    procedure limpiar (var a,b,c : tiparre);
    begin
      for Fi := 1 to x do
      begin
        for Co:= 1 to x do
            begin
                a[Fi,co]:= 0;
                b[Fi,co]:= 0;
                c[Fi,co]:= 0;
            end;
        end;
      end;
      procedure llenar (var a,b:tiparre);
      begin
        for Fi:= 1 to x do
          begin
             for Co:= 1 to x do
             begin
               writeln ('ingrese el valor de a en la posicion ',Fi,', ',Co);
               readln(a[Fi,Co]);
               writeln ('ingrese el valor de b en la posicion ',Fi,', ',Co);
               readln(b[Fi,Co]);
             end;
          end;
      end;
      procedure sumar (var a,b,c: tiparre);
      begin
        for Fi:= 1 to x do
        begin
          for Co:= 1 to x do
           begin
             c[Fi,Co]:= a[Fi,Co] + b[Fi,Co];
             writeln ('el valor de c en la posicion',Fi,',',Co,'es ', c[Fi,Co]);
           end;
        end;
      end;
      procedure determinar (a:tiparre);
      var
        cont: integer;
      begin
        cont:=0;
        for Co := 1 to x do
        begin
          if a[Fi,Co] = 1 then
            begin
              cont:= cont +1
            end;
        end;
        if (cont = x) then
          begin
            writeln('la diagonal de a es 1');
          end;
      end;
      begin
        limpiar (a,b,c);
        llenar (a,b);
        sumar (a,b,c);
        determinar(a);
      end.

If any of you have the knowledge and the disposition to do me the favor of translating this code I would be very grateful, it would really help me a lot to understand this language

    
asked by Victor Campos 20.07.2016 в 02:36
source

1 answer

2

Hello first, before all the migration, a program made in Pascal to C ++ is not so complicated since the two languages share many characteristics although not syntactically if in the way of posing and solving problems.

I recommend you see this link: link to know the equivalences between a language and another unfortunately the document is in English but seeing the examples you can guide.

Regarding your question, I made a small migration which tried to keep it the same.

#include <iostream>

using namespace std;

const int LEN=4;
typedef int Array2DInteger[LEN][LEN];


void _clear(Array2DInteger a,Array2DInteger b,Array2DInteger c){

    for(int fi=0;fi<LEN;fi++){
        for(int co=0; co<LEN;co++){
            a[fi][co]=0;
            b[fi][co]=0;
            c[fi][co]=0;
        }
    }
}

void _fill(Array2DInteger a,Array2DInteger b){

    for(int fi=0;fi<LEN;fi++){
        for(int co=0;co<LEN;co++){
            cout<<"Ingrese el valor de a en la posicion "<<fi<<" "<<co<<endl;
            cin>>a[fi][co];
            cout<<"Ingrese el valor de b en la posicion "<<fi<<" "<<co<<endl;
            cin>>b[fi][co];
        }
    }
}

void _add(Array2DInteger a,Array2DInteger b, Array2DInteger c){
    for(int fi=0;fi<LEN;fi++){
        for(int co=0;co<LEN;co++){
            c[fi][co]=a[fi][co]+b[fi][co];
            cout<<"El valor de c en la posicion "<<fi<<" "<<co<<" es "<<c[fi][co]<<endl;
        }
    }
}

void _determine(Array2DInteger a){
 int counter=0;
 int fi=0;

    for(int co=0;co<LEN;co++){
        if(a[fi][co]==1)  counter++;
    }
    if(counter==LEN-1){
        cout<<"La diagonal de a es 1"<<endl;
    }
}

int main()
{
    Array2DInteger a,b,c;

    _clear(a,b,c);
    _fill(a,b);
    _add(a,b,c);
    _determine(a);

    return 0;
}
    
answered by 20.07.2016 в 23:46