Computer Science Canada Help on dynamic arrays |
Author: | omni [ Wed Nov 10, 2004 6:10 pm ] |
Post subject: | Help on dynamic arrays |
I want to make a program that will just calculate an average of numbers. It will receive input from a file. I want my program to be able to accept a variable amount of numbers. I'm thinking of using dynamic memory for this. (pseudocode) Open file stream create array set SUM=0 set COUNTER=0 Loop receive input from file move input into array SUM=SUM + input from the array increase counter allocate more memory for the next number end loop AVERAGE=SUM / COUNTER (END) Is this logic right ? |
Author: | wtd [ Wed Nov 10, 2004 6:11 pm ] |
Post subject: | |
Storing the numbers in an array would be more flexible, but isn't necessary for this problem. Each time you read a number, add it to a total, then increment a counter by 1. At the end, divide the total by the counter. |
Author: | omni [ Wed Nov 10, 2004 7:28 pm ] |
Post subject: | |
LMAO, thank you wtd. I don't know what I was thinking. What if I want to keep the numbers in the array for later use? |
Author: | wtd [ Wed Nov 10, 2004 7:53 pm ] | ||||
Post subject: | |||||
If we're talking about C++, then don't use arrays. Instead, use a vector.
Or wrap up the sum and average functionality by subclassing vector.
|
Author: | omni [ Wed Nov 10, 2004 8:18 pm ] |
Post subject: | |
Ow my head hurts now. Gotta learn vectors now. *shudders* reminds me fricking physics class which i'm failing. |