C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
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:
  #1  
Old September 30th, 2005, 09:07 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 5
Send a message via AIM to Geo.Garnett
To All My for (loops) Helpers!!!

I thought you guyz might want to see some progress instead of always answering my questions. I thought I might show you some results of your efforts. I answered the following question all by me self, thanks to your guyz previous help.
Quote:
You sell the book C++ for Fools. Write a program that has you enter a year's worth of monthly sales(in terms of number of books, not of money). The program should use a loop to prompt you by month, using an array of char * (or an array of string objects, if you prefer) initialized to the month strings and storing the input data in an array of int. Then, the program should find the sum of the array contents and report the total sales for the year.


Here's my solution
Code:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(void)
{
    int i = 0;
    float B_S[12] = {0.0};//Books Sold
    float B_C = 15.0;//Book Cost
    float B_T = 0.0;//Sum of Books Sold
    string M[13]={"Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov","Dec"};
    for ( i = 0; i <12; i++)
    {
        cout<<M[i]<<": ";
        cin>>B_S[i];
        B_T += B_S[i];
        }
    cout<<B_T * B_C<<" Dollars in sales for the year "<<endl;
    system("PAUSE");
    return 0;
}

I know this isn't a question, I just thought this would be relevant to all the ppl who help me on a regular basis to show them that their efforts aren't in vain. <Thanks>
If some one has a faster || just a quicker way, please share...
Side note : to all the ppl that are like "SHUT UP WITH THE FOR LOOPS" this will be the final for(loop) post for me, I promise.


---Official Member of The Itsacon Fan Club---

Reply With Quote
  #2  
Old September 30th, 2005, 09:46 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
Send a message via ICQ to Itsacon
Great program, but just to be a whiner:
  • your array of months is sized 13, even though there are only 12 months.
  • you store all the monthly sales in an array, but never use them. It would be neater (memory-wise) to use a single temporay integer, since you immediately add the value to the B_T variable anyway. Effect is the same, but makes the memory footprint of your program 11 integers smaller. (Yes, I know on a modern computer this doesn't matter in the least, but if you're ever programming microcontrollers, you'll see why this can be important)
  • It is good practice when doing calculations between ostream operators (<<) to put them between parentheses.
    A bad compiler may give the << the preference value of the left-shift operator (which is the same), and output the first value before calculating the product.
    so
    Code:
    cout<<(B_T * B_C)<<" Dollars in sales for the year "<<endl;
    Instead of
    Code:
    cout<<B_T * B_C<<" Dollars in sales for the year "<<endl;
  • You're not using one of the officially recognized Brace Styles. My preference is the Allman style, but that's up to you.
Don't get me wrong though, very neat program I just like to whine

Well done, my young padawan!
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 564

Reply With Quote
  #3  
Old September 30th, 2005, 10:31 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 5
Send a message via AIM to Geo.Garnett
Don't worry I'm the type of person that likes constructive criticism. As long as you don't have thin skin that is the best way to improve yourself. I use to work in construction, and you can take the constructive right out of criticism because that's all you got, like it or not. I have noticed that the regular ppl of the world(other than construction ppl) put things much nicer

And yes I agree, conserving memory in a simple program this small, yes it doesn't make a difference, but like you say, and not even with just micro controllers, is that once you get to programming on a much larger scale it becomes more important to conserve memory when you can, and if you don't set out some good practices you are going to waste all the memory you have and still have half your program left to write.

The story of my life again

By the way thanks for that link too.
Side Question : What is your opinion on Hungarian Notation and in your job is it even necessary, I have read mixed reviews on it so far.

---Official Member Of The Itsacon Fan Club---

Reply With Quote
  #4  
Old October 4th, 2005, 11:04 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
Send a message via ICQ to Itsacon
Quote:
Originally Posted by Geo.Garnett
Side Question : What is your opinion on Hungarian Notation and in your job is it even necessary, I have read mixed reviews on it so far.

---Official Member Of The Itsacon Fan Club---


First time I heard of it, had to go to Wikipedia to find out what it was.

In my experience, giving your variables clear names is important, but only for variables that stay around longer.
Loop variables (such as the famous i for for loops) should be as short as possible ('loopvariableforsecondloopincalculatefunction' does not make your code any clearer, especially when used to index arrays ), whereas a temporary variable is very clear if you call it 'temp'.
For instance, when I retrieve info from a database, the result is usually called 'result', and the subsequent rows are called 'row'. This is ok, since they all have a very temporary scope in which I use them. Any place where I use them, I can still see the code where they were first assigned. (This is a pretty good guideline when deciding the scope in which you use a variable).
If you're keeping track of the number of errors occuring during the entire execution of a program, naming that variable 'numberOfErrors' is much clearer then 'e', since someone reading this at the end of your code has no idea whether 'e' is an error, or 2.718281828...



If you feel hungarian is a clear way to do this, it might be good to learn it. Same goes for several other naming methods. It's more important to be consistent than which convention you use.
here is a good list of the pros and cons of Hungarian.

Reply With Quote
  #5  
Old October 4th, 2005, 12:18 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 5
Send a message via AIM to Geo.Garnett
Well I don't know how Wikipedia defined Hungarian notation, but its basically putting an identifier before your variable that way at any point in your code you will always know of what type it is. int, float, pointer and so on. Its a Microsoft thing mostly but someone had mentioned to me if I was programming in C/C++ and had plans of programming for windows at all I would need to not only get familiar with it but also start to use it.
Heres a link that shows you a little bit of the examples of Hungarian notation.
http://web.umr.edu/~cpp/common/hungarian.html

But ya, I just wanted someones opinion on that, and being that you are in programming and you have never heard of it. I don't think I'll have to worry about it too much.

Reply With Quote
  #6  
Old October 4th, 2005, 03:17 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 14 m 46 sec
Reputation Power: 5
Quote:
Side Question : What is your opinion on Hungarian Notation

Me, I don't need it because I name all my variables accurately enough to know what type they need to be.

Well, usually, at least. I start rewriting code and using variables for different functions it gets messy
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #7  
Old October 4th, 2005, 05:00 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 5
Send a message via AIM to Geo.Garnett
Ya, I have gotten mixed reviews from different ppl, myself. Some say that its good practice and some say it is only a way to confuse yourself later, because eventually through the course of your code it will become less and less accurate, therefore making it difficult to read and understand by others. So, that was my opinion too, just make your variables the easiest for ppl to understand.

Reply With Quote
  #8  
Old October 4th, 2005, 07:34 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
Send a message via ICQ to Itsacon
To the smart programmer, there are two ways of writing code:
  1. Armour plated. The code is perfect and no-one ever needs to revisit or revise it.
    This is the code that makes companies want you to work for them.
  2. Buggy, but unreadable. This is the code that makes companies want you to KEEP working for them, since you're the only one with the slightest chance of getting the code to do <insert new function here>...

Other programmers may go for option 3, which is code that can be updated and edited by any code monkey, leaving you without a job....

Reply With Quote
  #9  
Old October 4th, 2005, 07:45 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 5
Send a message via AIM to Geo.Garnett
lol, Itsacon. Well, I hope in time I can be the number one, because right now Im more on the side of number two. . A comment on number three, I totally agree with that but it seems that all the ppl I have talked to so far are like you need other ppl to be able to read your code, but I think the third one is best, at least when your coding for money that is. So I guess the longer I'm doing this the better I will know what to do when the time comes.

Reply With Quote
  #10  
Old October 5th, 2005, 01:56 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 956 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 22 m 40 sec
Reputation Power: 5
If your employer, or direct boss, has any knowledge of coding you will not get away with being number 2. Number 2 and 3 only work in small projects where you are the only programmer (or the only good one ) If you think you can make yourself indispensable by being the only one who understands your code, be careful.. You might think you are a l33t coder but your boss might just think someone else is a cheaper l33t coder

I wish their was a good way to rate software, like a quality assurance grade.. There are so many ways to implement something, but so many ways are inferior which can not be seen by anyone until you really dig into the code..

Reply With Quote
  #11  
Old October 5th, 2005, 01:56 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1973911 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
Send a message via ICQ to Itsacon
Yeah, option 3 is usually the best (I try to go for that one too), especially since most software projects are done by more than one person at the same time...

Note, it is of course possible to combine options 1 and 3, but the programmer with that kind of patience and skill hasn't been born yet

Reply With Quote
  #12  
Old October 5th, 2005, 02:37 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 5
Send a message via AIM to Geo.Garnett
TY guys for all your insight, although Im not a programmer yet, information about the working arena is good to know, before I get involved in it. I'm from a totally different proffession all together so the change will be drastic I'm sure. and little tips like that are good to know in advanced.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > To All My for (loops) Helpers!!!


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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek