pascal quation..

ploutarchos

Distinguished
Sep 15, 2008
10
0
18,560
hi i want to make a program... to find the smallest and the largest of 'N' Numbers ( give by user ) and the avarage of all...

i make a code but i have problems, how i cant test 'N' Numbers with 1 variable ?????? This is the code.... please help me..


program new1 (input,output);

var num , i , n:integer;
sum , avarage:real;

Begin

sum:=0;

writeln('How mutch numbers you want ? ');
readln(n);

while i < n do
Begin


writeln('Give you ' , n , ' number')
readln('num);
n:=n-1;
sum:=sum+num;
i:=i+1;
end;

avarage:=sum/2;
writeln('The avatage of your numbers is ', avarage:6:2);

readln;
end.
 

pat mcgroin

Distinguished
Nov 21, 2007
149
0
18,660
I havent done any programming since basic and Im not familier with this code but..
Im confused about this line
var num , i , n:integer;
and

i:=i+1;
end;

avarage:=sum/2;

You seem to have called for "i" as a variable being a integer but in
writeln('How mutch numbers you want ? ');
readln(n);
you rename the integer to n
It seems to me that you are divideing by 0

Like I said Im not a programmer


 

ploutarchos

Distinguished
Sep 15, 2008
10
0
18,560
the number 'N' they will be given by user... example: if i give N= 5;
my program it is readln 5 num. i:=i+1 its collaborates with While to give you the 5 numbers.... sum is the total of all your numbers that you have and we find the avarage with sum / 2;

my problem is how i can check and find the smallest and the largerst number with only ony variable??? i mean my numbers it's num..
 

Ijack

Distinguished
Try this:

Code:
program new1 (input,output);

var num,  , n, minnum, maxnum:integer;
sum, average:real;

Begin
   sum:=0;
   minnum := 32767;
   maxnum := -32766;

   writeln('How many numbers do you want to enter? ');
   readln(n);
   while i < n do
      Begin
         writeln('Enter ', n, ' numbers')
         readln(num);
         n:=n-1;
         sum:=sum+num;
         if num < minnum then
         Begin
            minnum := num;
         End;
         if num > maxnum then
         Begin
            maxnum := num;
         End;
         i:=i+1;
      End;

      average:=sum/n; 

      writeln('The average of your numbers is ', average:6:2);
      writeln('The smallest number is ', minnum);
      writeln('The largest number is ', maxnum);

      readln;
End.
I haven't actually compiled this, as I don't have a Pascal compiler to hand, but it should work.
 

Ijack

Distinguished
Oops - not quite right. Add line 10 as:
Code:
 i := 0;
(the compiler may assume this, but it's good practice to always specifically initialize variables) and remove line 17;
 

ploutarchos

Distinguished
Sep 15, 2008
10
0
18,560
omg :s i think iam stupid.... i haven't see easyer than that!!!!!!!! i do it that before..... but i set min:= -9999 and max:=9999 and its not make sence because give it to me the same numbers :S ..The truth is iam stupid.... :S thank you very match for your help my friend...
 

Ijack

Distinguished
Not stupid - we all make silly mistakes. That's what learning is all about.

(Truth is, first time I wrote the program I made the same mistake. Luckily I noticed it before posting, which spared my blushes somewhat!)