Originally posted by deedlit
Hey thanks for replying so quickly!
I understand what you mean by recording the location, I just can't figure out how (that's how pathetically new I am to this language).
So after I have that first loop to find the min, should I not have min become ar[i]? Or should I just start the next loop with something like
Code:
if (min = ar[0])
printf("%d, %d, %d", &ar[0], &ar[1], &ar[2]);
etc? Sorry for my slowness.
Look at your first example:
Code:
/* finding min of array elements */
min = ar[0];
for (i = 1; i < N; ++i)
if (min > ar[i]) min = ar[i];
printf("%d\n", min);
In this case, you can use this code, except to record the location where 'min' is located, you'd use another variable to hold it, and put the value of 'i' in it.
That way later when you wanted to find the min again, you'd just do:
Code:
for( i = value_of_i_from_last_loop; i < size_of_array; i++ )
...do whatever, print a[i] or whatever...
Now your code here has a few issues also:
Code:
if (min = ar[0])
printf("%d, %d, %d", &ar[0], &ar[1], &ar[2]);
The first one is the if check. You use '=' to assign a value, and '==' to test a value. Otherwise what happens here is that 'min' is assigned 'ar[0]' when the if check happens. Since I doubt that's what you want, you'd use '==' to test for equality.
Second is your printf. You only use the & symbol, in reference to addresses, when you want the address of a variable. In this case, you don't want that, because that would print the address of 'ar[0]', and you want what 'ar[0]' contains, so you'd leave the & off.
The reason you use the & when you use scanf, is when you have a non-pointer variable that you're trying to read into.
Scanf expects you to pass it pointers, so if you don't have a pointer directly, you can use the & operator to give the address of the variable you want to put the value into.
If the terminology confuses you, a pointer is a variable that points to another one. That is, it holds the address of another variable, rather than a value of a variable.
'min' holds the value of something from your array.
'&min' would give you where 'min' is located in memory. Otherwise known as its address.
Quzah.