array subscript

How do you assign an array subscript value to a variable
I'm assuming you mean doing this in the declaration:
 
int arr1[x];

In my example, this is only valid if x is const, like:
1
2
3
4
5
...
const int x = 100;
...
int arr1[x];
...

If that isn't what you wanted, then I apologize.
1
2
3
4
5
6
7
8
9
10
11
// declaring an array which can hold three integer values
int arr[3];
// then Initialize the values
arr[0]=5; // first position holds 5
arr[1]=10;// second position holds 10
arr[2]=15; // third position holds 15
// Note size of the array is 3, but index are 0,1, and 2
// Now if you want to assign a variable with an array value
int myValue= arr[1]; // myValue now holds 10;
// You can also do declaration and initialization together
int YourArray[]={100,200,300};
Last edited on
NO that was good i just had a brain fart for a second.

I am writing a function to return a subscript and it isn't returning the right one. It keeps returning array[1] when the one it should is really array[8].

this is my function.... any ideas where im going wrong

double findLowMonth(double array[])
{
int lowMonth = 1, i;
double lowAmount = array[1];
for (i=2; i<=12; i++)
{
if (array[i] < lowAmount)
{
array[i] = lowAmount;
i = lowMonth;
}
}
return lowMonth;
}
yea.. simple mistake. heh.
i = lowMonth;

You just keep re-assigning i to 1. this makes an infinate loop.

You probably meant
lowMonth = i;

And while this part I'm about to talk about does work.. just for sake of clarity, i might drop the int i from above
int lowMonth = 1, i;
int lowMonth = 1; // use this instead

and then declare i in the for loop
for (int i = 2; i<=12; i++)

What you have works, but this way is easier to read
Last edited on
Topic archived. No new replies allowed.