C
C#9mo ago
MRW419

❔ 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.
No description
89 Replies
Saurus
Saurus9mo ago
What exactly you don't understand here? HmmRTX
MRW419
MRW4199mo ago
The hole part after decalring a method Reading it makes my head go blank
SG97
SG979mo ago
go step by step you have the method signature, do you know what an array is?
MRW419
MRW4199mo ago
Yes I have already delcared one outside this method
SG97
SG979mo ago
yup, do you know what it is? assuming you mean the parameter array
MRW419
MRW4199mo ago
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
SG97
SG979mo ago
would you agree it's a collection of something?
MRW419
MRW4199mo ago
Yes, it can hold collection of string, int, and anyother types simlair
SG97
SG979mo ago
alright, so we have the iteration and the sum have you learned loops, foreach or for ?
MRW419
MRW4199mo ago
I learned for loops and while loops but not for each
SG97
SG979mo ago
for loop is fine, foreach is what I usually always prefer how would you iterate over the array you've provided using for loop?
Angius
Angius9mo ago
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
MRW419
MRW4199mo ago
Well I would do for ( int i = 0; i < array.Length; i++) { print(array[i]; }
SG97
SG979mo ago
sure, but we should keep track of the sum instead of printing the element
MRW419
MRW4199mo ago
How would we do that?
SG97
SG979mo ago
you have an array of int's so create an int that you add onto on each iteration
MRW419
MRW4199mo ago
So a new int?
SG97
SG979mo ago
yup
MRW419
MRW4199mo ago
for ( int i = 0; i < array.Length; i++) { print(array[i]; array = new int[] } Something like this?
SG97
SG979mo ago
which one is the int that holds the sum?
MRW419
MRW4199mo ago
But puting in number ranging from 0 and up? array which is out the method
MRW419
MRW4199mo ago
No description
SG97
SG979mo ago
the sum not the elements you need to calculate the sum of all elements in array
MRW419
MRW4199mo ago
So the numbers within the array?
SG97
SG979mo ago
no, the sum of the numbers in your case 1+2+3+4+5
MRW419
MRW4199mo ago
Ain't those the numbers in the array?
No description
SG97
SG979mo ago
yes those are the numbers you're calculating the sum of
MRW419
MRW4199mo ago
Sorry if i'm being stupid but like them all together. The sum of them all
SG97
SG979mo ago
yes
MRW419
MRW4199mo ago
So 15?
SG97
SG979mo ago
in this case, yes
MRW419
MRW4199mo ago
So what do we do with the 15?
SG97
SG979mo ago
nothing you are not calculating them by hand, you need to iterate (loop) over the array and sum the numbers
MRW419
MRW4199mo ago
Oh so the loop has to go 15 times?
SG97
SG979mo ago
no the loop needs to iterate as many times as there are numbers in array
MRW419
MRW4199mo ago
Which there are only 5 of. So the loop needs to go for only 5 times. 0, 1, 2, 3, and 4 correct?
SG97
SG979mo ago
in this case, yes
MRW419
MRW4199mo ago
Wait
SG97
SG979mo ago
your loop here was fine
MRW419
MRW4199mo ago
I think I understand
SG97
SG979mo ago
only thing you needed was the integer to sum them
MRW419
MRW4199mo ago
So were using the array and += them to the new interger to add them all up?
SG97
SG979mo ago
exactly
MRW419
MRW4199mo ago
Oh.... Thank you
SG97
SG979mo ago
no problem
MRW419
MRW4199mo ago
I would have been stuck here for a while so thank you very much
wwww
wwww9mo ago
or just
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine(array.Sum());
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine(array.Sum());
wwww
wwww9mo ago
No description
SG97
SG979mo ago
I assumed the term "iterate" means looping by hand
Pobiega
Pobiega9mo ago
Thats clearly not in the spirit of the assignment
wwww
wwww9mo ago
my bad 🙂
SG97
SG979mo ago
no worries
MRW419
MRW4199mo ago
Why do I get 0?
No description
Pobiega
Pobiega9mo ago
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 🙂
MRW419
MRW4199mo ago
Other stuff was for different Methods I did
No description
Pobiega
Pobiega9mo ago
Look at what is on what side of the operation
MRW419
MRW4199mo ago
Oh
Pobiega
Pobiega9mo ago
what does += do?
MRW419
MRW4199mo ago
got it, I forgot it would add it to the left side. My bad
Pobiega
Pobiega9mo ago
👍 on a side note, why is z declared as a parameter?
MRW419
MRW4199mo ago
Cause i already have a, b, and c. so I just made it z Tho I could have use c
Pobiega
Pobiega9mo ago
tahts not what I meant I mean, why is it declared in the method signature?
MRW419
MRW4199mo ago
Good question.....
Pobiega
Pobiega9mo ago
if I were to call your method, I'd expect to call it as int sum = ArraySum(myArray);
MRW419
MRW4199mo ago
yeah, i got rid of the z since it wasn't needed
Pobiega
Pobiega9mo ago
Okay. What did you use instead? You still need a place to store the total sum, as you are building it up, no?
MRW419
MRW4199mo ago
Nothing as i already had a public z so it was uneeded there Here
MRW419
MRW4199mo ago
All I needed was the array
No description
Pobiega
Pobiega9mo ago
Can you show me how you call this method?
MRW419
MRW4199mo ago
No description
Pobiega
Pobiega9mo ago
awesome lets copypaste that
MRW419
MRW4199mo ago
Ye!
Pobiega
Pobiega9mo ago
just put it twice like:
print(ArraySum(array));
print(ArraySum(array));
print(ArraySum(array));
print(ArraySum(array));
and run your code
MRW419
MRW4199mo ago
Why put it twice? it works on it own
Pobiega
Pobiega9mo ago
indulge me 🙂 I have my reasons to ask it.
MRW419
MRW4199mo ago
No description
MRW419
MRW4199mo ago
I've done it
Pobiega
Pobiega9mo ago
yep, and if you run it? what are the results
MRW419
MRW4199mo ago
Oh
Pobiega
Pobiega9mo ago
anything odd about the results? 🙂
MRW419
MRW4199mo ago
I see now
Pobiega
Pobiega9mo ago
so, how might we fix this?
MRW419
MRW4199mo ago
it made go to 30 since 15 was never reset Not sure on the top of my head
Pobiega
Pobiega9mo ago
well, I see you know about variables do you know of the different "scopes" variables can have?
MRW419
MRW4199mo ago
Uh, no. I have not heard that
Pobiega
Pobiega9mo ago
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)
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public void Method(int parameter)
{
var localVariable = parameter;
}
}
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 them
MRW419
MRW4199mo ago
So itstead of making it public, we have it private so it resets each time it's used>
Pobiega
Pobiega9mo ago
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?
Accord
Accord9mo ago
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.