| | |
Array Algorithm
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
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?
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...
The following code totals the values in each of the two arrays. Will the code print the correct total for both arrays?
C++ Syntax (Toggle Plain Text)
int total = 0; int count; for (count = 0; count <= 25; count++) total += array1[count]; cout << "The total is " << total << endl; for (count = 0; count <= 25; count++) total += array2[count]; 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...
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
If both array1 and array2 contain 25 elements then you should write your for loops 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.
C++ Syntax (Toggle Plain Text)
for (count = 0; count < 25; count++)
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.
•
•
Join Date: Oct 2008
Posts: 22
Reputation:
Solved Threads: 0
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
![]() |
Similar Threads
- How sort a Dynamic Array? (C++)
- partition algorithm question (C)
- Reversive Array/Integer (C)
- recursive algorithm (C)
- sorting an array of string (C)
- Eigenface Algorithm (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: .h source file acting up
- Next Thread: Problem with memory allocation
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






