arrays! a little help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 30
Reputation: FrancisC07 is an unknown quantity at this point 
Solved Threads: 1
FrancisC07's Avatar
FrancisC07 FrancisC07 is offline Offline
Light Poster

arrays! a little help!

 
0
  #1
Dec 5th, 2008
oopss i still have a problem!!.. What's wrong with my code?
  1. main()
  2. {
  3.  
  4. int a[4];
  5. int b[4];
  6. int counter,sum=0;
  7.  
  8. cout<<"JANUARY";
  9. for(counter=0;counter<4;counter++)
  10. {
  11. cout<<"Numbers of Rainfall:";
  12. cin>>a[counter];
  13. }
  14.  
  15. for(counter=0;counter<4;counter++)
  16. {
  17. sum+=a[counter];
  18. }
  19.  
  20. cout<<"The numbers of rainfall in the first month"<<sum<<endl;
  21.  
  22. cout<<FEBRUARY;
  23. for(counter=0;counter<4;counter++)
  24. {
  25. cout<<"Numbers of Rainfall";
  26. cin>>b[counter];
  27. }
  28.  
  29. for(counter=0;counter<4;counter++)
  30. {
  31. sum+=b[counter];
  32. }
  33.  
  34. cout<<"The numbers of rainfall in the second month"<<sum<<endl;
  35.  
  36. getch();
  37. return 0;
  38. }
now my aim is to add all the values of first month and then print it. and same as the second month.(add all the values of second month and then print it). And when i run the program and then i put a values in the first month and its working fine but my problem is in second month because after i put a values in second month, the output of 1st month will add to the values of second month.

ex.
JANUARY
Numbers of Rainfall:4
Numbers of Rainfall:3
Numbers of Rainfall:2
Numbers of Rainfall:1

The numbers of rainfall in the first month 10

FEBRUARY
Numbers of Rainfall:5
Numbers of Rainfall:4
Numbers of Rainfall:3
Numbers of Rainfall:2

The numbers of Rainfall in Second month 24

now i need help on the second month on how to print the second month without adding the first month..?

Sorry i'm a newbie programmer but i'm studying hard. thanks guys..
Last edited by Narue; Dec 5th, 2008 at 11:18 am. Reason: added code tags
"One more round and it's bottle to the ground"
-NOFX
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 8
Reputation: sweeya is an unknown quantity at this point 
Solved Threads: 1
sweeya sweeya is offline Offline
Newbie Poster

Re: arrays! a little help!

 
0
  #2
Dec 5th, 2008
make sum = 0 after you have calculated the sum of first month.
Last edited by sweeya; Dec 5th, 2008 at 11:18 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: arrays! a little help!

 
0
  #3
Dec 5th, 2008
You need int a[4] and int b[4] for every month, something like:

int a[12][4];
int b[12][4];
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: arrays! a little help!

 
0
  #4
Dec 5th, 2008
Just put sum = 0; before starting to add the second month rainfalls.

Please read this and make sure to use code tags when posting code from now on

EDIT: sweeya was faster, sorry.
Last edited by mrboolf; Dec 5th, 2008 at 11:21 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 30
Reputation: FrancisC07 is an unknown quantity at this point 
Solved Threads: 1
FrancisC07's Avatar
FrancisC07 FrancisC07 is offline Offline
Light Poster

Re: arrays! a little help!

 
0
  #5
Dec 5th, 2008
thanks for helping guys! sorry for i don't use the code tags.. it won't be happen again..
"One more round and it's bottle to the ground"
-NOFX
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 960
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: arrays! a little help!

 
0
  #6
Dec 5th, 2008
Well as they already said reset sum to zero; next problem in month two would be, you forgot quotes " in cout, and I'm assuming the lack of int before main() is a copy-paste oversight.
You could create a class that holds all of the months, extra info and functions that deal with them; if you've gotten that far in your studies, yet.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC