I'm new to programming, and the book I'm having to study...doesn't really cover a specific language. However it does (kind of) go into pseudocode. And after reading the required 2 chapters for this week in the book I still don't understand how to do one of the assignments. (There's 4 of them, so I'll probably be here a lot this week.)

The assignment instructions are to "Write pseudocode for an algorithm that could be used to find the weighted average of three test scores. Use variables input1-3 and weight1-3 for consistency. How would you change the design to accept more entries? Does your design account for bad data (like letters or punctuation)?"

Is there anyway someone could explain this a bit better than the book, or help walk me through it without answering it for me?
Considering how long you've been a member of our various Minecraft servers, Reikanobi, I can't believe that this is your first forum post!
Reikanobi wrote:
The assignment instructions are to "Write pseudocode for an algorithm that could be used to find the weighted average of three test scores. Use variables input1-3 and weight1-3 for consistency. How would you change the design to accept more entries? Does your design account for bad data (like letters or punctuation)?"
It sounds like this assignment wants you to think about a number of aspects of the problem:
1) At the simplest, computing a weighted average. What would the equation (in math form, like avg = (a + b + c) / 3) be for a weighted average where input1 is weighted with weight1, input2 is weighted with weight2, and input3 is weighted with weight3?
2) Now, if you wanted to make a program to compute that average, you'd need two additional components. First, you'd need a way to get the three scores and the three weights from the user. Some sort of input routine? How do you make sure they only type digits and decimal points? What things are valid numbers, and what are not (is 100.01 a valid score? a valid weight? What about 50.1? 50.1.4? 4a? 4.a? -1013..5?). Second, you'd need a way to display the result.
3) What if you want to make it accept a variable number of inputs and associated weights? How would you ask for that number? What programming construct would you use to loop (hint) through the number of requested scores and weights, and how could you compute the final average?

Hope this helps! Feel free to answer those inline if you want more nudging towards the answer.
KermMartian wrote:
Considering how long you've been a member of our various Minecraft servers, Reikanobi, I can't believe that this is your first forum post!
1) At the simplest, computing a weighted average. What would the equation (in math form, like avg = (a + b + c) / 3) be for a weighted average where input1 is weighted with weight1, input2 is weighted with weight2, and input3 is weighted with weight3?


Yeah, I'm not one to usually post on forums much. I mostly lurk to be honest. Very Happy

And what does it mean by "weighted average" as opposed to just "average"? As for the code about figuring out the average, the best I can come up with is-

Set average = (input1 + input2 + input3) / 3

-though I'm not entirely sure its right?
That's correct for a regular average; a weighted average is a little bit different. Hopefully Wikipedia's basic example is helpful.
Would something like this be correct? If not, what did I do wrong. Also, am I missing something?

Start

num input1
num input2
num input3
num weight1
num weight2
num weight3

set average = (input1 + input2 + input3) / 3
set weight1 = input1 + average
set weight2 = input2 + average
set weight3 = input3 + average
set averageWeight = (weight1 + weight2 + weight3) / 3

End
Also the answers from the other people (it's more or less an assignment posted on forums for the class) I saw people put-

get input1
-or-
input input1

-and so on before the lines they are included in. Do I need to be doing that, or is that just one way to do it, but not needed?
Reikanobi wrote:
Would something like this be correct? If not, what did I do wrong. Also, am I missing something?

[snip]
I think you misunderstand what a moving average is. Both the inputs and the weights are inputs, and the output is a single weighted average of the three inputs, weighted by their respective weights. The pseudocode you gave does not correctly compute a single output average.

Reikanobi wrote:
Also the answers from the other people (it's more or less an assignment posted on forums for the class) I saw people put-

get input1
-or-
input input1

-and so on before the lines they are included in. Do I need to be doing that, or is that just one way to do it, but not needed?
That's part of the answer to:
KermMartian wrote:
2) Now, if you wanted to make a program to compute that average, you'd need two additional components. First, you'd need a way to get the three scores and the three weights from the user. Some sort of input routine? How do you make sure they only type digits and decimal points? What things are valid numbers, and what are not (is 100.01 a valid score? a valid weight? What about 50.1? 50.1.4? 4a? 4.a? -1013..5?). Second, you'd need a way to display the result.


A small point for forum etiquette: if it's 24 or fewer hours since your last post, please edit your post instead of replying to yourself. We call that double-posting, and it's frowned upon. Smile
Sorry, I'll remember that from now on. Like I stated before, I usually lurk on forums, so I'm sort of bad at forum etiquette. I should look it up...

Also Kerm, would-

weightedAverage = ((input1 * weight1) + (input2 * weight2) + (input3 + weight3) / 3)

-be more correct than what I did before? Or am I still missing the point entirely? And if it's correct would this also be correct?

start

get input1
get input2
get input3
get weight1
get weight2
get weight3

weightedAverage = ((input1 * weight1) + (input2 * weight2) + (input3 + weight3) / 3)
display weightedAverage

end
You've nearly got it! The only thing that that does incorrectly, if I remember the formula for a weighted average accurately, is not divide by the correct value. I think that you may want to sum the weights, but you should double-check.
Well at least I'm starting to understand it somewhat. I'll make sure to double check, thank you for the help. Smile
KermMartian wrote:
the formula for a weighted average accurately, is not divide by the correct value.


The sum of the weights would need to add up to 1 in this case. Since they don't necessarily have to, you might need to make them add up to 1, or otherwise make an adjustion to the formula that takes into account the sum of the weights.

Hinty hint hint: With a straight average, each weight is effectively 1/(number of data values). The sum of these weights is equal to 1. When working with a weighted average, the values of the weights change, but the sum remains 1.

Hopefully this helps with correcting the algorithm.
I don't believe that's accurate: I'd say in a standard non-weighted average, the sum of the weights is actually N (the number of averaged items), with each value having a weight of 1. You need to normalize the weights for the weighted average in some way, either beforehand (ie, weight_actual_1 = weight1 / sum(weights)) or in the average (ie, (weight1*input1 + ....) / sum(weights).
Also, it might not matter so much in pseudocode, but to me it looks like:

Code:
weightedAverage = ((input1 * weight1) + (input2 * weight2) + (input3 + weight3) / 3)
... is dividing the sum incorrectly. The /3 should be outside the parentheses, otherwise the only number being divided is (input3+weight3), not the sum of all 3 additions.
chickendude wrote:
Also, it might not matter so much in pseudocode, but to me it looks like:

Code:
weightedAverage = ((input1 * weight1) + (input2 * weight2) + (input3 + weight3) / 3)
... is dividing the sum incorrectly. The /3 should be outside the parentheses, otherwise the only number being divided is (input3+weight3), not the sum of all 3 additions.


You're right, I didn't even realize I had placed it in the wrong spot. As it's already submitted, so I can't change it Sad but I can still learn what I did wrong in this one to help me do better next time.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement