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