C# for statement and arrays...

Associate
Joined
30 Mar 2004
Posts
1,148
Location
West Wing
Hi, i've been trying out a bit of programming using C# language in my spare time. Im a complete newbie so need things broken down to me if im to understand them.

I think I more or less understand OOP in a conceptual way and its fascinating! Just a matter of learning the language so i can get my ideas into code to do what I want.

So ive made a console application that asks the user for an integer, n.

Then i want to take every number from 1 up to n and enter into an array called step1[].

What control statement should I use and how exactly do i do this?

Thanks,
 
You could use a for loop to do this.
http://msdn.microsoft.com/en-us/library/ch45axte.aspx

Something like:
Code:
for (int i = 1; i <= n; i++)
{
    step1[i-1] = i;
}

Should do it. Arrays in C# are zero-indexed though, and I've done my loop to go from 1 to n as per your requirements. Thus I need to put the 1st number into array index zero unless I define the array with n+1 elements in it.
If you read up on arrays in C# you'll find more info on this.

There are several helper methods that can do a similar thing for you though.
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

This method generates a sequence of integers, so you could do something like:
Code:
var step1 = Enumerable.Range(1,n).ToArray();

and get a similar result.
It doesn't really show you exactly what's going on as the other method does, but I'd prefer something like that in a real program, so it's good to know about it at least.
 
Thanks, that works fine.

I didn't know arrays were zero-indexed so I was getting an error: "index was outside the bounds of the array". I included the "-1" and that cures it.

I prefer the first option, shows whats happening up front.
 
Thanks, that works fine.

I didn't know arrays were zero-indexed so I was getting an error: "index was outside the bounds of the array". I included the "-1" and that cures it.
Use a List instead of an array. Arrays are of fixed size, Lists are dynamic size. You can then do stuff like:

Code:
var steps = new List<int>();
for  (var i = 0; i < n; i++) {
 steps.Add(i+1);
}

or
Code:
var steps = new List<int>();
steps.AddRange(Enumerable.Range(1, n));

I prefer the first option, shows whats happening up front.
You'll get over that soon enough :p
 
What is it with stereo-type developers that have to complicate stuff? :confused: Why bother posting that The Asgard? Does it even matter at this stage? Certainly not.

Lists make life easier, particularly for someone learning the ropes of a language.

Besides - the *only* overhead associated with Lists is if you frequently change their size which something that's going to bite you in the bum with Arrays anyway!

Plus .NET arrays are crap. They implement IList, but at the same time.. don't implement it. Lots of NotSupportedExceptions. Clearly the .NET team had a deadline to meet and cut a corner (or just couldn't be arsed), rather than create an interface for indexing or summin. :p
 
Last edited:
What is it with stereo-type developers that have to complicate stuff? :confused: Why bother posting that The Asgard? Does it even matter at this stage? Certainly not.

Lists make life easier, particularly for someone learning the ropes of a language.

Besides - the *only* overhead associated with Lists is if you frequently change their size which something that's going to bite you in the bum with Arrays anyway!

Plus .NET arrays are crap. They implement IList, but at the same time.. don't implement it. Lots of NotSupportedExceptions. Clearly the .NET team had a deadline to meet and cut a corner (or just couldn't be arsed), rather than create an interface for indexing or summin. :p

Use the right tools for the right job. Lists are slow.
 
Use the right tools for the right job. Lists are slow.

:confused:
I'd agree with DJ Jestar that you should use a List over an Array for most stuff.

I would always make my code expressive and readable before performance tuning the hell out of it.
Lists make the code more expressive than using Arrays and generally I'd always use them first until it proved to be a performance bottleneck.
 
Code:
//step5 - remove every 7th number after 7
            for (i = 14; i <= listLength; i = i + 7)
            {
                numList.Remove(i);
                //Console.WriteLine(i);
            }

There are several steps that remove certain numbers from the list, that eventually will leave just the prime numbers. Is it possible to collapse chunks of code to make it more manageable? I can imaging things getting very messy if I had to do something which needed more content.

Btw, it was just taking a while to crunch the numbers.
 
Lists ARE slower than arrays. If you want to know why you can google it.

In this example, as you know the size of the array when you define it - an array is the correct structure to use.

Also my job.

I quite often use lists for their helper methods, maintenance is easier when you have people like jester working for you :P
 
Last edited:
Code:
//step5 - remove every 7th number after 7
            for (i = 14; i <= listLength; i = i + 7)
            {
                numList.Remove(i);
                //Console.WriteLine(i);
            }

There are several steps that remove certain numbers from the list, that eventually will leave just the prime numbers. Is it possible to collapse chunks of code to make it more manageable? I can imaging things getting very messy if I had to do something which needed more content.

Btw, it was just taking a while to crunch the numbers.
It's difficult to judge without knowing the overall scope of things. We could suggest some changes here and there based on what we know, but that could make it difficult for you in other areas we don't know about. I'm not asking you to reveal everything you have, and I'm certainly not offering to do it for you! :)

Stick to the mantra "get it working, then fix it" before going too far though. Basically, do what you need to do to functionally have it working, then worry about code maintenance/readability. Then, and only if it is a problem, worry about performance optimisations.
 
I own the company.
So you're a director. You'll be too busy directing the company to actually do any development then, surely? Unless you are a self-employed contractor trying to pull a fast one.

If you don't understand the overheads and why Lists are slow I suggest using your friend Google to find out why.
translation: I know just enough to be dangerous, so you'll have to find out for yourself.
 
Lists ARE slower than arrays. If you want to know why you can google it.
I don't disagree, but I do disagree that it matters in the OPs case, and that using an Array for the sake of "it may be fractionally quicker, but will add a bit of complexity" is not worth it.

In this example, as you know the size of the array when you define it - an array is the correct structure to use.
See above :)

Also my job.

I quite often use arrays for their helper methods, maintenance is easier when you have people like jester working for you :P
:D
 
Back
Top Bottom