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 May 24th, 2006, 04:37 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
2-D array addition

Could someone give me a bit of advice on how to add the contents of a 2D array? I am working from a book and one of the exercises ask you to enter book sales for a 3 year period. Then it aska you to calculate the total for each year then the total for all the years. I can get the total for all the years but for the life of me i can't get the total for each individual year. I do a nested loop but it keeps adding all the arrays. I will put the code here:

// 2D_array_for_3_years.cpp -- storing 3 years of book sales in a 2D array
#include <iostream>

const int YEARS = 3;
const int MONTHS = 12;
int books[YEARS][MONTHS];

using std::cout;
using std::cin;
using std::endl;

int main(void)
{
cout << "Enter each monthly value of books for a 3 year period.\n\n";

int individual = 0;
int combined = 0;
int year, month;

for ( year = 1; year <= YEARS; year++ )
{
cout << "Year #" << year << ": " << endl;
for ( month = 0; month < MONTHS; month++ )
cin >> books[year][month];
cout << endl;
}

// output each individual years total



for ( year = 0; year <= YEARS; year++ )
for ( month = 0; month < MONTHS; month++ )
combined += books[year][month];
cout << "The combined total for all three years is: " << char ( -100 );
cout << combined;



return 0;
}

i just want someone to point me in the right direction if at all possible. Thanks in advance

Paul820

Reply With Quote
  #2  
Old May 24th, 2006, 04:57 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
I don't see any code that attempts to total individual years. but the pseudocode would be:
Code:
iterate through years
    total = 0
    iterate through months
        total += current
    end iterating through months
    output total
end iterating through years

Reply With Quote
  #3  
Old May 24th, 2006, 05:23 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
Thank you

Thanks a lot ubergeek, i have been banging my head for about 3 hours trying to get it to work. You're right the code wasn't there because it ended up doing the same as the total for all the years. I just posted the code so people would get a better understanding of what i wanted.

Anyway, followed your advice :

// output each individual years total
int y, m;
for ( y = 1; y <= YEARS; y++ )
{
int individual = 0;
for ( m = 0; m < MONTHS; m++ )
individual += books[y][m];
cout << "Year #" << y << " : ";
cout << individual << endl;

The way i was doing it, it was cycling through the whole lot. Thanks for the really fast reply, appreciate that.

Paul820

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > 2-D array addition


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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT