Using Arrays?
Author |
Message |
SAValkyrie
|
Posted: Thu Nov 01, 2012 9:33 pm Post subject: Using Arrays? |
|
|
I have to make a program that can average a series of marks. Before I enter the series, I have to have the program ask the user how many marks there are in the series then read it in.
This is my coding till now
Turing: |
% The "Mark Average" program
% Asks you the number of marks then averages it
% Created by Christopher An
var number, count, marks:int
count:=0
put "How many marks do you wish to average?"
get number
put "Input your marks"
loop
get marks
count:=count+1
exit when count=number
end loop
put marks/number
|
So I got the part where program asks you a number of marks. However, I use put marks/number part to average the marks but the answer it gives me is the only for the last mark that a user inputted. So, I wonder is there a way to do this by not using arrays because I think arrays can be used here...
If it's not possible without arrays can you guys plz write an example using the arrays? thanks.[/syntax] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Thu Nov 01, 2012 9:36 pm Post subject: RE:Using Arrays? |
|
|
You don't need an array. Forget about programming for a second and think about how you calculate averages with regular boring old math.
There's really only one step that you're missing, and it's the same if you use an array or not. |
|
|
|
|
|
Dan
|
Posted: Thu Nov 01, 2012 9:40 pm Post subject: RE:Using Arrays? |
|
|
I moved your topic to Turing Help.
Insectoid is correct, this problem does not need an array. Also you might want to try using a for loop rather then using "exit when", it would make your code a bit more readable. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
SAValkyrie
|
Posted: Thu Nov 01, 2012 10:23 pm Post subject: Re: Using Arrays? |
|
|
Well.... when you want to find the average of several marks. You add them all up and divide by a number of marks(in this case) you inputted...
Turing: |
% The "Mark Average" program
% Asks you the number of marks then averages it
% Created by Christopher An
var number, count, marks:int
put "How many marks do you wish to average?"
get number
for counter:1..number
put "Input your marks"
get marks
put counter
end for
put marks/number
|
I tried doing it with using for loop.
I can do that but the prob is my late sentence
I would normally do this for average..
Turing: |
(mark1+mark2+mark3+mark4+...)/numberofmarks
|
but i cant seem to do that in this case ?![/syntax] |
|
|
|
|
|
Dan
|
Posted: Thu Nov 01, 2012 10:27 pm Post subject: RE:Using Arrays? |
|
|
Well if you have a set of marks (10, 3, 50, 55, 40) then doing:
x = 0
x += 10
x += 3
x += 50
x += 55
x += 40
Then the average is x/5.
How would you make a program that does the same thing but input the marks rather then have them hard coded and allow any number of marks to be put in? |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
SAValkyrie
|
Posted: Thu Nov 01, 2012 10:31 pm Post subject: Re: Using Arrays? |
|
|
Turing: |
% The "Mark Average" program
% Asks you the number of marks then averages it
% Created by Christopher An
var number, marks, total:int :=0
put "How many marks do you wish to average?"..
get number
for counter:1..number
put "Input your marks"
get marks
put counter
total+=marks
end for
put "Average:", total/number
|
Oh I got it... it was easier than i thought..
I am starting to get ahold of turing so thanks for help guys |
|
|
|
|
|
|
|