Need hellp with my array

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

Join Date: Jun 2005
Posts: 4
Reputation: mav1 is an unknown quantity at this point 
Solved Threads: 0
mav1 mav1 is offline Offline
Newbie Poster

Need hellp with my array

 
0
  #1
Jul 28th, 2005
I am working on displaying the amount ofodd numbers in this array. It always says there is 5. That is not correct. When I use the debug it shows that it is placing a different value in the myArray and using that to determine the amount of odd numbers. Please someone take a look.

  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. const int MAX = 10;
  6. int myArray[MAX] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
  7. int odd = 0;
  8.  
  9. int main()
  10. {
  11. for (int i = 1; i < MAX; i++)
  12. {
  13. if ((i%2)==1)
  14. odd++;
  15. }
  16. cout << "Number of odd integers: " << odd << endl;
  17.  
  18. return 0;
  19. }
<< moderator edit: added code tags: [code][/code] >>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need hellp with my array

 
0
  #2
Jul 28th, 2005
The first element of an array is at index zero. That is, myArray[0] contains 1, and myArray[10] contains 11. Right now, your for loop iterates i from 1 to 9, and you probably want to iterate from 0 to 9.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need hellp with my array

 
0
  #3
Jul 28th, 2005
What's the point? Your code doesn't even look at the array at all...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need hellp with my array

 
0
  #4
Jul 28th, 2005
Originally Posted by winbatch
What's the point? Your code doesn't even look at the array at all...
Oh man, I really missed that...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 6
Reputation: The Senate is an unknown quantity at this point 
Solved Threads: 1
The Senate The Senate is offline Offline
Newbie Poster

Re: Need hellp with my array

 
0
  #5
Jul 29th, 2005
Just write "myArray[i]%2" in the "if", because you are only checking to see if the iterator is odd or not.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC