Help with printing out the total, average and the difference

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

Help with printing out the total, average and the difference

Post by dosierb »

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 order

Dim sum As Integer
Dim average As Integer
Dim size As Integer
Dim count As Integer
Dim difference As Integer

Print "Enter ten numbers"
size = 2
Dim scores(size) As Integer

For count = 1 to SIZE
    Input "Enter a number"; scores(count)
    sum = sum + scores(count)
Next 

average = sum / SIZE
difference = scores(count) - average

Print
Print "Numbers", "Average", "Difference from average"
Print "*****************************************************"
For count = 2 to 1 step -1
    print scores (count), average, difference
Next

Sleep
square1
Posts: 97
Joined: Nov 12, 2007 2:27

Post by square1 »

This is one way that works:

Code: Select all

Dim sum As Integer
Dim average As double
Dim size As Integer
Dim count As Integer


Print "Enter ten numbers"
size = 2

Dim scores(size) As Integer
Dim difference(size) As double

For count = 1 to SIZE
	Input "Enter a number"; scores(count)
	sum = sum + scores(count)
Next

average = sum / SIZE


Print
Print "Numbers", "Average", "Difference from average"
Print "*****************************************************"

For count = 2 to 1 step -1
	difference(count) = scores(count) - average
	Print scores (count), average, difference (count)
Next
sleep
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

Post by dosierb »

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 results

Dim classroomCount As Integer
Dim studentCount As Integer
Dim count As Integer
Dim room As Integer
Dim score As Integer

Input "How many classrooms are there"; classroomCount
Input "How many students are there"; studentCount

Dim numberOfStudents(classroomCount) As Integer
Dim pointsPerClassroom(classroomCount) As Integer


For 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) += score
Next
average = sum / SIZE
average = pointsPerClassroom(room) / numberOfStudents(room)

for count = 1 to classroomCount
Print
Print "Student class scores are as follows:"
Print "Classroom", "# of Students", "Total points", "Average points"
Print "---------------------------------------------------"
    Print room, numberOfStudents(count), pointsPerClassroom(count), average
next

Sleep

Barb


'Barb Dosier
'CO116 Chpt 6Q1
'November 20, 2010
'Program will have the user enter ten numbers and display them in reverse order

Dim sum As Integer
Dim average As Integer
Dim size As Integer
Dim count As Integer
Dim difference As Integer

Print "Enter ten numbers"
size = 2
Dim scores(size) As Integer

For count = 1 to SIZE
    Input "Enter a number"; scores(count)
    sum = sum + scores(count)
Next 

average = sum / SIZE
difference = scores(count) - average

Print
Print "Numbers", "Average", "Difference from average"
Print "*****************************************************"
For count = 2 to 1 step -1
    print scores (count), average, difference
Next

Sleep
square1
Posts: 97
Joined: Nov 12, 2007 2:27

Post by square1 »

Similar problem, you need a different average for each classroom.
See comments in code


Code: Select all

'Barb Dosier
'CO 116 Chpt 6Q3
'Displaying class test score results

Dim classroomCount As Integer
Dim studentCount As Integer
Dim count As Integer
Dim room As Integer
Dim score As Integer

Input "How many classrooms are there"; classroomCount
Input "How many students are there"; studentCount

Dim numberOfStudents(classroomCount) As Integer
Dim pointsPerClassroom(classroomCount) As Integer
'added line:
Dim average(classroomCount) As Double

For 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) += score
Next
'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)
next

Sleep
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

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

Post by dosierb »

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 order

Dim sum As Integer
Dim average As Integer
Dim size As Integer
Dim count As Integer
Dim difference As Integer

Print "Enter ten numbers"
size = 2
Dim scores(size) As Integer

For count = 1 to SIZE
    Input "Enter a number"; scores(count)
    sum = sum + scores(count)
Next 

average = sum / SIZE
difference = scores(count) - average

Print
Print "Numbers", "Average", "Difference from average"
Print "*****************************************************"
For count = 2 to 1 step -1
    print scores (count), average, difference
Next

Sleep
[/quote]
j_milton
Posts: 458
Joined: Feb 11, 2010 17:35

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

Post by j_milton »

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.
Post Reply