Member Avatar for Christi_1

Okay so I have this program that generates 10 random number (1-1000), calculates their average and the amount of larger as well as smaller numbers than the average, this is what I have and for the life of me I can't figure out wheat I don't do right:

program random1;
var
    i,sum,min_pl,max_pl:integer;
    pin:array[1..10] of integer;
    mo:array[1..10] of real;
begin
        randomize;
    for i:=1 to 10 do
        pin[i]:=random(1000);
    for i:=1 to 10 do
        writeln(pin[i]);
        sum:=0;
        for i:=1 to 10 do
            begin
                sum:=sum+pin[i];
                mo[i]:=sum/10;
            end;
                writeln();
                writeln('The average is: ',mo[i]:4:2);
                writeln();
        min_pl:=0;
        max_pl:=0;
        for i:=1 to 10 do
            if (mo[i]<pin[i]) then
                min_pl:=min_pl+1
            else if (mo[i]>pin[i]) then
                max_pl:=max_pl+1;

    writeln('The amount of numbers smaller than the average is: ',min_pl);
    writeln('The amount of numbers larger than the average is: ',max_pl);
end.

So, without any error or other output, there isn't much we can do, other than analyze your code. Also, why are you using pascal? That is just such a dead language (no disrespect to Niklas Wirth intended).

And FWIW, I haven't used Pascal in about 35 years...

Member Avatar for Christi_1

@ rubberman
I just started learning programming and the Professors started us with Pascal to get the basics down.

What I can tell you, as far as outputs go, since the error is a logical one is that the amount I get for the numbers larger and smaller than the average are wrong and always ranging between results of (2-8/8-2, 4-6/6-4, 3-7/7-3).

My Pascal skills are also a bit rusty, but I used a superset of Pascal(Modula-2) during 5 years while working so this can help here. Your teacher had a good idea teaching you Pascal, but we have evolved into the 21th century. He/she should have noticed that.
So here we go trying to help you out:
Don't know why you defined line 5 as array of real, simply write this:average : real;
line 16 divides sum for the 10th time. Then you write out the last element of your real array . Try this:

for i:=1 to 10 do
    begin
        sum:=sum + pin[i];
    end;
    average = sum / 10;
writeln();
writeln('The average is: ',average:4:2);
writeln();

I will leave the rewriting of your min and max code as an exercise.
May I also point out that indentating correctly is of the utmost importance!
Believe me, I know.

Member Avatar for Christi_1

@ddanbe well we have another 3 semesters to go to do C++, Java and SQUALL, it is going to be tough but it's a night school so they do the best they can.

Thank you sooo much!!! I can't believe that messing up the average almost messed up the code!
I'm struggling a bit with the indentating for which our Professor is adamant on doing it correctly, but I'll get the hang of it, hopefully ><. Also I have to note that after trying to find the error in the code, and alot of re-writing the indentating went to hell LOL!

Chris, a good technique to trace errors like this is to simply use a pencil & paper and manually follow the instructions in your code. Set & change the values of your variables as you work through the code and your errors should become apparent. Later you will no doubt learn to use the debugger instead of pencil & paper.

There are numerous style issues (indenting, variable naming, layout, comments ...) that could be improved in your code, and 2 main errors.
The first error, as pointed out by ddanbe, is that you only need to calculate 1 average, not 10. The code he suggests will get around that problem. Then when you make that change to a single average you will need to change your comparisons and you may end up with something like this:

        for i:=1 to 10 do
            if (average<pin[i]) then
                min_pl:=min_pl+1
            else if (average>pin[i]) then
                max_pl:=max_pl+1;

This shows that your comparisons are the wrong way around, so your max_pl and min_pl values will be swapped. Better to use:

 if (pin[i] < average) then
   min_pl:=min_pl+1
 else if (pin[i] > average) then
   max_pl:=max_pl+1;
Member Avatar for Christi_1

@SalmiSoft
yeah I noticed that and changed it immediately, but thank you for the confirmation =D
Our professor is always harping about not immediately starting on writing the code in the computer but on pen and paper, but most of us rely on getting the errors when we run the program (the logical ones, not the ones the compiler shows) even though 99% of the times we have no clue were we went wrong... thus pen and paper should have been done.

I'll try and follow the advise given by both the professor and you =D.

Also I know there are errors like the ones you named, it's just that I did a quick translation to english (which isn't my native language) and some things fell from the way side. We'll be ore careful though.

Again thanx everyone for all the help

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.