## Help with printing out the total, average and the difference

New to FreeBASIC? Post your questions here.
dosierb
Posts: 3
Joined: Nov 28, 2010 23:08

### Help with printing out the total, average and the difference

My code is below. The only thing that is not working is the print for the difference. If you run this program, the difference is showing a long string of numbers not the difference between the user's grade and the average grade.

Barb

Code: Select all

`'Barb Dosier'CO116 Chpt 6Q1'November 20, 2010'Program will have the user enter ten numbers and display them in reverse orderDim sum As IntegerDim average As IntegerDim size As IntegerDim count As IntegerDim difference As IntegerPrint "Enter ten numbers"size = 2Dim scores(size) As IntegerFor count = 1 to SIZE    Input "Enter a number"; scores(count)    sum = sum + scores(count)Next average = sum / SIZEdifference = scores(count) - averagePrintPrint "Numbers", "Average", "Difference from average"Print "*****************************************************"For count = 2 to 1 step -1    print scores (count), average, differenceNextSleep`
square1
Posts: 97
Joined: Nov 12, 2007 2:27
This is one way that works:

Code: Select all

`Dim sum As IntegerDim average As doubleDim size As IntegerDim count As IntegerPrint "Enter ten numbers"size = 2Dim scores(size) As IntegerDim difference(size) As doubleFor count = 1 to SIZE   Input "Enter a number"; scores(count)   sum = sum + scores(count)Nextaverage = sum / SIZEPrintPrint "Numbers", "Average", "Difference from average"Print "*****************************************************"For count = 2 to 1 step -1   difference(count) = scores(count) - average   Print scores (count), average, difference (count)Nextsleep`

The problem is that the difference of each score is going to be different, if that makes any sense :p

Also, average and difference should be doubles rather than integers
dosierb
Posts: 3
Joined: Nov 28, 2010 23:08

### Re: Help with printing out the total, average and the differ

Thank you. We briefly touched on doubling in class. Due to an instructor being sick and with a holiday we have not had a class in three weeks and so we have not had any lab time. Obviously this is basic programming.

I have one more that I am having problems with, if you could take a look at that code. When it prints out the classroom and average is not correct. I copied the code below.

Code: Select all

`'Barb Dosier'CO 116 Chpt 6Q3'Displaying class test score resultsDim classroomCount As IntegerDim studentCount As IntegerDim count As IntegerDim room As IntegerDim score As IntegerInput "How many classrooms are there"; classroomCountInput "How many students are there"; studentCountDim numberOfStudents(classroomCount) As IntegerDim pointsPerClassroom(classroomCount) As IntegerFor count = 1 to studentCount    Print "Student"; count    Do        Input "Which room is the student in"; room    Loop While room < 1 and room <= classroomCount        Input "What is the student's score"; score    numberOfStudents(room) += 1    pointsPerClassroom(room) += scoreNextaverage = sum / SIZEaverage = pointsPerClassroom(room) / numberOfStudents(room)for count = 1 to classroomCountPrintPrint "Student class scores are as follows:"Print "Classroom", "# of Students", "Total points", "Average points"Print "---------------------------------------------------"    Print room, numberOfStudents(count), pointsPerClassroom(count), averagenextSleepBarb'Barb Dosier'CO116 Chpt 6Q1'November 20, 2010'Program will have the user enter ten numbers and display them in reverse orderDim sum As IntegerDim average As IntegerDim size As IntegerDim count As IntegerDim difference As IntegerPrint "Enter ten numbers"size = 2Dim scores(size) As IntegerFor count = 1 to SIZE    Input "Enter a number"; scores(count)    sum = sum + scores(count)Next average = sum / SIZEdifference = scores(count) - averagePrintPrint "Numbers", "Average", "Difference from average"Print "*****************************************************"For count = 2 to 1 step -1    print scores (count), average, differenceNextSleep`
square1
Posts: 97
Joined: Nov 12, 2007 2:27
Similar problem, you need a different average for each classroom.

Code: Select all

`'Barb Dosier'CO 116 Chpt 6Q3'Displaying class test score resultsDim classroomCount As IntegerDim studentCount As IntegerDim count As IntegerDim room As IntegerDim score As IntegerInput "How many classrooms are there"; classroomCountInput "How many students are there"; studentCountDim numberOfStudents(classroomCount) As IntegerDim pointsPerClassroom(classroomCount) As Integer'added line:Dim average(classroomCount) As DoubleFor count = 1 to studentCount    Print "Student"; count        Do        Input "Which room is the student in"; room    Loop While room < 1 and room <= classroomCount       Input "What is the student's score"; score    numberOfStudents(room) += 1    pointsPerClassroom(room) += scoreNext'removed: average = sum / SIZE'removed: average = pointsPerClassroom(room) / numberOfStudents(room)for count = 1 to classroomCount   'added line:   average(count)=pointsPerClassroom(count)/numberOfStudents(count)   Print   Print "Student class scores are as follows:"   Print "Classroom", "# of Students", "Total points", "Average points"   Print "---------------------------------------------------"   'changed Line: Print room, numberOfStudents(count), pointsPerClassroom(count), average into:    Print count, numberOfStudents(count), pointsPerClassroom(count), average(count)nextSleep`
fxm
Posts: 9814
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE
dosierb,

If you compile your original program with the option "-exx", you could see that there is an "Aborting due to runtime error 6 (out of bounds array access) at line 22" :

(22) difference = scores(count) - average

because at the output of the "for" iteration, count = size + 1, when ubound of scores() is equal to size only (Dim scores(size) As Integer)!

Anyway, it is strongly recommended not to use the value of exit of an iterator (as count) in a program.
dosierb
Posts: 3
Joined: Nov 28, 2010 23:08

### Re: Help with printing out the total, average and the differ

Thank you for the tips and explanations. I went through the code last night for the other program to see the difference in "my" program" and the one that actually works. It really helps. I will now try this one.

One more question:
When do you know when to use double instead of single or integer?

Barb

Code: Select all

`'Barb Dosier'CO116 Chpt 6Q1'November 20, 2010'Program will have the user enter ten numbers and display them in reverse orderDim sum As IntegerDim average As IntegerDim size As IntegerDim count As IntegerDim difference As IntegerPrint "Enter ten numbers"size = 2Dim scores(size) As IntegerFor count = 1 to SIZE    Input "Enter a number"; scores(count)    sum = sum + scores(count)Next average = sum / SIZEdifference = scores(count) - averagePrintPrint "Numbers", "Average", "Difference from average"Print "*****************************************************"For count = 2 to 1 step -1    print scores (count), average, differenceNextSleep`
[/quote]
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

### Re: Help with printing out the total, average and the differ

dosierb wrote:One more question:
When do you know when to use double instead of single or integer?

Barb

If the numbers you are dealing with have fractional components, say 5.7 or 3.14 rather than 6 or 2 then you need to use a floating point type (single or double) to store that information. The significant advantage singles have over doubles is that they take half as much memory to store (4 bytes as opposed to 8), but they have a smaller range of values and a smaller number of digits of accuracy. The smaller memory size can be useful if you were writing a program that was going to store millions of values in memory at the same time for example, otherwise no real reason not to do it all with doubles.