Help with iterating an array...

  • Thread starter Thread starter Gen
  • Start date Start date
G

Gen

Hi - I'm a c# student, as a small class exercise we were asked to write a
program that uses an Average function using the params keyword. My
problem was with the loop, I could only get the Do loop to work. When
trying the for and foreach I got errors (explained below)

for (int i = 0; i < list.Length; i++)
{
x = x + list;
}
but when i put x = x/list.Length out here it said something like I couldn't
use x here.

The Do loop below worked. But can someone show me how to do this using for
or foreach? I just want to know why I couldn't get it to work.

public static void Average(params int[] list)
{
int x = 0;
int i = 0;
do
{
x = x + list[i++];
}
while(i < list.Length);
x = x / list.Length;
Console.WriteLine(x);
}
static void Main()
{
Average(2, 5, 8);
}
 
Gen said:
Hi - I'm a c# student, as a small class exercise we were asked to write a
program that uses an Average function using the params keyword. My
problem was with the loop, I could only get the Do loop to work. When
trying the for and foreach I got errors (explained below)

for (int i = 0; i < list.Length; i++)
{
x = x + list;
}
but when i put x = x/list.Length out here it said something like I couldn't
use x here.

The Do loop below worked. But can someone show me how to do this using for
or foreach? I just want to know why I couldn't get it to work.


It would be best if you could post your attempts at using for/foreach.
If we just post the actual answer, it wouldn't really help much -
correcting your broken code would teach you more, I think.
 
Hi Gen,

I agree with Jon. The foreach-loop works for sure ... this here works for
me:

public static void Average( params int[] list ) {

int x = 0;

foreach ( int i in list )
x += i;

x = x / list.Length;

Console.WriteLine( x );
}

static void Main() {

Average( 2, 5, 8 );

}

Regards,

Frank Eller
 
Hi Frank,
Thanks. I couldn't remember exactly what I had done, and now I can't seem
to recreate what I did wrong. All I remember is I had the "x = x /
list.Length;" on the outside of a " { " and it gave me some sort of error
about the "x" not being allowed there. I still don't understand why in the
foreach loop that the line after the x += i; doesn't execute until the
iteration is done.

With your help I figured out the for loop as well, but still not sure why
division runs after the loop.

public static void Average(params int[] list)

{

int x = 0;

for (int i = 0; i < list.Length; i++)

x += list;

x /= list.Length;

Console.WriteLine(x);

}

news.microsoft.com said:
Hi Gen,

I agree with Jon. The foreach-loop works for sure ... this here works for
me:

public static void Average( params int[] list ) {

int x = 0;

foreach ( int i in list )
x += i;

x = x / list.Length;

Console.WriteLine( x );
}

static void Main() {

Average( 2, 5, 8 );

}

Regards,

Frank Eller


Gen said:
Hi - I'm a c# student, as a small class exercise we were asked to write a
program that uses an Average function using the params keyword. My
problem was with the loop, I could only get the Do loop to work. When
trying the for and foreach I got errors (explained below)

for (int i = 0; i < list.Length; i++)
{
x = x + list;
}
but when i put x = x/list.Length out here it said something like I
couldn't use x here.

The Do loop below worked. But can someone show me how to do this using
for or foreach? I just want to know why I couldn't get it to work.

public static void Average(params int[] list)
{
int x = 0;
int i = 0;
do
{
x = x + list[i++];
}
while(i < list.Length);
x = x / list.Length;
Console.WriteLine(x);
}
static void Main()
{
Average(2, 5, 8);
}

 
Gen said:
Hi Frank,
Thanks. I couldn't remember exactly what I had done, and now I can't seem
to recreate what I did wrong. All I remember is I had the "x = x /
list.Length;" on the outside of a " { " and it gave me some sort of error
about the "x" not being allowed there. I still don't understand why in the
foreach loop that the line after the x += i; doesn't execute until the
iteration is done.

With your help I figured out the for loop as well, but still not sure why
division runs after the loop.

Do you mean you're not sure why the division *should* run after the
loop (in algorithmic terms) or why it runs after the loop in C# terms?

If it's the latter, this is where braces come in handy - I *always* use
braces for loops, if statements etc. The loop is exactly equivalent to:

for (int i = 0; i < list.Length; i++)
{
x += list;
}

Basically, if there are no braces, mentally put braces round the next
statement.
 
Back
Top