| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
General - Need help with pseudocode
Hey everybody, I am about ready to pull my hair out converting this code to pseudocode. I have gotten past the declarations, but after that I am having lots of trouble.
#include <iostream> using namespace std; float sum(const float a[], const int b); int main() { float array[1000]; int n = 0; cout << "Please enter as many positive numbers as you wish, then press the equals key to find the average of your numbers"<< endl; while (cin >> array[n]) { n++; } cout << "The Average of your numbers is " << sum(array, n)/n << endl; return 0; } float sum(const float a[], const int b) { float total = 0.0; for (int i=0; i<b; i++) { total = total + a[i]; } return total; } If anybody can give me any help or tips. Especially with this line; while (cin >> array[n]) { n++; } Thanks. |
|
#2
|
|||
|
|||
|
Quote:
If a teacher told you to do that, please kick him for me. If a book told you to do it, please burn it. Code:
#include <iostream>
#include <vector>
using namespace std;
int main {
vector<float> bta; //better_than_array is a bit long :)
//...
while (true) {
float f;
cin >> f;
if (cin) break; // See footnote
bta.push_back(f);
}
...
}
That if line is what you're really checking - specifically you'd be checking for cin.fail() but this allows end-of-file to work as well. The idea is that you don't add the last element if it wasn't valid input anyway. (Just as you don't increment n in your code) After that, you can use it like the array you're already using, 0 to bta.size() instead of 0 to n. (Or use iterators, which I should probably spare you from for now.) The point? The long version can be found in http://www.research.att.com/~bs/new_learning.pdf The short version:Doing it like that, you can store a "unlimited" number of values, instead of just 1000. Your version is brittle at best, and downright broken if someone decides to attack it. (Don't get in the habit of writing buffer overflowing programs to begin with!) The only real downside to the standard library containers is that compilers may produce quite messy error messages when you do something wrong. (Usually it's a matter of finding the one line that matters) They're learning though, and with concepts (which is expected to be in the next c++ standard, currently codenamed C++0x because they don't know if it will be 08 (this year) or 09) the error messages are that much better. btw, Any nonspace nonnumeric non-'.' characters will work, not only =. Alternatively, since you're doing averages, you can just skip arrays/vectors entirely: float f=0, g; int i=0; while (cin>>g) { f+=g; i++; } cout << f/i; In pseudocode, I'd basically say Code:
initialize storage while user input is valid numbers (floats) add to storage compute the average display it Which is valid for your array version, my bta version, and the single-float version. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > General - Need help with pseudocode |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|