tags:

views:

564

answers:

6

hi

I have a code here and I would like that it will display the first 10 and if I click on that, it will display again the second batch. I tried this first with my first for-code and it work now I'm working with arrays it seems it didn't accept it

The one I commented dont work? is this wrong? Thanks

long [] potenzen = new long[32];
potenzen[0] = 1;

for (int i = 1; i < potenzen.Length; ++i)
{
    potenzen[i] = potenzen[i-1] * 2;
    //if (potenzen % 10 == 0)
    //    Console.ReadLine();
}

foreach (long elem in potenzen)
{
    Console.WriteLine(" " + elem);
}
+3  A: 

Applying the modulus operator to an array of longs is dubious.

Thomas L Holaday
thanks for this input. so modulus operator doesn't really work to array of longs?
tintincute
An array is an object. The modulus operator works only with numeric primitive types. http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx
Will
+5  A: 

You need:

if (i % 10 == 0)

and not:

if (potenzen % 10 == 0)
David M
+2  A: 

potenzen is an array so you maybe try

if (i % 10 == 0)

or maybe

if (potenzen[i] % 10 == 0)
Jhonny D. Cano -Leftware-
+4  A: 
long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
{ 
  potenzen[i]=potenzen[i-1]*2;
  Console.WriteLine(potenzen[i-1]);
  if (i % 10 == 0)
    Console.ReadLine();
}

is more in line with what you want. An improvement would be to separate your data-manipulation logic from your data display logic.

long [] potenzen = new long[32];
potenzen[0] = 1;
for (int i = 1; i < potenzen.Length; ++i)
  potenzen[i]=potenzen[i-1]*2;

for (int i = 0; i < potenzen.Length; ++i)
{
  Console.WriteLine(potenzen[i]);
  if (i % 10 == 0)
    Console.ReadLine();
}

Of course, you could do this without an array

long potenzen = 1;
for (int i = 1; i < 32; ++i)
{ 
  Console.WriteLine(potenzen);
  potenzen = potenzen * 2;
  if (i % 10 == 0)
    Console.ReadLine();
}
Will
+1  A: 

You're taking an array mod 10 -- at best, in an unsafe language, you'd be doing the modulo operation on a memory address.

This should work fine if you just change the line to:

// if you don't want to pause the first time you run it, replace with:
// if (i > 0 && i % 10 == 0) {
if (i % 10 == 0) {
    Console.ReadLine();
}
ojrac
+1  A: 

Try changing it to:

long [] potenzen = new long[32];
potenzen[0] = 1;
Console.WriteLine(potenzen[0]);
for (int i = 1; i < potenzen.Length; ++i)
{
    potenzen[i]=potenzen[i-1]*2;
    Console.WriteLine(potenzen[i]);
    if (i % 10 == 0)
    {
        var s = Console.ReadLine();
        // break if s == some escape condition???
    }
}

Right now, you're never printing, unless you completely finish your first for loop. My guess is that you're not allowing the full 32 elements to complete, so you're never seeing your results -

This will print them as they go.

Reed Copsey
i'm seeing my results. that's right, I'm not allowing the 32 elements to complete. I would like to see the first batch (10) elements and if I enter again, I will see the next batch of 10 elements. Sorry i don't understand the last sentence of your question? what do you mean,---
tintincute
The way you had it, it never displayed until AFTER you had told it to do all of your processing. The way I rewrote it, it will do 10 and display them, then prompt you (then do the next 10 + display them).
Reed Copsey
hi thanks Reed, I got it! Thanks for the clear explanation:-)
tintincute