Making new array whose elements are every second element of another array.(Pascal) -


i've been trying make program ask user input elements of array , use array make new 1 elements every 2nd element of inputted array. writing:

program keanu; uses crt; type arr=array of integer;  var n,i:integer; a,c:arr; begin writeln('--enter desired length of array--'); readln(n); setlength(a,n); setlength(c,n); writeln('elements of array a:'); i:=1 n  readln(a[i]); writeln('elements of array c are:'); i:=1; while (i<=n)  begin   c[i]:=a[i];   i:=i+2;  end; write('c = {'); i:=1 n  begin   if c[i]=0 continue else     begin       write(c[i],' ');     end;  end; write('}'); readln; end.   

but can notice far efficient way make program job. first, because new array contain blank/empty elements(zeros) ignored continue statement , dont want if possible. second,i have problem when inputting number array length.last element of new array in output window small,negative number , shouldn't there @ all.i know has counter "i" crossing "undefined" indexes of array. tried replacing while loop variations of:

for i:=0 n c[i]:=a[2*i-1] ; 

which more elegant way still , besides desired result , large numbers , again because of crossing limits of array.i suspect has done proper steps of how new array made , moving elements next each other no blank elements. so, if can give me solutions of how these loop steps , limits right order , make efficient,shortest algorithm, , preferably without using while loop if possible ,and without ignoring blank elements of new array.

declaring variables 1 character a, c: array of integer bad practice. name of variable tell it's type , meaning. (highly recommended)

and read hungarian notation, you'll understand why tarr , arra instead of arr , a. not necessary, recommended. (also recommended)

as second arrc, can use operator div make twice smaller first array arra. can crete new variable n div 2, in order not change n div 2 in whole code (good practice):

ndivided := n div 2; setlength(arra,n); setlength(arrc, ndivided);  

this make program quite bit efficient: you'll save n - (n div 2) * sizeof(integer) bytes.

here both cases (for , odd n). no "blank elements" , no "very small, negative number in end of new array(-32768)".

  n 6                               n 5  elements of array a:                 elements of array a:   arra[1]=1                           arra[1]=1   arra[2]=2                           arra[2]=2          arra[3]=3                           arra[3]=3   arra[4]=4                           arra[4]=4          arra[5]=5                           arra[5]=5   arra[6]=6                          elements of array c are:             elements of array c are:   arrc[1]=2                           arrc[1]=2     arrc[2]=4                           arrc[2]=4      arrc[3]=6 

anyway, here code (a little changed). not (efficent) need.

program workingwitharrays; uses crt; type   tarr = array of integer; var   i, n: integer;   arra, arrc: tarr;   begin    writeln('enter length of array:');   readln(n);   setlength(arra,n);   setlength(arrc, n div 2);    writeln('elements of array a:');       i:=1 (n) begin     arra[i]:=i;     writeln('@> arra[',i,']=', arra[i]);   end;    writeln('elements of array c are:');   i:=1;   while (i <= n div 2)     begin     arrc[i]:=arra[i+i];     i:=i+1;   end;    i:=0 (n div 2) begin     if arrc[i]=0 continue else begin       writeln('@> arrc[',i,']=', arrc[i]);     end;   end;    readln; end.  // compiled here: http://www.compileonline.com/compile_pascal_online.php 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -