Add the elements of an array together

FlamingClaw 0 Tallied Votes 800 Views Share

The Rule:
s:=0;
Loop i:=1 To N
s:=s+T;
End of Loop;
Out:s;
By FlamingClaw

{
Question
Add together the elements of an array
s:=0
Loop i:=1...N
s:=s+T[i]
End of Loop
Out: s
}
Program Solution01;
Const n=20;
Var i,s:Integer;
        T:Array[1..n]Of Integer;
Begin {main}
   WriteLn('First run: 1..',n);
   For i:=1 To n Do Begin
      T[i]:=i;
      Write(T[i]:2,' ');
   End;
   WriteLn;
   Write('Add this numbers together:');
   s:=0;
   For i:=1 To n Do s:=s+T[i];
   {write the results to the screen}
   WriteLn(s);
   Write('Press enter to quit..');
   ReadLn;
End.{end of main}

{
-= Note By FlamingClaw =-

All programs created by me are written and tested
in Dev Pascal and/or Turbo Pascal 7.0
Of course working!
-= Created By FlamingClaw =-
-=2009.04.05=-
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.