Array Algorithm

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

Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Array Algorithm

 
0
  #1
Nov 19th, 2008
I'm really not understanding what's going on with the array in this. Here's the question.

The following code totals the values in each of the two arrays. Will the code print the correct total for both arrays?

  1.  
  2. int total = 0;
  3. int count;
  4.  
  5. for (count = 0; count <= 25; count++)
  6. total += array1[count];
  7. cout << "The total is " << total << endl;
  8.  
  9. for (count = 0; count <= 25; count++)
  10. total += array2[count];
  11. cout << "The total is " << total << endl;

I'm not understanding this at all. Both arrays hold 25 integer elements but they aren't equal to anything, so im just lost here... or maybe it's really simple and I'm thinking too hard about it...
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: Array Algorithm

 
0
  #2
Nov 19th, 2008
If both array1 and array2 contain 25 elements then you should write your for loops
  1. for (count = 0; count < 25; count++)
as arrays of n elements are indexed from 0 to n-1.

Moreover, the second cout will print the sum of all the elements of the second array plus the sum of all the elements of the first array, as long as you don't reset your total variable between the for loops.

EDIT: What do you mean with "they aren't equal to anything" ? Please clarify this.
Last edited by mrboolf; Nov 19th, 2008 at 5:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Array Algorithm

 
0
  #3
Nov 19th, 2008
Leave out the equal sign in <= !
Else you are counting from 0 to 25 inclusive, which is 26, not 25.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Array Algorithm

 
0
  #4
Nov 19th, 2008
Don't know what I was thinking there. I changed it to just greater than and then inbetween the two for loop I made total = 0 again. Both arrays outputted -20 so that seems to be working properly. So by not initializing total to = 0 again after the first array is finished will cause total to equal whatever it was in the first output which would be -40 which I checked and so the counter in the for loop must be written as < and not <= because the element starts at 0? I'm going to look in my book about the indexing
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,908
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 273
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Array Algorithm

 
0
  #5
Nov 19th, 2008
Don't worry, there are many people on this globe(including me) that have made this mistake. Join the club!
Last edited by ddanbe; Nov 19th, 2008 at 6:21 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
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