not in array

This program is supposed to find X perfect numbers. So after it finds one it enter the value into the array sums. Then the next time throught i want it to go past that number and find the next number. So basically i just need the syntax or code needed to check if a number is in an array. I included the code if you need to see what i'm talking about. The part i need help wiht is commented out. Thanks


{B+}
program prob7_23;

{Nate Hecht}
{3-8-04}
{Finds X perfect numbers}

uses fdelay, crt;

function prime_check(x:integer):boolean;

var
i,y,n:integer;

begin
y:=0;n:=0;
if x=1 then
prime_check:=true
else
begin
for i:=2 to x do
begin
if (x mod i = 0) then
Y:=Y+1
else
N:=N+1;
end;
if Y=1 then
prime_check:=true
else
prime_check:=false;
end;
end; {prime_check}

var
maxNUM,i,sum,power,index:integer;
sums:array [1..10] of integer;
again:char;

begin
repeat
clrscr;
write('Perfect numbers to find : ');
readln(maxNUM);writeln;

fillchar (sums, sizeof(sums), 0);

index:=1;
for i:=1 to maxNUM do
begin
sum:=0;
power:=1;
repeat
sum:=sum+power;
power:=power*2;
until (prime_check(sum)) and (sum <> 1){ and (not in sums)};
writeln(sum * (power div 2));
sums [index]:=(sum * (power div 2));
index:=index+1;
end;

writeln;write('Again (Y/N) : ');
repeat
again:=readkey;
again:=upcase(again);
until again in ['Y','N'];
until again = 'N';
end.

Comments

  • : This program is supposed to find X perfect numbers. So after it finds one it enter the value into the array sums. Then the next time throught i want it to go past that number and find the next number. So basically i just need the syntax or code needed to check if a number is in an array. I included the code if you need to see what i'm talking about. The part i need help wiht is commented out. Thanks
    :
    :
    : {B+}
    : program prob7_23;
    :
    : {Nate Hecht}
    : {3-8-04}
    : {Finds X perfect numbers}
    :
    : uses fdelay, crt;
    :
    : function prime_check(x:integer):boolean;
    :
    : var
    : i,y,n:integer;
    :
    : begin
    : y:=0;n:=0;
    : if x=1 then
    : prime_check:=true
    : else
    : begin
    : for i:=2 to x do
    : begin
    : if (x mod i = 0) then
    : Y:=Y+1
    : else
    : N:=N+1;
    : end;
    : if Y=1 then
    : prime_check:=true
    : else
    : prime_check:=false;
    : end;
    : end; {prime_check}
    :
    : var
    : maxNUM,i,sum,power,index:integer;
    : sums:array [1..10] of integer;
    : again:char;
    :
    : begin
    : repeat
    : clrscr;
    : write('Perfect numbers to find : ');
    : readln(maxNUM);writeln;
    :
    : fillchar (sums, sizeof(sums), 0);
    :
    : index:=1;
    : for i:=1 to maxNUM do
    : begin
    : sum:=0;
    : power:=1;
    : repeat
    : sum:=sum+power;
    : power:=power*2;
    : until (prime_check(sum)) and (sum <> 1){ and (not in sums)};
    : writeln(sum * (power div 2));
    : sums [index]:=(sum * (power div 2));
    : index:=index+1;
    : end;
    :
    : writeln;write('Again (Y/N) : ');
    : repeat
    : again:=readkey;
    : again:=upcase(again);
    : until again in ['Y','N'];
    : until again = 'N';
    : end.
    :
    The in operator only applies to sets. If you want to check if a value is in an array, you need to check each element seperately in that array. This requires another loop.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion