Hi, I am extremely new to C++, I apologise in advance if my question is long winded or difficult to follow but here I go. I am reading through a book called C++ for Dummies. In one of the examples used in the Arrays section we are given some code that asks the user to input numbers and the program will print out the average of those numbers. I don’t have a problem understanding the code itself but I do have a question regarding the results of a ‘for’ loop used in the program a couple of times. The first time it is used is here like this:
In the program this gets printed out, If the numbers entered were 5,4,5,6 for example, the printout would read:
0 = 5
1 = 4
2 = 5
3 = 6
However, when the ‘for' loop is used again like this:
1 2 3 4 5 6 7 8 9
int averageArray(int nValues[100], int nCount)
{
int nSum=0;
for(int i=0; i<nCount; i++)
{
nSum+=nValues[i];
}
return nSum/nCount;
}
The nCount ends up equalling 4 (I know this because I added a small piece of my own code that I have not included here that gave my the print out of nSum and nCount).
My question is why doesn’t nCount equal 3? I understand the ‘for’ loop goes through four iterations but as the first number is 0 so why isn’t 3 the final number in this variable?
Also watch out for integer division with code like this. Integer division truncates, so 9/11 is zero for example. If there is an average involved make the type of nsum and the function return double
I think I have worked out my problem. The program asks for a negative number to be entered to end the input cycle of numbers so this becomes the 5th number entered but number four in the array. For example I entered the numbers 5,4,5 and 6 and -1 to end the loop that asks for numbers to be entered (the negative number is not included as an entered number, it just acts as the prompt to leave that loop) resulting in this:
0 = 5
1 = 4
2 = 5
3 = 6
4 = -1
I apologise for the lack of info to work this out but I was hesitant to put all the code in as it would have been a lot, next time I will try to be clearer in my questions. Thank you everyone for your help.
Where the numbers come from is irrelevant from the point of view of displayArray and averageArray. As long as the values for nValues (the array of all the values) and nCount (the number of values) are passed to the functions correctly they will do their work correctly (assuming they are correctly implemented).
I agree that writing the parameter as int nValues[100] is unnecessary and misleading because the number 100 is simply ignored.
int* nValues has the advantage that it more clearly shows that the argument is passed as a pointer.
int nValues[] has the advantage that it more clearly shows the intention, that the argument is supposed to be (a pointer to the first element in) an array rather than a pointer to a single object.
you can't even use std::size(nValues) because nValues is treated as a pointer
Which is why std::size should be prefered over the sizeof hack. It's better to get a compilation error that you can easily fix right away rather than a silent bug.
:) Always prefer a compilation error to a run-time issue...
Why not just use std::array (or std::vector) instead of a c-style arrays. Then there's no need to pass a separate param for number of elements (which could be incorrect)... Teach these first before c-style arrays.
Why not just use std::array (or std::vector) instead of a c-style arrays.
As a function parameter a pointer and a size is more flexible because it can accept a varying number of elements from either raw "c-style" arrays, std::array or std::vector.
It may be a better choice. The question is why they're teaching new programmers to use c-style arrays.
These writers and professors can teach basic programming principles just fine using std::array and std::vector.
std::vector is an entirely c++ entity (good though it is). std::array I would find very difficult to find a use for.
"Basic programming principles" suggest that you should use something (a) simple and (b) relatively language-agnostic ... a c-style array fits the bill. (It's also about the only type of array you could use in mixed-language processing).