C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old April 25th, 2008, 02:03 PM
814 814 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 1 814 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 20 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old May 6th, 2008, 05:04 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 338 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 14 h 13 m 10 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
Quote:
float array[1000];
int n = 0;

cout << "Please enter as many positive numbers as you wish,


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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Need help with pseudocode


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway