[C#] help on arrays

Joined
Nov 24, 2004
Messages
147
Reaction score
2
Hi everyone, Whats wrong with my code?

static void main()
{
int count = 0;
string[] test = new string[20];
int[] num1 = new int[20] ;


Console.Write("Test For String");
test[count] = Console.ReadLine();

Console.Write("Number 1");
num1[count] = int.Parse(Console.ReadLine());

Console.WriteLine(count + " " + test[count] + " " + num1[count]);

}


}
}


I been thinking how to store my results in a array for 5 times (with looping),

OUTPUT:

1 Hello 5
2 Everyone 4
3 From 3
4 This 2
5 Forum 1

Anyone know how to get this results?
 
You forgot your loop:
PHP:
for(count = 0; count < 20; count++)  // Loop 20 times
{
    test[count] = Console.ReadLine();

    Console.Write("Number {0}", count); //Here you print Number x depending on the count
    num1[count] = int.Parse(Console.ReadLine()); 

    Console.WriteLine(count + " " + test[count] + " " + num1[count]);
}
 
If i were to use 'Do loop' for looping, is this acceptable?

do
{
test[count] = Console.ReadLine();

Console.Write("Number {0}", count); //Here you print Number x depending on the count
num1[count] = int.Parse(Console.ReadLine());

count++;

Console.WriteLine(count + " " + test[count] + " " + num1[count]);
}
while (count < 20);
 
Other than this examples, is there any way to store a array for 10 times.. Meaning if there is two method

CalculatePay (calculate 10 times)

DisplayPay (display 10 times)

1 blah blah blah
2 blah blah blah
3 blah blah blah
4 blah blah blah
5 blah blah blah
6 blah blah blah
7 blah blah blah
8 blah blah blah
9 blah blah blah
10 blah blah blah


So when i calculatePay for 10 times, it automatically store the 10 results in the array, next i have another menu that i can display the 10 records of my payment at one go.
 
Of course, it is possible, what kind of argument does your CalculatePay function takes?

To display play you can do like that :

PHP:
public int[] pay = new int[10]; //Assuming you are storing pays as an integer.

public void DisplayPay()
{
    for(int i = 0; i < 10; i ++)   //Just loop through the array
    {
        Console.WriteLine("Pay #{0}  : {1]", i, pay[i]); //And display each pay
    }
}


//The CalculatePay function should look like something like that :
public void CalculatePay(whatever argument_it_takes)
{
    for(int i = 0; i < 10; i++)
    {
             //Do your calculations here
            pay[i] = yourstuff; //and store it in the array
    }
}

And could you try to use [ php ] tags when you post code? Thanks :)
 
Back