| Dizzy (6) | |||
The assignment asked to modify a program that will calculate and print the sum and mean using an array. The program compiles and runs flawlessly. But did I use the array correctly? Im very new to coding just an FYI :)
| |||
|
Last edited on
|
|||
| MiiNiPaa (232) | |
|
1) You have included iostream twice. 2) You are using variable length arrays on line 16. This is unofficial extension an may not work on every compiler. It's fine for now, but when you know the right way to do this use it instead. 3) Use doubles instead of floats. 4) Your for statement should be: for (count=0; count<n; count++)Array indexes in C++ starts from 0! When ypu write int x[5] valid indexes will be x[0], x[1], x[2], x[3] and x[4].EDIT: 5) Why you defined a size variable? Why didn't you use n as array size? | |
|
Last edited on
|
|
| Dizzy (6) | |
|
for (count=0; count<n; count++) If the counter starts at 0 when the computer prompts the user "How many numbers?" If I say I want to use 2 numbers, it will prompt for 3 since the counter is starting at 0 instead of 1. | |
|
|
|
| MiiNiPaa (232) | |
|
it won't Did you see that I have used count<n instead of count<=n? so there will be 2 iteration: for n==0 and n==1; | |
|
|
|
| usandfriends (186) | |||
| |||
|
Last edited on
|
|||
| Chervil (1203) | |
|
Although an array is used, here, it isn't actually useful. You could just as well have used a single integer instead. In any case, each value is stored in the same element, array[n]But since the array index ranges from 0 to n-1, the element actually used is outside the array and is invalid. | |
|
Last edited on
|
|
| Dizzy (6) | |
| I appreciate the code. usandfriends but the code i have is what i must modify. I was not to write new code but modify the one I had. Mii, I have made the adjustments and it works! Thank you | |
|
|
|
| Chervil (1203) | |||
In my opinion, the best way to modify it, would be to ditch the array completely as there is no benefit in its being there.
Alternatively, if this is an exercise in the use of arrays, use two stages. In the first, get the input and store it in the array. In the second stage, loop through all the array elements and find the total, before finally getting the average. | |||
|
Last edited on
|
|||
| usandfriends (186) | |
|
Wow, Chervil, I completely forgot that I wrote a program that finds the sum and average of a list without arrays. Nice thinking! +1 | |
|
|
|
| Dizzy (6) | |
| Lol, you guys are absolutley right. The original code was without an array, the assignment was to modify the program so that an array was used. Although it is useless, it was the point of the assignment. I prob should have let you guys know that before. | |
|
|
|
| Chervil (1203) | |||
|
Thanks for the clarification. Anyway, in my opinion, you can put the array to work by taking two passes through it, the first to get the user input and the second to get the sum. At least this will pick up any obvious errors, such as using the wrong index value etc. I've used new and delete here as standard C++ doesn't allow the array size to be a variable (though some compilers have an extension to enable this).
| |||
|
|
|||