0

I've been trying to make a program that will ask user to input elements of an array and then use that array to make a new one whose elements would be every 2nd element of inputted array. This is what I was 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:');
for i:=1 to n do
 readln(A[i]);
writeln('Elements of array C are:');
i:=1;
while (i<=n) do
 begin
  c[i]:=a[i];
  i:=i+2;
 end;
write('C = {');
for i:=1 to n do
 begin
  if c[i]=0 then continue else
    begin
      write(c[i],' ');
    end;
 end;
write('}');
readln;
end.  

But as you can notice this is far from efficient way to make this program do the job. First, because my new array will contain blank/empty elements(zeros) which I simply ignored with continue statement and I dont want to do that if possible. Second,I have problem when inputting an even number for array length.Last element of new array in output window is very small,negative number and he shouldn't be there at all.I know this has to do something with my counter "i" crossing into "undefined" indexes of array. I also tried replacing while loop with some variations of:

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

Which is more elegant way but I still get , besides desired result , those large numbers , again because of crossing limits of an array.I suspect this has to be done with proper steps of how new array is made and moving those elements next to each other with no blank elements. So, if anyone can give me some solutions of how to get these loop steps and limits into right order and make efficient,shortest algorithm, and preferably without using while loop if possible ,and definitely without ignoring those blank elements of new array.

4
  • 1
    If the new array should contain every other element of the original array it follows immediately that the new array will have half the size of the old array (we'll assume an even number of elements in the original array for now). So what you need to do is allocate the new array to be n div 2 elements long and then use two index variables, e.g. i for reading, and j for writing, increasing i with two and j with one for each loop step. Commented Jan 31, 2014 at 17:04
  • Thank you sir for a hint.It was obvious but I needed that little "push" to think. I made it to work with odd numbers too with x:=Round(n/2+0.1); where x is length of new array. Commented Jan 31, 2014 at 18:20
  • Dynamic arrays are 0=based, so you're starting with an empty element at array[0] in both arrays. You can also use a step clause in your for loop, as in for i := 0 to n - 1 step 2 do, which will loop with 0, 2, 4, and so forth. I personally would switch to a while i < n do instead, and increment i by the desired amount inside the loop. It makes the code more clear, IMO, and prevents overrunning the end of the array. You should also enable range checking ({$R+})and overflow checking ({$Q+}), at least during development, to catch any possible issues. Commented Jan 31, 2014 at 23:15
  • Yes , while loops are more clear and I used them in this case.To be honest I didnt know that Pascal can do custom steps within forloop, but thank you very much for showing me step.I understand that dyn.arrays start with index 0 but Im wondering about what if I said for loop in the end to write me elements of a new array from -1 to x ?Would array start with : 0 0 and then integer values of inputted array from beginning of program? Would that mean that array[-1],array[-2] are also empty elements although they are not indexed(I think) or assigned any value? Commented Jan 31, 2014 at 23:43

1 Answer 1

1

Declaring variables by one character A, C: array of integer is a bad practice. The name of variable will tell you about it's type and meaning. (highly recommended)

And read about Hungarian notation, then you'll understand why TArr and arrA instead of Arr and A. Not necessary, but recommended. (also recommended)

As for the second arrC, you can use operator div to make it twice smaller than the first array arrA. You can also crete a new variable that will be n div 2, in order not to change n div 2 in the whole code (good practice):

nDivided := n div 2;
SetLength(arrA,n);
SetLength(arrC, nDivided); 

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

Here are both cases (for even and odd N). No "blank elements" and no "very small, negative number in the end of new array(-32768)".

  N is 6                               N is 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 is code (a little changed). Also not very good (efficent) but it does exactly what you need.

program WorkingWithArrays;
uses crt;
type
  TArr = Array of Integer;
var
  i, n: integer;
  arrA, arrC: TArr;


begin

  writeln('Enter the length of array:');
  readln(n);
  SetLength(arrA,n);
  SetLength(arrC, n div 2);

  writeln('Elements of array A:');    
  for i:=1 to (n) do begin
    arrA[i]:=i;
    writeln('@> arrA[',i,']=', arrA[i]);
  end;

  writeln('Elements of array C are:');
  i:=1;
  while (i <= n div 2) do
    begin
    arrC[i]:=arrA[i+i];
    i:=i+1;
  end;

  for i:=0 to (n div 2) do begin
    if arrC[i]=0 then continue else begin
      writeln('@> arrC[',i,']=', arrC[i]);
    end;
  end;

  readln;
end.

// compiled here: http://www.compileonline.com/compile_pascal_online.php
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.