English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What does this code mean ?
for ( i = 1, i < n , i ++ )

i know i is an integer from this line
int curMax, i ;

also i know that n is an integer from this line

int CompareToMax (int array[ ], int n )

does this line means that n = 1,2,3,4 .... if that is the case then why is an array always starting like array [0] , then array [1] , etc ??

what's the point of defining array staring with [0], [1], [2]
and integers with 1,2,3...

2007-11-02 09:14:01 · 4 answers · asked by sum142121 1 in Computers & Internet Programming & Design

4 answers

the code is the start of a loop where i goes from 1 to n, whatever n happens to be at the time the loop is started. I'd have to see what's inside the loop to tell you want it's doing, so I don't know if you are operating on the array starting at index 1 (which as you say is the 2nd index since arrays start at index 0) or if there is a line such as array[i-1] so it starts at zero.

2007-11-02 09:22:29 · answer #1 · answered by Stache Man 6 · 0 0

For is the starting statement of a loop.
The parameters to the for are the variable and its initial value, the continuation condition and the increment rule.

So you start the loop with i having a value of 1.
you then test i to make sure that it is less than n
If i is less than n then you continue with the code in the loop. At then end of the loop, may be a next statement or just a curly bracket (}) you increment i by the increment rule and then do the test again.

The integer n is a memory location that contains a single numeric value.

As for arrays, this depends on the programming language but a lot of arrays do indeed start with the value 0 (zero).
However, if you want to do a random access of an array and the values you have are say 1 to 10 then it may well be worth defining an array with 11 elements and ignoring the 0 element rather than confusing the program by altering the subscript with a subtraction of 1 to adjust the number to fit the array.

2007-11-02 16:27:19 · answer #2 · answered by AnalProgrammer 7 · 0 0

The "for" loop will begin with i having the value 1; it will continue looping while i is less than n, and it's using the autoincrement shorthand "++" - which means the value of i will be incremented by one each time it loops.

For languages where arrays are zero-based, you are correct: array[i], where i=1 is actually the second element in the array.

array[ i - 1] would be the same as array[0] in that case.

You could alternately define the "for" loop as:

for (int i = 0; i < n; i++)
{
...array[i]
}

Lastly > "does this line means that n = 1,2,3,"?

No, based solely on the information you provided, the value of "n" is not being incremented, only "i"

2007-11-02 16:47:35 · answer #3 · answered by christopher c 5 · 0 0

lets say in vb.net
dim names as new ArrayList
names.add ("john")
name.add ("jerry")
name.add ("aryan")

so ur array has 3 items
and u want to print them
u can print them 1 by 1. but what if u have thousands of items in ur array?
then u use a loop so u can type them all in a simple loop like this:

dim x as integer = 0
n = names.count

for x = 0 to n step 1
response.write(names(x))
next

this is exactly like:
or ( i = 0, i < n , i ++ )
{
print names(i)
}
except it's in a different language
for x=0

2007-11-02 16:29:56 · answer #4 · answered by aryaxt 3 · 0 0

fedest.com, questions and answers