❔ Can someone help explain this is a more basic way.
I get what arrays, integers, parameter and such are but reading it this way doesn't make sense.
89 Replies
What exactly you don't understand here?
The hole part after decalring a method
Reading it makes my head go blank
go step by step
you have the method signature, do you know what an array is?
Yes I have already delcared one outside this method
yup, do you know what it is?
assuming you mean the parameter
array
I understand arrays but not well
My knowledge of them lead to confusion due to the practice making me random.Range which was something I was not ready for
would you agree it's a collection of something?
Yes, it can hold collection of string, int, and anyother types simlair
alright, so we have the iteration and the sum
have you learned loops,
foreach
or for
?I learned for loops and while loops but not for each
for
loop is fine, foreach
is what I usually always prefer
how would you iterate over the array
you've provided using for
loop?To iterate over an array means to go over all of its elements, in order
With a loop most commonly, whatever loop it might be
Well I would do
for ( int i = 0; i < array.Length; i++)
{
print(array[i];
}
sure, but we should keep track of the sum
instead of printing the element
How would we do that?
you have an array of
int
's so create an int
that you add onto on each iterationSo a new int?
yup
for ( int i = 0; i < array.Length; i++)
{
print(array[i];
array = new int[]
}
Something like this?
which one is the
int
that holds the sum?But puting in number ranging from 0 and up?
array which is out the method
the sum
not the elements
you need to calculate the sum of all elements in
array
So the numbers within the array?
no, the sum of the numbers
in your case 1+2+3+4+5
Ain't those the numbers in the array?
yes
those are the numbers you're calculating the sum of
Sorry if i'm being stupid but like them all together. The sum of them all
yes
So 15?
in this case, yes
So what do we do with the 15?
nothing
you are not calculating them by hand, you need to iterate (loop) over the
array
and sum the numbersOh so the loop has to go 15 times?
no
the loop needs to iterate as many times as there are numbers in
array
Which there are only 5 of. So the loop needs to go for only 5 times. 0, 1, 2, 3, and 4 correct?
in this case, yes
Wait
your loop here was fine
I think I understand
only thing you needed was the integer to sum them
So were using the array and += them to the new interger to add them all up?
exactly
Oh.... Thank you
no problem
I would have been stuck here for a while so thank you very much
or just
I assumed the term "iterate" means looping by hand
Thats clearly not in the spirit of the assignment
my bad 🙂
no worries
Why do I get 0?
It's great that you want to help others @tttkisuke but remember that we do not believe in spoonfeeding and to keep your advice appropriate to the person askings skill level, as much as you can 🙂
Other stuff was for different Methods I did
Look at what is on what side of the operation
Oh
what does
+=
do?got it, I forgot it would add it to the left side. My bad
👍
on a side note, why is
z
declared as a parameter?Cause i already have a, b, and c. so I just made it z
Tho I could have use c
tahts not what I meant
I mean, why is it declared in the method signature?
Good question.....
if I were to call your method, I'd expect to call it as
int sum = ArraySum(myArray);
yeah, i got rid of the z since it wasn't needed
Okay. What did you use instead?
You still need a place to store the total sum, as you are building it up, no?
Nothing as i already had a public z so it was uneeded there
Here
All I needed was the array
Can you show me how you call this method?
awesome
lets copypaste that
Ye!
just put it twice
like:
and run your code
Why put it twice?
it works on it own
indulge me 🙂
I have my reasons to ask it.
I've done it
yep, and if you run it?
what are the results
Oh
anything odd about the results? 🙂
I see now
so, how might we fix this?
it made go to 30 since 15 was never reset
Not sure on the top of my head
well, I see you know about variables
do you know of the different "scopes" variables can have?
Uh, no. I have not heard that
okay, well, the way (and place) you have declared
z
currently makes it a "field"
it belongs to a class and keeps its state as long as the object is alive
there is another variable kind we call "local"
they are temporary, and only exist within their current method (or even smaller scopes, like inside an if statements body)
here is an example
z
is a field currently. localVariable
is a local variable
its down to where its declared that decides what it is, but things like private
and public
are not used with locals, as it doesnt affect themSo itstead of making it public, we have it private so it resets each time it's used>
No, you want z to be a local variable, not a field
If you look at the example I posted above, especially where a field vs a local variable is declared, can you see the major difference?
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.