0
votes

I'm trying to add two multi-dimensional arrays of different sizes.

I hope you understand my code the variables are in portuguese.

Program NOTA_ALUNO; 
Var   
  A,B,C: Array[1..100,1..100] Of Integer;
  contadorHorizontal,contadorVertical: Integer; 
  valoresVertical_A,valoresHorizontal_A,mm,nn: Integer; 
  valoresVertical_B,valoresHorizontal_B: Integer; 
Begin   
  Writeln('digite as dimensões das matrizes:'); 
  Readln(valoresHorizontal_A,valoresVertical_A);  
  Writeln('digite os elementos da matriz A:');   
  For contadorHorizontal := 1 To valoresHorizontal_A Do
  Begin
    For contadorVertical := 1 To valoresVertical_A Do
    Begin
      Read(A[contadorHorizontal,contadorVertical]);
    End;
    Readln;
  End;   
  Writeln('digite as dimensões das matrizes:'); 
  Readln(valoresHorizontal_B,valoresVertical_B);  
  Writeln('digite os elementos da matriz B:');   
  For contadorHorizontal := 1 To valoresHorizontal_B Do
  Begin
    For contadorVertical := 1 To valoresVertical_B Do
    Begin
      Read(B[contadorHorizontal,contadorVertical]);
    End;
    Readln;
  End;  

And here is what I did. If the values of the x axis of array A is bigger than of array B, I'd take the number of array B and add one and fill the missing spaces with zeroes. But that doesn't work:

  If (valoresHorizontal_A > valoresHorizontal_B) Then
    nn := valoresHorizontal_B+1;   
  For contadorHorizontal := nn To valoresHorizontal_A Do
  Begin
    B[contadorHorizontal,contadorVertical] := 0;
  End;  

  {calcular a soma de A + B} 
  For contadorHorizontal := 1 To valoresHorizontal_A Do
  Begin
    For contadorVertical := 1 To valoresVertical_A Do
    Begin
      C[contadorHorizontal,contadorVertical] := 
        A[contadorHorizontal,contadorVertical] +
        B[contadorHorizontal,contadorVertical];
    End;
  End;  
  {imprimir o resultado}   
  Writeln('O resultado da soma de A+B:');   
  For contadorHorizontal := 1 To valoresHorizontal_A Do
  Begin
    For contadorVertical := 1 To valoresVertical_A Do
    Begin
      Write(C[contadorHorizontal,contadorVertical],'  '); 
      {deixar espaço entre números}
    End;
    Writeln; { quebra linha }
  End;   
  Readln; 
End.
1
But that doesn't work. What do you expect to happen and what happens? IOW how does it not work? You must be clear and specific in your problem description. - Tom Brunberg
BTW, please think about the following: 1) You consider only (I abbreviate your variables) horiz_A > horiz_B. 2) What if horiz_B > horiz_A? 3) What if vert_A > vert_B or if vert_B > vert_A? 4) What if any dimension is bigger than 100? Finally, when you write results, you write the values in vertical order horizontally. Is that your intention? - Tom Brunberg

1 Answers

0
votes

What could solve your issue is filling your arrays with zeroes at the beginning, before you enter your values. Like this:

  For contadorHorizontal := 1 To 100 Do
  Begin
    For contadorVertical := 1 To 100 Do
    Begin
      A[contadorHorizontal,contadorVertical] := 0;
      B[contadorHorizontal,contadorVertical] := 0;
    End;
  End; 

And deleting this section:

  If (valoresHorizontal_A > valoresHorizontal_B) Then
    nn := valoresHorizontal_B+1;   
  For contadorHorizontal := nn To valoresHorizontal_A Do
  Begin
    B[contadorHorizontal,contadorVertical] := 0;
  End;