|
OriginalGriff wrote: better to use a dictionary instead.
Hey ... we are talking about "high level languages" not those "evoluted assemblers" like C and C++!!
y=v[x] is an expression extracting a value for y corresponding to a discrete value of x using the v table.
The data structure shouldn't care, and should be up to the compiler to decide to use a plain array, and array with some index elaboration, a dictionary (or <map> in C++ lexicon), a "sparse matrix", a skip-list or whatever better suits the way data are stored, walked and accessed.
Otherwise, whats the meaning of "high level" ??
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Emilio Garavaglia wrote: you start at one if you are enumerating elements and you want to count them.
Really, lets try that...
int count = 1;
int [] a = { 1, 2, 3 };
foreach (var element in a)
count++;
As you can see, 0 maybe a better choice for start value here. You only raise to one when you pass the first element.
|
|
|
|
|
Nope.
I just was meaning another thing: I give you a box of pencils, and i want to know how many of them Are in the box.
How do you country them? 01234.... Or 12345.... ??
Are you the first child of your parents, or just the zeroth?
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Ok, so at start you have count = 0 pencils
you take the FIRST pencil, and AFTER THAT you have a count of 1
At first your parents had NO childs (count=0)
AFTER you were born the have 1 child (except you are a twin ...)
The count generally starts with 0 ind is incremented AFTER something happened
|
|
|
|
|
Klaus-Werner Konrad wrote: At first your parents had NO childs (count=0)
AFTER you were born the have 1 child (except you are a twin ...)
Good example. So what the hell child[0] represents ?
My parents have three children.
Let them be child[1], child[2], child[3], and the array size to be 3, so that I can be the "first" and not the 0th
(Yes: the array size was 0 when they did not have children yet, and [] was meaningless).
We are speaking about high level languages. Don't think in C++! An "index" is not necessarily a "counter". Starting at 0 or at 1 is a matter of context. Not something that is "always good" or "always bad"... You didn't demonstrate anything.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Well - in your FIRST year of life you are 0 years old
Generally an array index is not more than the OFFSET from the start of the array, and the count is the length.
So the first element is from offset 0 to offset 1, the second element is from offset 1 to offset 2 and so on.
And, NO, it shouldn't be context sensitive, just for consistency.
It would be absolutely confusing if one array would start with 0, the next with 1, the third with 19.75 ...
|
|
|
|
|
Klaus-Werner Konrad wrote: Generally an array index is not more than the OFFSET from the start of the
array, and the count is the length
Wrong.
A C array index is not more than the OFFSET from the start of the
array, and the count is the length. (C array, not generally).
But the question was another. "Should in high level programming language ..."
So, first, before teaching me the C language read carefolly the question (including the puctuation), than define:
1) What is an "high level language"? Is it ASM? C? C++? D? LISP? English? TNPL(aka "the new programming language": I just inventing it right now?)
2) What is an "array"? a "bouch of continguos memory holding identical objects"? a "set of identical object identifyed by an index"? a "set of similar objects identifyed by a key"?
3) What is the behavior of elements and indexes respect to insertion removal? Shold the indexes remain contiguous? Shuold them remain stable?
If you think to a well specific language (like C) every of the above question has an exact precise ansewr.
If you are designing something on a higer abstraction level ... all of those answers can be true. Of course, you'll come to different implementations. Sometimes even not based on C arrays.
Just as am example, give a look on how the "D laguage" defines arrays[^].
Yes, in their simple declination they start at 0, but can have even whatever kind of key!
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Ok -you're right !
I'm just another 'old-fashioned' programmer, who start ist's life with BASIC (Sharp PC1211), learned PL/1 and FORTRAN at university, followed by FORTH at Sharp PC 1512, proceeded to ASM Z80 while adapting my own built Z80 computer system to CP/M, then switched to C.
But in those old days memory was a real issue - a Z80 can only access 64 KB of memory, and this with memory mapped display space ...
Today memory isn't a real issue anymore, and processing speed doesn't (really) matter, too - my original Z80-system was by far the fastest system possible, built purely with 8 MHz-parts (1983). This was by far more than the original IBM PC speed (not to mention the ability of vectored interrupts etc.)
So, today, we as developers, should say 'don't bother' to performance issues ????
Just an example:
i < count in pseudo-assempler means i-count; if (sign_flag==-1) THEN ...
i a<= count means i-count; if (sign_flag==-1 OR zero_flag==TRUE) THEN ...
As you can see, the latter is less optimal, even if we talk about nanoseconds, but in summarize it can be a real difference in performance.
And: I Like PURE C - *I* can allocate memory, when I need it, and *I* decide, when to release it !!!
Of course, one have to be very careful about it, but - correct used - it gave you the most control, and the most power !
Another question is: WHO needs more 'abstraction level' ?
If I have a well done codebase fore the basic tasks (i.e. wrappers to WIN API and other basics) then I can do my buisiness layer even in pure C (or C++) as well ...
|
|
|
|
|
Bacon or CListCtrl are involved: it should obviously not be possible to have
Bacon[0] or
CListCtrl[0] As that would imply that we have run out...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Bacon[AsMuchAsPossible] ????
I'm not heavy - I'm KIDNAP RESISTANT... ----- Don't tell my folks I'm a computer programmer - They think I'm a piano player in a cat house... ----- Da mihi sis crustum Etruscum cum omnibus in eo! ----- Everybody is ignorant, only on different subjects - Will Rogers, September 7, 1924
|
|
|
|
|
Bacon[int64.MaxValue]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
OriginalGriff wrote: Bacon[int64.MaxValue]
You have no ambitions, I'm sure you get more.
BTW is that bacon by weight [kilos? ] or slices?
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
|
|
|
|
|
I always count my bacon by the Buttie.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
...because it makes loops work more obviously:
for (int index = 0; index < array.Length; index++) Rather than
for (int index = 1; index < array.Length + 1; index++)
(by the way, I'm a C# developer which never uses unsafe pointers and things like that).
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
So don't do that - use foreach instead... Then it doesn't matter anyhow.
I'm not heavy - I'm KIDNAP RESISTANT... ----- Don't tell my folks I'm a computer programmer - They think I'm a piano player in a cat house... ----- Da mihi sis crustum Etruscum cum omnibus in eo! ----- Everybody is ignorant, only on different subjects - Will Rogers, September 7, 1924
|
|
|
|
|
Not always possible: what if you are removing all entries with "XXX" in the name? You can't use a foreach where the IEnumerable changes...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
|
What about this?
for (int index = 1; index <= array.Length; index++)
That's what I used to do in BASIC (of course the syntax was different)
BTW I voted for 0. Habit.
modified on Monday, March 7, 2011 9:46 AM
|
|
|
|
|
If you did that you'd fall off the end of the array - you need:
for (int index = 1; index <= array.Length; index++)
|
|
|
|
|
Copy & paste mistake. Fixed, thanks
|
|
|
|
|
for (int index = 1; index <= array.Length; index++)
Ya i am agree this is the right way.........
|
|
|
|
|
You misunderstand. There is no right way, I only pointed out a different method
|
|
|
|
|
Mathematically,
for (int index = 1; index <= array.Length; index++) would be the right way to say it and count it.
It's been drilled into programmer's heads that 0 is better than 1 that it has become a religious offense to state otherwise.
|
|
|
|
|
|
We're all someone's infidel. Death to all, and to all a good night.
|
|
|
|